-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compiler circular dependency detection (switch to DFS).
- Loading branch information
1 parent
fe0b73a
commit 3167c5b
Showing
4 changed files
with
120 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Norvica\Container; | ||
|
||
use Norvica\Container\Exception\CircularDependencyException; | ||
use Norvica\Container\Exception\ContainerException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Visitor | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private array $visiting = []; | ||
|
||
public function enter(string $id): void | ||
{ | ||
if ($this->visiting($id)) { | ||
throw new CircularDependencyException( | ||
sprintf( | ||
"Circular dependency detected when resolving the following chain: '%s' → '{$id}'.", | ||
implode("' → '", $this->visiting), | ||
) | ||
); | ||
} | ||
|
||
$this->visiting[] = $id; | ||
} | ||
|
||
public function exit(string $id): void | ||
{ | ||
$index = array_search($id, $this->visiting, true); | ||
if ($index === false) { | ||
throw new ContainerException("Tried to exit node '{$id}' that hasn't been entered."); | ||
} | ||
|
||
unset($this->visiting[$index]); | ||
} | ||
|
||
private function visiting(string $id): bool | ||
{ | ||
return in_array($id, $this->visiting, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters