Skip to content

Commit

Permalink
Merge pull request #254 from arai-ta/4.x
Browse files Browse the repository at this point in the history
Fix non-existent class causing infinite loop
  • Loading branch information
philipobenito authored Mar 13, 2024
2 parents 6a917ac + e7c72a1 commit 1609119
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
};
use League\Container\ContainerAwareTrait;
use League\Container\Exception\ContainerException;
use League\Container\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use ReflectionClass;

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

/**
* @var bool
*/
protected $isAlreadySearched = false;

/**
* @param string $id
* @param mixed|null $concrete
Expand Down Expand Up @@ -185,9 +191,17 @@ public function resolveNew()
$container = null;
}

// stop recursive resolving
if ($this->isAlreadySearched) {
throw new NotFoundException(
sprintf('Alias or class "%s" did not found.', $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->isAlreadySearched = true;
$concrete = $container->get($concrete);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,15 @@ public function testContainerAwareCannotBeUsedWithoutImplementingInterface(): vo
$container = $this->getMockBuilder(Container::class)->getMock();
$class->setContainer($container);
}

public function testNonExistentClassCausesException(): void
{
$container = new Container();
$container->add(NonExistent::class);

self::assertTrue($container->has(NonExistent::class));

$this->expectException(NotFoundException::class);
$container->get(NonExistent::class);
}
}

0 comments on commit 1609119

Please sign in to comment.