/**
  * {@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($this->getConstructorInjection($constructor));
     }
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     return $definition;
 }
 /**
  * {@inheritdoc}
  * @throws AnnotationException
  * @throws InvalidArgumentException The class doesn't exist
  */
 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 $parentDefinition;
     }
     $class = new ReflectionClass($className);
     $definition = new ClassDefinition($name);
     $this->readInjectableAnnotation($class, $definition);
     // Browse the class properties looking for annotated properties
     $this->readProperties($class, $definition);
     // Browse the object's methods looking for annotated methods
     $this->readMethods($class, $definition);
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     return $definition;
 }
 /**
  * {@inheritdoc}
  * @throws AnnotationException
  * @throws InvalidArgumentException The class doesn't exist
  */
 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 $parentDefinition;
     }
     $class = new ReflectionClass($className);
     $definition = new ClassDefinition($name);
     // Injectable annotation
     /** @var $injectableAnnotation Injectable|null */
     try {
         $injectableAnnotation = $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 ($injectableAnnotation) {
         if ($injectableAnnotation->getScope()) {
             $definition->setScope($injectableAnnotation->getScope());
         }
         if ($injectableAnnotation->isLazy() !== null) {
             $definition->setLazy($injectableAnnotation->isLazy());
         }
     }
     // Browse the class properties looking for annotated properties
     $this->readProperties($class, $definition);
     // Browse the object's methods looking for annotated methods
     $this->readMethods($class, $definition);
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     return $definition;
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function merge(MergeableDefinition $definition)
 {
     if (!$definition instanceof ClassDefinition) {
         throw new DefinitionException("DI definition conflict: there are 2 different definitions for '" . $this->getName() . "' that are incompatible, they are not of the same type");
     }
     $newDefinition = clone $this;
     // The current prevails
     if ($newDefinition->className === null) {
         $newDefinition->className = $definition->className;
     }
     if ($newDefinition->scope === null) {
         $newDefinition->scope = $definition->scope;
     }
     if ($newDefinition->lazy === null) {
         $newDefinition->lazy = $definition->lazy;
     }
     // Merge constructor injection
     if ($definition->getConstructorInjection() !== null) {
         if ($newDefinition->constructorInjection !== null) {
             // Merge
             $newDefinition->constructorInjection->merge($definition->getConstructorInjection());
         } else {
             // Set
             $newDefinition->constructorInjection = $definition->getConstructorInjection();
         }
     }
     // Merge property injections
     foreach ($definition->getPropertyInjections() as $propertyName => $propertyInjection) {
         if (!array_key_exists($propertyName, $newDefinition->propertyInjections)) {
             // Add
             $newDefinition->propertyInjections[$propertyName] = $propertyInjection;
         }
     }
     // Merge method injections
     foreach ($definition->getMethodInjections() as $methodName => $methodInjection) {
         if (array_key_exists($methodName, $newDefinition->methodInjections)) {
             // Merge
             $newDefinition->methodInjections[$methodName]->merge($methodInjection);
         } else {
             // Add
             $newDefinition->methodInjections[$methodName] = $methodInjection;
         }
     }
     return $newDefinition;
 }