From f14ecacd0f5e86590c67b7f05312ec7ef3f3437d Mon Sep 17 00:00:00 2001 From: yiiliveext Date: Tue, 21 Sep 2021 08:55:20 +0300 Subject: [PATCH] Move dependencies cache from ArrayDefinitionBuilder to DefinitionExtractor (#12) --- src/Infrastructure/ArrayDefinitionBuilder.php | 28 +------------------ src/Infrastructure/DefinitionExtractor.php | 14 +++++++++- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/Infrastructure/ArrayDefinitionBuilder.php b/src/Infrastructure/ArrayDefinitionBuilder.php index 826cf32..2792c6c 100644 --- a/src/Infrastructure/ArrayDefinitionBuilder.php +++ b/src/Infrastructure/ArrayDefinitionBuilder.php @@ -23,11 +23,6 @@ final class ArrayDefinitionBuilder { private static ?self $instance = null; - /** - * @psalm-var array> - */ - private static array $dependencies = []; - private function __construct() { } @@ -49,7 +44,7 @@ public static function getInstance(): self public function build(ContainerInterface $container, ?ContainerInterface $referenceContainer, ArrayDefinition $definition): object { $class = $definition->getClass(); - $dependencies = $this->getDependencies($class); + $dependencies = DefinitionExtractor::getInstance()->fromClassName($class); $constructorArguments = $definition->getConstructorArguments(); $this->injectArguments($dependencies, $constructorArguments); @@ -143,25 +138,4 @@ private function isIntegerIndexed(array $arguments): bool return $hasIntegerIndex; } - - /** - * Returns the dependencies of the specified class. - * - * @param class-string $class Class name or interface name. - * - * @throws NotInstantiableException - * @throws NotFoundException - * @throws NotInstantiableException - * - * @return DefinitionInterface[] The dependencies of the specified class. - * @psalm-return array - */ - private function getDependencies(string $class): array - { - if (!isset(self::$dependencies[$class])) { - self::$dependencies[$class] = DefinitionExtractor::getInstance()->fromClassName($class); - } - - return self::$dependencies[$class]; - } } diff --git a/src/Infrastructure/DefinitionExtractor.php b/src/Infrastructure/DefinitionExtractor.php index d4a0af0..905254e 100644 --- a/src/Infrastructure/DefinitionExtractor.php +++ b/src/Infrastructure/DefinitionExtractor.php @@ -27,6 +27,11 @@ final class DefinitionExtractor { private static ?self $instance = null; + /** + * @psalm-var array> + */ + private static array $dependencies = []; + private function __construct() { } @@ -51,6 +56,10 @@ public static function getInstance(): self */ public function fromClassName(string $class): array { + if (isset(self::$dependencies[$class])) { + return self::$dependencies[$class]; + } + try { $reflectionClass = new ReflectionClass($class); } catch (ReflectionException $e) { @@ -62,7 +71,10 @@ public function fromClassName(string $class): array } $constructor = $reflectionClass->getConstructor(); - return $constructor === null ? [] : $this->fromFunction($constructor); + $dependencies = $constructor === null ? [] : $this->fromFunction($constructor); + self::$dependencies[$class] = $dependencies; + + return $dependencies; } /**