示例#1
0
 /**
  * Adds the specified service to the service container.
  * @param  mixed  object, class name or service factory callback
  * @param  string optional service name (for factories is not optional)
  * @param  bool   promote to higher level?
  * @return void
  * @throws InvalidArgumentException, AmbiguousServiceException
  */
 public function addService($service, $name = NULL, $promote = FALSE)
 {
     if (is_object($service)) {
         if ($name === NULL) {
             $name = get_class($service);
         }
     } elseif (is_string($service)) {
         if ($name === NULL) {
             $name = $service;
         }
     } elseif (is_callable($service, TRUE)) {
         if (empty($name)) {
             throw new InvalidArgumentException('When factory callback is given, service name must be specified.');
         }
     } else {
         throw new InvalidArgumentException('Service must be name, object or factory callback.');
     }
     $lower = strtolower($name);
     if (isset($this->registry[$lower])) {
         throw new AmbiguousServiceException("Service named '{$name}' has been already registered.");
     }
     if (is_object($service)) {
         $this->registry[$lower] = $service;
     } else {
         $this->factories[$lower] = $service;
     }
     if ($promote && $this->parent !== NULL) {
         $this->parent->addService($service, $name, TRUE);
     }
 }