예제 #1
0
 /**
  * Instanciates $this->className and return the instance.
  * 
  * @param Container   $container The Di Container
  * @param null|string $name      Name of the current definition (if any)
  * 
  * @return VirtualProxyInterface
  * @throws Exceptions\InvalidClassDefinitionException
  */
 public function invoke(Container $container, $name = null)
 {
     $proxy = $this->getProxyFactory()->createProxy($container->propertizeString($this->className), function (&$wrappedInstance, LazyLoadingInterface $proxy) use($container, $name) {
         $wrappedInstance = parent::invoke($container, $name);
         $proxy->setProxyInitializer(null);
         return true;
     });
     return $proxy;
 }
예제 #2
0
 /**
  * Transform arguments to their real value if they are instance of InvokableInterface
  * or a @reference.
  * 
  * @param array<mixed> $args       List of arguments
  * @param Container    $container  The Di Container
  * @param null|string  $definition Name of the current definition (if any)
  * 
  * @return array<mixed>
  * @throws Exceptions\InvalidArgumentException
  */
 protected function propertizeArguments(array $args, Container $container, $definition = null)
 {
     $return = array();
     foreach ($args as $idx => $arg) {
         $arg = $this->transformValueType($arg);
         if (is_string($arg)) {
             $arg = $container->propertizeString($arg);
         }
         if (is_string($arg) && strpos($arg, '@', 0) === 0) {
             $arg = new Reference(substr($arg, 1));
         } elseif (is_array($arg)) {
             $arg = $this->propertizeArguments($arg, $container, $definition);
         }
         try {
             $return[$idx] = $arg instanceof InvokableInterface ? $arg->invoke($container, $definition) : $arg;
         } catch (\Fwk\Di\Exception $exp) {
             throw new Exceptions\InvalidArgumentException($idx, $definition, $exp);
         }
     }
     return $return;
 }
예제 #3
0
파일: ContainerBuilder.php 프로젝트: fwk/di
 /**
  * Converts XML class definitions from parsing results
  * 
  * @param array     $classDefs Parsing results
  * @param Container $container The Di Container
  * 
  * @return void
  */
 protected function applyClassDefinitions(array $classDefs, Container $container)
 {
     foreach ($classDefs as $name => $infos) {
         $shared = (bool) $this->transformValueType($infos['shared']);
         $lazy = (bool) $this->transformValueType($infos['lazy']);
         $defClass = $lazy ? LazyClassDefinition::class : ClassDefinition::class;
         $def = new $defClass($infos['className'], $infos['arguments']);
         foreach ($infos['methodsCalls'] as $mnfos) {
             $def->addMethodCall($container->propertizeString($mnfos['method']), $mnfos['arguments']);
         }
         $def->setShared($shared)->setData($infos['data']);
         $container->set($name, $def);
     }
 }
예제 #4
0
파일: ClassDefinition.php 프로젝트: fwk/di
 /**
  * Instanciates the class ($this->className) 
  * 
  * @param Container   $container  The Di Container
  * @param null|string $definition Name of the current definition (if any)
  * 
  * @return object
  * @throws Exceptions\ClassNotFoundException
  * @throws Exceptions\InvalidClassDefinitionException
  */
 protected function newInstance(Container $container, $definition = null)
 {
     if (is_string($this->className) && strpos($this->className, ':') >= 0) {
         $this->className = $container->propertizeString($this->className);
     }
     if (!class_exists($this->className, true)) {
         throw new Exceptions\ClassNotFoundException($this->className);
     }
     $reflect = new \ReflectionClass($this->className);
     if (null !== $reflect->getConstructor()) {
         $args = array();
         try {
             $args = $this->getConstructorArguments($container, $definition);
         } catch (Exception $exp) {
             throw new InvalidClassDefinitionException($this->className, $definition, $exp);
         }
         $return = $reflect->newInstanceArgs($args);
     } else {
         $return = new $this->className();
     }
     return $return;
 }
예제 #5
0
 /**
  * Transforms a wildcard to a regex
  *
  * @param string $value
  *
  * @return string
  * @throws Exceptions\SearchException
  */
 protected function searchQueryToRegex($value, Container $container)
 {
     $original = $value;
     $value = $container->propertizeString($value);
     if (!is_string($value)) {
         throw new Exceptions\SearchException("Invalid Query: '{$original}' because of a non-string value.");
     }
     if (empty($value)) {
         return "/(.+){1,}/";
     }
     return '/^' . str_replace(array('?', '*'), array('(.+){1}', '(.+){1,}'), $value) . '$/';
 }