Exemple #1
0
 /**
  * @param string $key
  * @param string $instanceOf
  * @param callable|null $notFound
  * @return mixed|null
  */
 public function find(string $key, string $instanceOf, callable $notFound = null)
 {
     // Check in runtime memory
     if ($this->repo->has($key)) {
         $pull = $this->repo->pull($key);
         if (is_object($pull) && is_a($pull, $instanceOf)) {
             return $pull;
         }
     }
     // Check in cache
     if (isset($this->cache)) {
         $cached = $this->cache->get($key);
         if (is_object($cached) && is_a($cached, $instanceOf)) {
             return $cached;
         }
     }
     if (is_callable($notFound)) {
         $callBack = call_user_func($notFound);
         if (is_object($callBack)) {
             $this->set($key, clone $callBack);
             return $callBack;
         }
     }
     return null;
 }
Exemple #2
0
 /**
  * Retrieve an instance of service from container
  *
  * @param string $key
  * @param array ...$args
  * @return mixed
  * @throws ContainerException
  * @throws Exception\RepositoryException
  */
 public function get(string $key, ...$args)
 {
     // Check if service exists
     if (!$this->has($key)) {
         throw ContainerException::serviceNotFound(__METHOD__, $key);
     }
     // Check if its in Repository
     if ($this->repo->has($key)) {
         // Pull instance from repository
         return $this->repo->pull($key);
     } else {
         // Get new instance of service class
         $service = $this->services[$key];
         return $service->createInstance($this, $args);
     }
 }