Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore ability to perform recursive definition search #259

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
use League\Container\ContainerAwareTrait;
use League\Container\Exception\ContainerException;
use League\Container\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use ReflectionClass;

Expand Down Expand Up @@ -56,11 +55,6 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
*/
protected $resolved;

/**
* @var array
*/
protected $recursiveCheck = [];

/**
* @param string $id
* @param mixed|null $concrete
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}

Expand Down
12 changes: 7 additions & 5 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,22 @@ public function testContainerAwareCannotBeUsedWithoutImplementingInterface(): vo
{
$this->expectException(BadMethodCallException::class);

$class = new class {
$class = new class
{
use ContainerAwareTrait;
};

$container = $this->getMockBuilder(Container::class)->getMock();
$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));
}
}
9 changes: 9 additions & 0 deletions tests/Definition/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}