/**
  * {@inheritdoc}
  */
 public function getDefinition($name, MergeableDefinition $parentDefinition = null)
 {
     // Only merges with class definition
     if ($parentDefinition && !$parentDefinition instanceof ClassDefinition) {
         return null;
     }
     $className = $parentDefinition ? $parentDefinition->getClassName() : $name;
     if (!class_exists($className) && !interface_exists($className)) {
         return null;
     }
     $class = new \ReflectionClass($className);
     $definition = new ClassDefinition($name);
     // Constructor
     $constructor = $class->getConstructor();
     if ($constructor && $constructor->isPublic()) {
         $definition->setConstructorInjection(MethodInjection::constructor($this->getParametersDefinition($constructor)));
     }
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     return $definition;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function getDefinition($entryName)
 {
     $definition = new ClassDefinition($entryName, $this->className);
     if ($this->lazy !== null) {
         $definition->setLazy($this->lazy);
     }
     if ($this->scope !== null) {
         $definition->setScope($this->scope);
     }
     if (!empty($this->constructor)) {
         $parameters = $this->fixParameters($definition, '__construct', $this->constructor);
         $constructorInjection = MethodInjection::constructor($parameters);
         $definition->setConstructorInjection($constructorInjection);
     }
     if (!empty($this->properties)) {
         foreach ($this->properties as $property => $value) {
             $definition->addPropertyInjection(new PropertyInjection($property, $value));
         }
     }
     if (!empty($this->methods)) {
         foreach ($this->methods as $method => $parameters) {
             $parameters = $this->fixParameters($definition, $method, $parameters);
             $methodInjection = new MethodInjection($method, $parameters);
             $definition->addMethodInjection($methodInjection);
         }
     }
     return $definition;
 }
 /**
  * Create the parameter array to call a method.
  *
  * @param ClassDefinition  $definition
  * @param MethodInjection  $methodInjection
  * @param ReflectionMethod $methodReflection
  * @param array            $parameters       Force some parameters to specific values.
  *
  * @throws DefinitionException A parameter has no defined or guessable value.
  * @return array Array of parameters to use to call the method.
  */
 private function prepareMethodParameters(ClassDefinition $definition, MethodInjection $methodInjection = null, ReflectionMethod $methodReflection = null, array $parameters = array())
 {
     if (!$methodReflection) {
         return array();
     }
     $args = array();
     foreach ($methodReflection->getParameters() as $index => $parameter) {
         if (array_key_exists($parameter->getName(), $parameters)) {
             // Look in the $parameters array
             $value = $parameters[$parameter->getName()];
         } elseif ($methodInjection && $methodInjection->hasParameter($index)) {
             // Look in the definition
             $value = $methodInjection->getParameter($index);
         } else {
             // If the parameter is optional and wasn't specified, we take its default value
             if ($parameter->isOptional()) {
                 $args[] = $this->getParameterDefaultValue($parameter, $methodReflection);
                 continue;
             }
             throw DefinitionException::create($definition, sprintf("The parameter '%s' of %s::%s has no value defined or guessable", $parameter->getName(), $methodReflection->getDeclaringClass()->getName(), $methodReflection->getName()));
         }
         if ($value instanceof EntryReference) {
             $args[] = $this->container->get($value->getName());
         } else {
             $args[] = $value;
         }
     }
     return $args;
 }
Example #4
0
 private function dumpMethodParameters($className, MethodInjection $methodInjection)
 {
     $methodReflection = new \ReflectionMethod($className, $methodInjection->getMethodName());
     $args = array();
     foreach ($methodReflection->getParameters() as $index => $parameter) {
         if ($methodInjection->hasParameter($index)) {
             $value = $methodInjection->getParameter($index);
             if ($value instanceof EntryReference) {
                 $args[] = sprintf('$%s = link(%s)', $parameter->getName(), $value->getName());
             } else {
                 $args[] = sprintf('$%s = %s', $parameter->getName(), var_export($value, true));
             }
             continue;
         }
         // If the parameter is optional and wasn't specified, we take its default value
         if ($parameter->isOptional()) {
             try {
                 $value = $parameter->getDefaultValue();
                 $args[] = sprintf('$%s = (default value) %s', $parameter->getName(), var_export($value, true));
                 continue;
             } catch (ReflectionException $e) {
                 // The default value can't be read through Reflection because it is a PHP internal class
             }
         }
         $args[] = sprintf('$%s = #UNDEFINED#', $parameter->getName());
     }
     return implode(PHP_EOL . '        ', $args);
 }
Example #5
0
 /**
  * @param MethodInjection $methodInjection
  */
 public function addMethodInjection(MethodInjection $methodInjection)
 {
     $this->methodInjections[$methodInjection->getMethodName()] = $methodInjection;
 }
 /**
  * {@inheritdoc}
  */
 private function getMethodInjection(ReflectionMethod $method)
 {
     // Look for @Inject annotation
     /** @var $annotation Inject|null */
     try {
         $annotation = $this->getAnnotationReader()->getMethodAnnotation($method, 'DI\\Annotation\\Inject');
     } catch (AnnotationException $e) {
         throw new AnnotationException(sprintf('@Inject annotation on %s::%s is malformed. %s', $method->getDeclaringClass()->getName(), $method->getName(), $e->getMessage()), 0, $e);
     }
     $annotationParameters = $annotation ? $annotation->getParameters() : array();
     // @Inject on constructor is implicit
     if (!($annotation || $method->isConstructor())) {
         return null;
     }
     $parameters = array();
     foreach ($method->getParameters() as $index => $parameter) {
         $entryName = $this->getMethodParameter($index, $parameter, $annotationParameters);
         if ($entryName !== null) {
             $parameters[$index] = new EntryReference($entryName);
         }
     }
     if ($method->isConstructor()) {
         return MethodInjection::constructor($parameters);
     } else {
         return new MethodInjection($method->getName(), $parameters);
     }
 }