/** * @param $id * @return mixed * @throws CircularDependencyException */ public function get($id) { try { $service = $this->repository->get($id); } catch (ServiceNotFoundException $exception) { $service = $this->createService($id); } return $service; }
/** * Gets the instance via lazy initialization (created on first usage) * * @param $id * * @throws CircularDependencyException * * @return a registered service */ public function get($id) { $service = $this->repository->get($id); if (is_null($service)) { if (isset($this->servicesCreating[$id])) { $msg = 'Circular dependency detected: ' . implode(' => ', array_keys($this->servicesCreating)) . " => {$id}"; throw new CircularDependencyException($msg); } // remmember ids called $this->servicesCreating[$id] = true; // pass the container to force only one instantiation per class $service = $this->factory->create($id, $this); unset($this->servicesCreating[$id]); $this->repository->add($id, $service); } return $service; }