/** * {@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; }
/** * {@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; }
/** * {@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); } }