示例#1
0
 /**
  * @param string $file
  * @param string $content
  * @dataProvider provider
  */
 public function testGenerateFactory($file, $content)
 {
     if (strtolower(substr(php_uname(), 0, 7)) == 'windows') {
         $this->markTestSkipped('Skipped since doesnt work on windows.');
     }
     FactoryDependencyInjectionContainer::$prefix = '';
     $config = new Configuration(null, '');
     $config->setClassConfig('rg\\injektor\\generators\\FGTestClassOne', array('singleton' => true));
     $config->setClassConfig('rg\\injektor\\generators\\FGTestClassFour', array('singleton' => true));
     $config->setClassConfig('rg\\injektor\\generators\\FGTestClassThree', array('params' => array('foo' => array('value' => 'foo'), 'four' => array('class' => 'rg\\injektor\\generators\\FGTestClassFour'))));
     $factoryGenerator = new TestingFactoryGenerator($config, '');
     $factoryGenerator->processFileForClass('rg\\injektor\\generators\\FGTestClassOne');
     $this->assertEquals($content, $factoryGenerator->files[$file]);
 }
示例#2
0
 protected function analyze()
 {
     $argumentClass = null;
     $isInjectable = $this->dic->isInjectable($this->docComment);
     if (!empty($this->classConfig['params'][$this->name]['class'])) {
         $argumentClass = $this->classConfig['params'][$this->name]['class'];
         $isInjectable = true;
     } else {
         if ($this->hasClass()) {
             $argumentClass = $this->getClass();
         }
     }
     if ($argumentClass && $isInjectable) {
         try {
             $namedClass = $this->dic->getNamedClassOfArgument($argumentClass, $this->docComment, $this->nameForAnnotationParsing);
             if ($namedClass) {
                 $argumentClass = $namedClass;
             }
         } catch (InjectionException $e) {
         }
         if ($argumentClass === 'rg\\injektor\\DependencyInjectionContainer') {
             $this->defaultValue = '\\' . $argumentClass . '::getDefaultInstance()';
         } else {
             $providerClassName = $this->dic->getProviderClassName($this->config->getClassConfig($argumentClass), new \ReflectionClass($argumentClass), $this->dic->getImplementationName($this->docComment, $this->nameForAnnotationParsing));
             if ($providerClassName && $providerClassName->getClassName()) {
                 $argumentFactory = $this->dic->getFullFactoryClassName($providerClassName->getClassName());
                 $this->className = $providerClassName->getClassName();
                 $this->factoryName = $argumentFactory;
                 $this->defaultValue = '\\' . $argumentFactory . '::getInstance(' . var_export(array_merge($providerClassName->getParameters(), $this->additionalArguments), true) . ')->get()';
             } else {
                 $argumentClass = $this->dic->getRealConfiguredClassName($this->config->getClassConfig($argumentClass), new \ReflectionClass($argumentClass));
                 $argumentClassReflection = new \ReflectionClass($argumentClass);
                 if (!$argumentClassReflection->isInstantiable() && !$argumentClassReflection->hasMethod('getInstance')) {
                     $this->setParameterToDefault();
                     return;
                 }
                 $argumentFactory = $this->dic->getFullFactoryClassName($argumentClass);
                 $this->className = $argumentClass;
                 $this->factoryName = $argumentFactory;
                 $this->defaultValue = '\\' . $argumentFactory . '::getInstance(' . var_export($this->additionalArguments, true) . ')';
             }
         }
     } else {
         $this->setParameterToDefault();
     }
 }
 public function tearDown()
 {
     FactoryDependencyInjectionContainer::$prefix = '';
 }
示例#4
0
 protected function getFactoryMethod(\ReflectionMethod $method, $classConfig)
 {
     $factoryMethod = new Generator\MethodGenerator($this->dic->getFactoryMethodName($method->name));
     $factoryMethod->setParameter(new Generator\ParameterGenerator('object'));
     $factoryMethod->setStatic(true);
     $arguments = $method->getParameters();
     $body = '$i = 0;' . PHP_EOL;
     $methodArgumentStringParts = array();
     if (count($arguments) > 0) {
         $factoryMethod->setParameter(new \Zend\Code\Generator\ParameterGenerator('parameters', 'array', array()));
         foreach ($arguments as $argument) {
             /** @var \ReflectionParameter $argument */
             $argumentName = $argument->name;
             $methodArgumentStringParts[] = '$' . $argumentName;
         }
         $body .= 'if (!$parameters) {' . PHP_EOL;
         foreach ($arguments as $argument) {
             /** @var \ReflectionParameter $argument */
             $injectionParameter = new \rg\injektor\generators\InjectionParameter($argument, $classConfig, $this->config, $this->dic, InjectionParameter::MODE_NO_ARGUMENTS);
             try {
                 if ($injectionParameter->getClassName()) {
                     $this->factoryGenerator->processFileForClass($injectionParameter->getClassName());
                 }
                 if ($injectionParameter->getFactoryName()) {
                     $this->usedFactories[] = $injectionParameter->getFactoryName();
                 }
                 $body .= '    ' . $injectionParameter->getProcessingBody();
             } catch (\Exception $e) {
                 $body .= '    ' . $injectionParameter->getDefaultProcessingBody();
             }
         }
         $body .= '}' . PHP_EOL;
         $body .= 'else if (array_key_exists(0, $parameters)) {' . PHP_EOL;
         foreach ($arguments as $argument) {
             /** @var \ReflectionParameter $argument */
             $injectionParameter = new \rg\injektor\generators\InjectionParameter($argument, $classConfig, $this->config, $this->dic, InjectionParameter::MODE_NUMERIC);
             try {
                 if ($injectionParameter->getClassName()) {
                     $this->factoryGenerator->processFileForClass($injectionParameter->getClassName());
                 }
                 if ($injectionParameter->getFactoryName()) {
                     $this->usedFactories[] = $injectionParameter->getFactoryName();
                 }
                 $body .= '    ' . $injectionParameter->getProcessingBody();
             } catch (\Exception $e) {
                 $body .= '    ' . $injectionParameter->getDefaultProcessingBody();
             }
         }
         $body .= '}' . PHP_EOL;
         $body .= 'else {' . PHP_EOL;
         foreach ($arguments as $argument) {
             /** @var \ReflectionParameter $argument */
             $injectionParameter = new \rg\injektor\generators\InjectionParameter($argument, $classConfig, $this->config, $this->dic, InjectionParameter::MODE_STRING);
             try {
                 if ($injectionParameter->getClassName()) {
                     $this->factoryGenerator->processFileForClass($injectionParameter->getClassName());
                 }
                 if ($injectionParameter->getFactoryName()) {
                     $this->usedFactories[] = $injectionParameter->getFactoryName();
                 }
                 $body .= '    ' . $injectionParameter->getProcessingBody();
             } catch (\Exception $e) {
                 $body .= '    ' . $injectionParameter->getDefaultProcessingBody();
             }
         }
         $body .= '}' . PHP_EOL;
     }
     $body .= '$result = $object->' . $method->name . '(' . implode(', ', $methodArgumentStringParts) . ');' . PHP_EOL . PHP_EOL;
     $body .= PHP_EOL . 'return $result;';
     $factoryMethod->setBody($body);
     return $factoryMethod;
 }