Skip to content

Commit

Permalink
Resolution stack now instance-specific
Browse files Browse the repository at this point in the history
This prevents other unrelated delegating containers from manipulating the stack.
  • Loading branch information
XedinUnknown committed Sep 21, 2024
1 parent 414e3d1 commit 92c5bfd
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/DelegatingContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class DelegatingContainer implements ContainerInterface
*/
protected $parent;

/**
* Keys represent the list of service names accessed recursively, in order of access
* @var array<string, true>
*/
protected $stack = [];

/**
*/
public function __construct(ServiceProviderInterface $provider, PsrContainerInterface $parent = null)
Expand All @@ -39,10 +45,8 @@ public function __construct(ServiceProviderInterface $provider, PsrContainerInte
*/
public function get($id)
{
static $stack = [];

if (array_key_exists($id, $stack)) {
$trace = implode(' -> ', array_keys($stack)) . ' -> ' . $id;
if (array_key_exists($id, $this->stack)) {
$trace = implode(' -> ', array_keys($this->stack)) . ' -> ' . $id;

throw new ContainerException(
$this->__("Circular dependency detected:\n%s", [$trace]),
Expand All @@ -51,12 +55,12 @@ public function get($id)
);
}

$stack[$id] = true;
$this->stack[$id] = true;

try {
return $this->createService($id);
} finally {
unset($stack[$id]);
unset($this->stack[$id]);
}
}

Expand Down

0 comments on commit 92c5bfd

Please sign in to comment.