Esempio n. 1
0
 /**
  * Add service to container wrapping it before
  *
  * @param string $name
  * @param mixed $data
  */
 protected function addFactory($name, $data)
 {
     if ($data['type'] === self::TYPE_PARAM) {
         $this->container[$name] = $this->container->protect($data['function']);
         return;
     }
     $parameters = $this->parametersConfig;
     $closure = function ($c) use($data, $parameters) {
         $args = array();
         // fetch arguments for injecting into object
         if (!empty($data['arguments']) && is_array($data['arguments'])) {
             foreach ($data['arguments'] as $argument) {
                 if (is_string($argument) && strlen($argument) > 1 && ($argument[0] == '@' || $argument[0] == '%' && $argument[strlen($argument) - 1] == '%' && strlen($argument) > 2)) {
                     $name = substr($argument, 1);
                     switch ($argument[0]) {
                         case '@':
                             $args[] = $c[$name];
                             break;
                         case '%':
                             $name = substr($name, 0, -1);
                             if ($parameters->has($name)) {
                                 $args[] = $parameters->get($name);
                             }
                             break;
                     }
                 } else {
                     $args[] = $argument;
                 }
             }
         }
         $class = new ReflectionClass($data['class']);
         return $class->newInstanceArgs($args);
     };
     if ($data['type'] === self::TYPE_FACTORY) {
         $closure = $this->container->factory($closure);
     }
     $this->container[$name] = $closure;
 }