diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 9897cf209a..4e429a73fc 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -30,7 +30,6 @@ namespace Espo\Core; use Espo\Core\InjectableFactory; -use Espo\Entities\User; use Espo\Core\Exceptions\Error; @@ -57,7 +56,7 @@ class Container */ public function get(string $name) : object { - if (!array_key_exists($name, $this->data)) { + if (!isset($this->data[$name])) { $this->load($name); } @@ -69,13 +68,23 @@ class Container */ public function has(string $name) : bool { - if (isset($this->data[$name])) return true; + if (isset($this->data[$name])) { + return true; + } $loadMethodName = 'load' . ucfirst($name); - if (method_exists($this, $loadMethodName)) return true; - if ($this->configuration->getLoaderClassName($name)) return true; - if ($this->configuration->getServiceClassName($name)) return true; + if (method_exists($this, $loadMethodName)) { + return true; + } + + if ($this->configuration->getLoaderClassName($name)) { + return true; + } + + if ($this->configuration->getServiceClassName($name)) { + return true; + } return false; } @@ -109,6 +118,7 @@ class Container if ($loaderClassName) { $loadClass = $this->get('injectableFactory')->create($loaderClassName); + $this->data[$name] = $loadClass->load(); return; } @@ -116,7 +126,7 @@ class Container $className = $this->configuration->getServiceClassName($name); if (!$className || !class_exists($className)) { - throw new Error("Could not load {$name} service."); + throw new Error("Could not load '{$name}' service."); } $dependencyList = $this->configuration->getServiceDependencyList($name); @@ -126,7 +136,9 @@ class Container foreach ($dependencyList as $item) { $dependencyObjectList[] = $this->get($item); } + $reflector = new \ReflectionClass($className); + $this->data[$name] = $reflector->newInstanceArgs($dependencyObjectList); return; } diff --git a/application/Espo/Core/InjectableFactory.php b/application/Espo/Core/InjectableFactory.php index bed436854a..e1d7ad9b30 100644 --- a/application/Espo/Core/InjectableFactory.php +++ b/application/Espo/Core/InjectableFactory.php @@ -30,9 +30,11 @@ namespace Espo\Core; use Espo\Core\Exceptions\Error; +use Throwable; + +use Espo\Core\Interfaces\Injectable; use ReflectionClass; -use Espo\Core\Interfaces\Injectable; /** * Creates an instance by a class name. Uses constructor param names to detect which @@ -113,45 +115,48 @@ class InjectableFactory $injectionList = []; $constructor = $class->getConstructor(); - if ($constructor) { - $params = $constructor->getParameters(); - foreach ($params as $param) { - $name = $param->getName(); + if (!$constructor) { + return $injectionList; + } - if ($with && array_key_exists($name, $with)) { - $injection = $with[$name]; - } else { - $dependencyClassName = null; + $params = $constructor->getParameters(); - if ($param->getType()) { - try { - $dependencyClassName = $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()); - } - } + foreach ($params as $param) { + $name = $param->getName(); - if (!$dependencyClassName) { - if ($param->isDefaultValueAvailable()) { - $injectionList[] = $param->getDefaultValue(); - continue; - } - } + if ($with && array_key_exists($name, $with)) { + $injection = $with[$name]; + } else { + $dependencyClassName = null; - $injection = $this->container->get($name); - - if (!$injection) { - $className = $class->getName(); - throw new Error("InjectableFactory: Could not create {$className}, dependency '{$name}' not found."); + if ($param->getType()) { + try { + $dependencyClassName = $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()); } } - $injectionList[] = $injection; + if (!$dependencyClassName) { + if ($param->isDefaultValueAvailable()) { + $injectionList[] = $param->getDefaultValue(); + continue; + } + } + + $injection = $this->container->get($name); + + if (!$injection) { + $className = $class->getName(); + throw new Error("InjectableFactory: Could not create {$className}, dependency '{$name}' not found."); + } } + + $injectionList[] = $injection; } return $injectionList; @@ -162,13 +167,19 @@ class InjectableFactory foreach ($class->getInterfaces() as $interface) { $interfaceName = $interface->getShortName(); - if (substr($interfaceName, -5) !== 'Aware' || strlen($interfaceName) <= 5) continue; + if (substr($interfaceName, -5) !== 'Aware' || strlen($interfaceName) <= 5) { + continue; + } $name = lcfirst(substr($interfaceName, 0, -5)); - if (in_array($name, $ignoreList)) continue; + if (in_array($name, $ignoreList)) { + continue; + } - if (!$this->classHasDependencySetter($class, $name, true)) continue; + if (!$this->classHasDependencySetter($class, $name, true)) { + continue; + } $injection = $this->container->get($name);