Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Conversation

Ocramius
Copy link
Member

Fixes #83

…ait`, as it is common to `ServiceManager` and `AbstractPluginManager`
… loops by skipping alias resolution for already visited aliases
@weierophinney
Copy link
Member

Nice, clean solution. Would be nice if we hit a cyclic structure, but just being told it cannot resolve is reasonable, I think. 👍

@stof
Copy link

stof commented Jan 26, 2016

you could detect it: when exiting the loop, check whether the final name appears in $this->aliases. If yes, it means there was a cycle for this alias.

@Ocramius
Copy link
Member Author

I provided a much more comprehensive exception, which is thrown at ServiceManager::__construct() and has some decent error messages (debugging cycles is no fun!).

Hope that clears up all this :-)

@stof
Copy link

stof commented Jan 26, 2016

Should you really detect all cycles in the alias map when building the exception, or only the cycle being found at this point ? Your current logic means that each cycle is duplicated many times (the number of times is equal to the length of the cycle), making the user think there is 4 cycles in the map while there is actually only 1 (involving 4 items)

@Ocramius
Copy link
Member Author

Your current logic means that each cycle is duplicated many times (the number of times is equal to the length of the cycle), making the user think there is 4 cycles in the map while there is actually only 1 (involving 4 items)

Agreed, but I wanted to show all possible entry points. De-duplicating those is another piece of work.

In general, my though-flow is following:

  • quick servicemanager runtime
  • slow exception building (performance doesn't matter here), just show everything and provide possibly meaningful information

@stof
Copy link

stof commented Jan 26, 2016

@Ocramius you could display only the cycle involving the service detected by the runtime IMO. Then, people would be able to fix cycles one after the other. Displaying the same cycle many times (letting the user determine that it is actually the same as the display is different and may not even be grouped together) makes debugging harder IMO.
An alternative would be to display each cycle only once, but removing cycle members from the list of remaining entry points when finding a cycle (so that the cycle is not found again starting at a different place in the cycle)

@Ocramius
Copy link
Member Author

@stof I simply de-duplicated the cycles (see latest commits). Less confusing now, IMO :-)

@kynx
Copy link
Contributor

kynx commented Jan 27, 2016

I'd love to see the use case for aliasing deeper then 100 :)

@kynx
Copy link
Contributor

kynx commented Jan 27, 2016

How's about:

    /**
     * Resolve all aliases to their canonical service names.
     */
    private function resolveAliases(array $aliases)
    {
        foreach ($aliases as $alias => $service) {
            $name = $alias;

            $count = 0;
            while (isset($this->aliases[$name]) && $count < 100) {
                $name = $this->aliases[$name];
                $count++;
            }

            if ($count > 99) {
                throw new ExceptionalIdiotlException("This is nuts. Find another library.")
            }

            $this->resolvedAliases[$alias] = $name;
        }
    }

@Ocramius
Copy link
Member Author

@kynx correct solutions over approximate ones, IMO

@kynx
Copy link
Contributor

kynx commented Jan 27, 2016

@Ocramius yeah, but please, please can't we squeak the ExceptionalIdiotExecption into this one? The world needs it ;)

@Ocramius
Copy link
Member Author

Would love to, but it would violate some sort of yet to be invented CoC ;-)

@asgrim
Copy link
Contributor

asgrim commented Jan 27, 2016

LGTM 👍

@weierophinney weierophinney self-assigned this Jan 27, 2016
@weierophinney
Copy link
Member

Really nice work, @Ocramius !

@weierophinney weierophinney merged commit a27bafd into zendframework:master Feb 1, 2016
weierophinney added a commit that referenced this pull request Feb 1, 2016
…n-lead-to-infinite-loops

Fix for #83: misconfigured aliases can lead to infinite loops
weierophinney added a commit that referenced this pull request Feb 1, 2016
weierophinney added a commit that referenced this pull request Feb 1, 2016
weierophinney added a commit that referenced this pull request Feb 1, 2016
@Ocramius Ocramius deleted the fix/#83-misconfigured-aliases-can-lead-to-infinite-loops branch February 1, 2016 22:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants