Skip to content

Commit

Permalink
Ensure a string is returned if defined and happens to be a non existe…
Browse files Browse the repository at this point in the history
…nt class name
  • Loading branch information
philipobenito committed Mar 13, 2024
1 parent 56f6f3f commit ff34631
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 6 additions & 7 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
protected $resolved;

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

/**
* @param string $id
Expand Down Expand Up @@ -192,16 +192,15 @@ public function resolveNew()
}

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

Expand Down
4 changes: 1 addition & 3 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ public function testNonExistentClassCausesException(): void
$container->add(NonExistent::class);

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

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

0 comments on commit ff34631

Please sign in to comment.