container = $container; } /** * Creates an instance by a class name. */ public function create(string $className) : object { return $this->createByClassName($className); } /** * Creates an instance by a class name. Allows passing specific constructor parameters. * Defined in an associative array. A key should match the parameter name. */ public function createWith(string $className, array $with = []) : object { return $this->createByClassName($className, $with); } /** * @deprecated Use create or createWith methods instead. Left public for backward compatibility. * @todo Make protected. */ public function createByClassName(string $className, ?array $with = null) : object { if (!class_exists($className)) { throw new Error("InjectableFactory: Class '{$className}' does not exist."); } $class = new ReflectionClass($className); $injectionList = $this->getConstructorInjectionList($class, $with); $obj = $class->newInstanceArgs($injectionList); // @todo Remove in 6.4. if ($class->implementsInterface(Injectable::class)) { $this->applyInjectable($class, $obj); return $obj; } $this->applyAwareInjections($class, $obj); return $obj; } /** * @deprecated * @todo Remove in 6.4. */ protected function applyInjectable(ReflectionClass $class, object $obj) { $setList = []; $dependencyList = $obj->getDependencyList(); foreach ($dependencyList as $name) { $injection = $this->container->get($name); if ($this->classHasDependencySetter($class, $name)) { $methodName = 'set' . ucfirst($name); $obj->$methodName($injection); $setList[] = $name; } $obj->inject($name, $injection); } $this->applyAwareInjections($class, $obj, $setList); return $obj; } protected function getConstructorInjectionList(ReflectionClass $class, ?array $with = null) : array { $injectionList = []; $constructor = $class->getConstructor(); if (!$constructor) { return $injectionList; } $params = $constructor->getParameters(); foreach ($params as $param) { $injectionList[] = $this->getConstructorParamInjection($class, $param, $with); } return $injectionList; } protected function getConstructorParamInjection(ReflectionClass $class, ReflectionParameter $param, ?array $with) { $name = $param->getName(); if ($with && array_key_exists($name, $with)) { return $with[$name]; } $dependencyClass = null; if ($param->getType()) { try { $dependencyClass = $param->getClass(); } catch (Throwable $e) { $badClassName = $param->getType()->getName(); // This trick allows to log syntax errors. class_exists($badClassName); throw new Error("InjectableFactory: " . $e->getMessage()); } } if (!$dependencyClass && $param->isDefaultValueAvailable()) { return $param->getDefaultValue(); } if ( $dependencyClass && $this->container->has($name) && $this->areDependencyClassesMatching($dependencyClass, $this->container->getClass($name)) ) { return $this->container->get($name); } if ($dependencyClass && $param->allowsNull()) { return null; } if ($dependencyClass) { return $this->create($dependencyClass->getName()); } $className = $class->getName(); throw new Error("InjectableFactory: Could not create {$className}, dependency '{$name}' not found."); } protected function areDependencyClassesMatching(ReflectionClass $paramHintClass, ReflectionClass $returnHintClass) : bool { if ($paramHintClass->getName() === $returnHintClass->getName()) { return true; } if ($returnHintClass->isSubclassOf($paramHintClass)) { return true; } return false; } protected function applyAwareInjections(ReflectionClass $class, object $obj, array $ignoreList = []) { foreach ($class->getInterfaces() as $interface) { $interfaceName = $interface->getShortName(); if (substr($interfaceName, -5) !== 'Aware' || strlen($interfaceName) <= 5) { continue; } $name = lcfirst(substr($interfaceName, 0, -5)); if (in_array($name, $ignoreList)) { continue; } if (!$this->classHasDependencySetter($class, $name, true)) { continue; } $injection = $this->container->get($name); $methodName = 'set' . ucfirst($name); $obj->$methodName($injection); } } protected function classHasDependencySetter(ReflectionClass $class, string $name, bool $skipInstanceCheck = false) : bool { $methodName = 'set' . ucfirst($name); if (!$class->hasMethod($methodName) || !$class->getMethod($methodName)->isPublic()) { return false; } $params = $class->getMethod($methodName)->getParameters(); if (!$params || !count($params)) { return false; } $injection = $this->container->get($name); $paramClass = $params[0]->getClass(); if ($skipInstanceCheck || $paramClass && $paramClass->isInstance($injection)) { return true; } return false; } }