示例#1
0
 protected function _instantiateViaProxy(Config\Bean $definition, $args)
 {
     $proxyName = $this->_lookupMethodProxyGenerator->loadProxy($definition);
     array_unshift($args, $this);
     $proxy = $this->_instantiateByConstructor($proxyName, $args);
     return $proxy;
 }
 /**
  * @param Bean $bean
  * @return mixed
  */
 public function create(Bean $bean)
 {
     $useConfigurator = true;
     $originalClass = $class = ltrim($bean->class, '\\');
     if ($bean->factoryMethod) {
         // We don't have a clue what what the real class is, fake it and hope nothing breaks;
         $class = "stdClass";
     } else {
         if (class_exists($class)) {
             $rClass = new \ReflectionClass($class);
             if (!$rClass->implementsInterface('MooDev\\Bounce\\Config\\Configurable')) {
                 // The class definitely doesn't need the configurator, so we can disable it.
                 $useConfigurator = false;
             }
         }
     }
     $usesLookupMethods = false;
     if ($bean->lookupMethods) {
         if (!$this->proxyGeneratorFactory) {
             throw new BounceException("Proxy generator not configured, cannot use lookup-method");
         }
         // If we have lookup methods then the class is actually a generated proxy.
         $class = ltrim($this->proxyGeneratorFactory->loadProxy($bean), '\\');
         $usesLookupMethods = true;
     }
     $def = new Definition($class);
     if ($usesLookupMethods) {
         // The proxy will take an additional, first, constructor arg which is expected to be a bean factory.
         $def->addArgument($this->getBeanFactory());
     }
     if ($useConfigurator) {
         // We use the configurator if we know the class of the bean and it implements Configurable
         // or if we have no idea what the class of the bean is (there's a factory method.)
         $def->setConfigurator($this->getConfigurator());
     }
     if ($bean->scope) {
         // This is getting killed off in Symfony 3. Sigh.
         // TODO: deal with Symfony 3.
         switch ($bean->scope) {
             case "singleton":
                 $def->setScope(ContainerBuilder::SCOPE_CONTAINER);
                 break;
             case "prototype":
                 $def->setScope(ContainerBuilder::SCOPE_PROTOTYPE);
                 break;
             default:
                 $def->setScope($bean->scope);
         }
     }
     foreach ($bean->constructorArguments as $constructorArgument) {
         $def->addArgument($this->convertValueProviderToValue($constructorArgument));
     }
     foreach ($bean->properties as $name => $property) {
         // TODO: Could support setter injection using Reflection here?
         $def->setProperty($name, $this->convertValueProviderToValue($property));
     }
     if ($bean->factoryBean) {
         $def->setFactoryService($bean->factoryBean);
         $def->setFactoryMethod($bean->factoryMethod);
     } elseif ($bean->factoryMethod) {
         $def->setFactoryClass($originalClass);
         $def->setFactoryMethod($bean->factoryMethod);
     }
     return $def;
 }