Exemple #1
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;
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinition($entryName)
 {
     $definition = new ObjectDefinition($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 => $calls) {
             foreach ($calls as $parameters) {
                 $parameters = $this->fixParameters($definition, $method, $parameters);
                 $methodInjection = new MethodInjection($method, $parameters);
                 $definition->addMethodInjection($methodInjection);
             }
         }
     }
     return $definition;
 }
Exemple #3
0
 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() : [];
     // @Inject on constructor is implicit
     if (!($annotation || $method->isConstructor())) {
         return null;
     }
     $parameters = [];
     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);
     }
 }