Author: Matthieu Napoli (matthieu@mnapoli.fr)
Inheritance: implements DI\Definition\Definition, implements CacheableDefinition, implements DI\Definition\HasSubDefinition
Exemplo n.º 1
0
 private function dumpMethods($className, ObjectDefinition $definition)
 {
     $str = '';
     foreach ($definition->getMethodInjections() as $methodInjection) {
         $parameters = $this->dumpMethodParameters($className, $methodInjection);
         $str .= sprintf(PHP_EOL . "    %s(" . PHP_EOL . "        %s" . PHP_EOL . "    )", $methodInjection->getMethodName(), $parameters);
     }
     return $str;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getDefinition($name)
 {
     if (!class_exists($name) && !interface_exists($name)) {
         return null;
     }
     $definition = new ObjectDefinition($name);
     // Constructor
     $class = new \ReflectionClass($name);
     $constructor = $class->getConstructor();
     if ($constructor && $constructor->isPublic()) {
         $definition->setConstructorInjection(MethodInjection::constructor($this->getParametersDefinition($constructor)));
     }
     return $definition;
 }
Exemplo n.º 3
0
 /**
  * Fixes parameters indexed by the parameter name -> reindex by position.
  *
  * This is necessary so that merging definitions between sources is possible.
  *
  * @param ObjectDefinition $definition
  * @param string          $method
  * @param array           $parameters
  * @return array
  */
 private function fixParameters(ObjectDefinition $definition, $method, $parameters)
 {
     $fixedParameters = array();
     foreach ($parameters as $index => $parameter) {
         // Parameter indexed by the parameter name, we reindex it with its position
         if (is_string($index)) {
             $callable = array($definition->getClassName(), $method);
             $reflectionParameter = new \ReflectionParameter($callable, $index);
             $index = $reflectionParameter->getPosition();
         }
         $fixedParameters[$index] = $parameter;
     }
     return $fixedParameters;
 }
Exemplo n.º 4
0
 private function assertClassExists(ObjectDefinition $definition)
 {
     if (!class_exists($definition->getClassName()) && !interface_exists($definition->getClassName())) {
         throw DefinitionException::create($definition, sprintf("Entry %s cannot be resolved: class %s doesn't exist", $definition->getName(), $definition->getClassName()));
     }
 }
Exemplo n.º 5
0
 private function readInjectableAnnotation(ReflectionClass $class, ObjectDefinition $definition)
 {
     try {
         /** @var $annotation Injectable|null */
         $annotation = $this->getAnnotationReader()->getClassAnnotation($class, 'DI\\Annotation\\Injectable');
     } catch (UnexpectedValueException $e) {
         throw new DefinitionException(sprintf('Error while reading @Injectable on %s: %s', $class->getName(), $e->getMessage()), 0, $e);
     }
     if (!$annotation) {
         return;
     }
     if ($annotation->getScope()) {
         $definition->setScope($annotation->getScope());
     }
     if ($annotation->isLazy() !== null) {
         $definition->setLazy($annotation->isLazy());
     }
 }
Exemplo n.º 6
0
 private function mergePropertyInjections(ObjectDefinition $definition)
 {
     foreach ($definition->getPropertyInjections() as $propertyName => $propertyInjection) {
         if (!array_key_exists($propertyName, $this->propertyInjections)) {
             // Add
             $this->propertyInjections[$propertyName] = $propertyInjection;
         }
     }
 }
Exemplo n.º 7
0
 private function mergeConstructorInjection(ObjectDefinition $definition)
 {
     if ($definition->getConstructorInjection() !== null) {
         if ($this->constructorInjection !== null) {
             // Merge
             $this->constructorInjection->merge($definition->getConstructorInjection());
         } else {
             // Set
             $this->constructorInjection = $definition->getConstructorInjection();
         }
     }
 }
Exemplo n.º 8
0
 /**
  * Fixes parameters indexed by the parameter name -> reindex by position.
  *
  * This is necessary so that merging definitions between sources is possible.
  *
  * @param ObjectDefinition $definition
  * @param string          $method
  * @param array           $parameters
  * @throws DefinitionException
  * @return array
  */
 private function fixParameters(ObjectDefinition $definition, $method, $parameters)
 {
     $fixedParameters = [];
     foreach ($parameters as $index => $parameter) {
         // Parameter indexed by the parameter name, we reindex it with its position
         if (is_string($index)) {
             $callable = [$definition->getClassName(), $method];
             try {
                 $reflectionParameter = new \ReflectionParameter($callable, $index);
             } catch (\ReflectionException $e) {
                 throw DefinitionException::create($definition, sprintf("Parameter with name '%s' could not be found. %s.", $index, $e->getMessage()));
             }
             $index = $reflectionParameter->getPosition();
         }
         $fixedParameters[$index] = $parameter;
     }
     return $fixedParameters;
 }
Exemplo n.º 9
0
 private function assertClassIsInstantiable(ObjectDefinition $definition)
 {
     if (!$definition->isInstantiable()) {
         throw DefinitionException::create($definition, sprintf('Entry "%s" cannot be resolved: the class is not instantiable', $definition->getName()));
     }
 }
Exemplo n.º 10
0
 private function assertClassIsInstantiable(ObjectDefinition $definition)
 {
     if (!$definition->isInstantiable()) {
         throw DefinitionException::create($definition, sprintf("Entry %s cannot be resolved: class %s is not instantiable", $definition->getName(), $definition->getClassName()));
     }
 }