/**
  * {@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;
 }
示例#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;
 }
 private function readInjectableAnnotation(ReflectionClass $class, ClassDefinition $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());
     }
 }