Ejemplo n.º 1
0
 /**
  * Exists the service?
  * @param  string service name
  * @param  bool   must be created yet?
  * @return bool
  */
 public function hasService($name, $created = FALSE)
 {
     if (!is_string($name) || $name === '') {
         throw new InvalidArgumentException("Service name must be a non-empty string, " . gettype($name) . " given.");
     }
     $lower = strtolower($name);
     return isset($this->registry[$lower]) || !$created && isset($this->factories[$lower]) || $this->parent !== NULL && $this->parent->hasService($name, $created);
 }
Ejemplo n.º 2
0
 /**
  * Gets the service object of the specified type.
  * @param  string service name
  * @param  bool   throw exception if service doesn't exist?
  * @return mixed
  */
 public function getService($name, $need = TRUE)
 {
     if (!is_string($name) || $name === '') {
         throw new InvalidArgumentException("Service name must be a non-empty string, " . gettype($name) . " given.");
     }
     $lower = strtolower($name);
     if (isset($this->registry[$lower])) {
         return $this->registry[$lower];
     } elseif (isset($this->factories[$lower])) {
         $service = $this->factories[$lower];
         if (is_string($service)) {
             if (substr($service, -2) === '()') {
                 // trick to pass callback as string
                 $service = substr($service, 0, -2);
             } else {
                 fixNamespace($service);
                 if (!class_exists($service)) {
                     throw new AmbiguousServiceException("Cannot instantiate service '{$name}', class '{$service}' not found.");
                 }
                 return $this->registry[$lower] = new $service();
             }
         }
         fixCallback($service);
         if (!is_callable($service)) {
             $able = is_callable($service, TRUE, $textual);
             throw new AmbiguousServiceException("Cannot instantiate service '{$name}', handler '{$textual}' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
         }
         return $this->registry[$lower] = call_user_func($service);
     }
     if ($this->parent !== NULL) {
         return $this->parent->getService($name, $need);
     } elseif ($need) {
         throw new InvalidStateException("Service '{$name}' not found.");
     } else {
         return NULL;
     }
 }
Ejemplo n.º 3
0
 /**
  * Gets the service object of the specified type.
  * @param  string service name
  * @param  bool   throw exception if service doesn't exist?
  * @return mixed
  */
 public function getService($name, $need = TRUE)
 {
     if (!is_string($name) || $name === '') {
         throw new InvalidArgumentException('Service name must be a non-empty string.');
     }
     $lower = strtolower($name);
     if (isset($this->registry[$lower])) {
         return $this->registry[$lower];
     } elseif (isset($this->factories[$lower])) {
         $service = $this->factories[$lower];
         if (is_string($service)) {
             if (substr($service, -2) === '()') {
                 // trick to pass callback as string
                 $service = substr($service, 0, -2);
             } else {
                 /**/
                 // fix for namespaced classes/interfaces in PHP < 5.3
                 if ($a = strrpos($service, '\\')) {
                     $service = substr($service, $a + 1);
                 }
                 /**/
                 if (!class_exists($service)) {
                     throw new AmbiguousServiceException("Cannot instantiate service, class '{$service}' not found.");
                 }
                 return $this->registry[$lower] = new $service();
             }
         }
         return $this->registry[$lower] = call_user_func($service);
     }
     if ($this->parent !== NULL) {
         return $this->parent->getService($name);
     } elseif ($need) {
         throw new InvalidStateException("Service '{$name}' not found.");
     } else {
         return NULL;
     }
 }