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; }
/** * {@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; }
/** * 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; }
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())); } }
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()); } }
private function mergePropertyInjections(ObjectDefinition $definition) { foreach ($definition->getPropertyInjections() as $propertyName => $propertyInjection) { if (!array_key_exists($propertyName, $this->propertyInjections)) { // Add $this->propertyInjections[$propertyName] = $propertyInjection; } } }
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(); } } }
/** * 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; }
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())); } }
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())); } }