Skip to content
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

Closed
XedinUnknown opened this issue Mar 30, 2020 · 7 comments · Fixed by #13
Closed

Detect Recursive References #10

XedinUnknown opened this issue Mar 30, 2020 · 7 comments · Fixed by #13
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@XedinUnknown
Copy link
Member

XedinUnknown commented Mar 30, 2020

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.

@XedinUnknown XedinUnknown added the enhancement New feature or request label Mar 30, 2020
@XedinUnknown XedinUnknown added this to the 0.1 milestone Mar 30, 2020
@mecha
Copy link
Member

mecha commented Mar 30, 2020

What a coincidence, I literally implemented this detection last Friday. I can share my solution if you want.

@XedinUnknown
Copy link
Member Author

I would love that!

@mecha
Copy link
Member

mecha commented Mar 30, 2020

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);
}

@XedinUnknown
Copy link
Member Author

I, uh, thought that this would come in the form of a PR, to be honest :D

@mecha
Copy link
Member

mecha commented Apr 9, 2020

I'll get around it eventually.

@mecha
Copy link
Member

mecha commented Sep 15, 2020

I'll get around it eventually.

... aaaaaand I never did

mecha added a commit that referenced this issue Sep 15, 2020
@mecha mecha linked a pull request Sep 15, 2020 that will close this issue
@mecha
Copy link
Member

mecha commented Sep 15, 2020

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
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants