-
Notifications
You must be signed in to change notification settings - Fork 3
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
Detect Recursive References #10
Comments
What a coincidence, I literally implemented this detection last Friday. I can share my solution if you want. |
I would love that! |
public function get($id)
{
static $fetching = [];
if (isset($fetching[$id])) {
$trace = $this->getFetchTrace($fetching);
throw new LogicException(
"Circular dependency detected for \"${id}\":\n{$trace}"
);
}
$fetching[$id] = true;
try {
return $this->actualGet($id);
} catch (ContainerExceptionInterface $exception) {
throw $exception;
} finally {
unset($fetching[$id]);
}
}
protected function getFetchTrace(array $fetched)
{
$keys = array_reverse(array_keys($fetched));
$i = 0;
$trace = array_map(function ($id) use (&$i) {
$i++;
return "required by: {$id}";
}, $keys);
return implode("\n", $trace);
} |
I, uh, thought that this would come in the form of a PR, to be honest :D |
I'll get around it eventually. |
... aaaaaand I never did |
Until now 😉 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Problem
It can happen that during resolution of a service, its own definition gets invoked. In these cases, it can become quite hard to debug by looking at just a stack trace.
Suggested Solution
Implement recursion detection, such that an exception is thrown if a service definition invokes itself, directly or not.
The text was updated successfully, but these errors were encountered: