Skip to content

Commit

Permalink
Move dependencies cache from ArrayDefinitionBuilder to DefinitionExtr…
Browse files Browse the repository at this point in the history
…actor (#12)
  • Loading branch information
yiiliveext committed Sep 21, 2021
1 parent 0ab9295 commit f14ecac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
28 changes: 1 addition & 27 deletions src/Infrastructure/ArrayDefinitionBuilder.php
Expand Up @@ -23,11 +23,6 @@ final class ArrayDefinitionBuilder
{
private static ?self $instance = null;

/**
* @psalm-var array<string, array<string, DefinitionInterface>>
*/
private static array $dependencies = [];

private function __construct()
{
}
Expand All @@ -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);
Expand Down Expand Up @@ -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<string, DefinitionInterface>
*/
private function getDependencies(string $class): array
{
if (!isset(self::$dependencies[$class])) {
self::$dependencies[$class] = DefinitionExtractor::getInstance()->fromClassName($class);
}

return self::$dependencies[$class];
}
}
14 changes: 13 additions & 1 deletion src/Infrastructure/DefinitionExtractor.php
Expand Up @@ -27,6 +27,11 @@ final class DefinitionExtractor
{
private static ?self $instance = null;

/**
* @psalm-var array<string, array<string, DefinitionInterface>>
*/
private static array $dependencies = [];

private function __construct()
{
}
Expand All @@ -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) {
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit f14ecac

Please sign in to comment.