'Entry "%s" cannot be resolved: the class is not instantiable', $definition->getName() )); } $classname = $definition->getClassName(); $classReflection = new ReflectionClass($classname); $constructorInjection = $definition->getConstructorInjection(); try { $args = $this->parameterResolver->resolveParameters( $constructorInjection, $classReflection->getConstructor(), $parameters ); $object = new $classname(...$args); $this->injectMethodsAndProperties($object, $definition); } catch (NotFoundExceptionInterface $e) { throw new DependencyException(sprintf( 'Error while injecting dependencies into %s: %s', $classReflection->getName(), $e->getMessage() ), 0, $e); } catch (InvalidDefinition $e) { throw InvalidDefinition::create($definition, sprintf( 'Entry "%s" cannot be resolved: %s', $definition->getName(), $e->getMessage() )); } return $object; } protected function injectMethodsAndProperties($object, ObjectDefinition $objectDefinition) { // Property injections foreach ($objectDefinition->getPropertyInjections() as $propertyInjection) { $this->injectProperty($object, $propertyInjection); } // Method injections foreach ($objectDefinition->getMethodInjections() as $methodInjection) { $methodReflection = new \ReflectionMethod($object, $methodInjection->getMethodName()); $args = $this->parameterResolver->resolveParameters($methodInjection, $methodReflection); $methodReflection->invokeArgs($object, $args); } } /** * Inject dependencies into properties. * * @param object $object Object to inject dependencies into * @param PropertyInjection $propertyInjection Property injection definition * * @throws DependencyException * @throws InvalidDefinition */ private function injectProperty($object, PropertyInjection $propertyInjection) { $propertyName = $propertyInjection->getPropertyName(); $value = $propertyInjection->getValue(); if ($value instanceof Definition) { try { $value = $this->definitionResolver->resolve($value); } catch (DependencyException $e) { throw $e; } catch (Exception $e) { throw new DependencyException(sprintf( 'Error while injecting in %s::%s. %s', get_class($object), $propertyName, $e->getMessage() ), 0, $e); } } self::setPrivatePropertyValue($propertyInjection->getClassName(), $object, $propertyName, $value); } public static function setPrivatePropertyValue(string $className = null, $object, string $propertyName, $propertyValue) { $className = $className ?: get_class($object); $property = new ReflectionProperty($className, $propertyName); if (! $property->isPublic()) { $property->setAccessible(true); } $property->setValue($object, $propertyValue); } }