From 93aa3332c73e6b55e21b834584f3c455756290f1 Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Fri, 12 Apr 2024 23:17:11 -0400 Subject: [PATCH] non-class alias/concrete returned as is while preserving recursive lookup --- src/Definition/Definition.php | 21 ++++++--------------- tests/ContainerTest.php | 12 +++++++----- tests/Definition/DefinitionTest.php | 9 +++++++++ 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Definition/Definition.php b/src/Definition/Definition.php index 44e4157..fbe5227 100644 --- a/src/Definition/Definition.php +++ b/src/Definition/Definition.php @@ -12,7 +12,6 @@ }; use League\Container\ContainerAwareTrait; use League\Container\Exception\ContainerException; -use League\Container\Exception\NotFoundException; use Psr\Container\ContainerInterface; use ReflectionClass; @@ -56,11 +55,6 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface */ protected $resolved; - /** - * @var array - */ - protected $recursiveCheck = []; - /** * @param string $id * @param mixed|null $concrete @@ -177,8 +171,12 @@ public function resolveNew() $concrete = $concrete->getValue(); } - if (is_string($concrete) && class_exists($concrete)) { - $concrete = $this->resolveClass($concrete); + if (is_string($concrete)) { + if (class_exists($concrete)) { + $concrete = $this->resolveClass($concrete); + } elseif ($this->getAlias() === $concrete) { + return $concrete; + } } if (is_object($concrete)) { @@ -191,16 +189,9 @@ public function resolveNew() $container = null; } - // stop recursive resolving - if (is_string($concrete) && in_array($concrete, $this->recursiveCheck)) { - $this->resolved = $concrete; - return $concrete; - } - // if we still have a string, try to pull it from the container // this allows for `alias -> alias -> ... -> concrete if (is_string($concrete) && $container instanceof ContainerInterface && $container->has($concrete)) { - $this->recursiveCheck[] = $concrete; $concrete = $container->get($concrete); } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 0e99971..776aee8 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -214,7 +214,8 @@ public function testContainerAwareCannotBeUsedWithoutImplementingInterface(): vo { $this->expectException(BadMethodCallException::class); - $class = new class { + $class = new class + { use ContainerAwareTrait; }; @@ -222,12 +223,13 @@ public function testContainerAwareCannotBeUsedWithoutImplementingInterface(): vo $class->setContainer($container); } - public function testNonExistentClassCausesException(): void + public function testNonExistentClassIsReturnedAsIdenticalString(): void { + $nonExistent = NonExistent::class; $container = new Container(); - $container->add(NonExistent::class); + $container->add($nonExistent); - self::assertTrue($container->has(NonExistent::class)); - self::assertSame(NonExistent::class, $container->get(NonExistent::class)); + self::assertTrue($container->has($nonExistent)); + self::assertSame($nonExistent, $container->get($nonExistent)); } } diff --git a/tests/Definition/DefinitionTest.php b/tests/Definition/DefinitionTest.php index d284218..33591de 100644 --- a/tests/Definition/DefinitionTest.php +++ b/tests/Definition/DefinitionTest.php @@ -136,4 +136,13 @@ public function testDefinitionCanSetConcrete(): void $definition->setConcrete($concrete); self::assertSame($concrete, $definition->getConcrete()); } + + public function testNonExistentClassIsReturnedAsIdenticalString(): void + { + $nonExistent = NonExistent::class; + $definition = new Definition($nonExistent); + + self::assertSame($nonExistent, $definition->getAlias()); + self::assertSame($nonExistent, $definition->resolve()); + } }