Skip to content

Fix erroneous order of events for multi events per listener #1885

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

Open
wants to merge 1 commit into
base: 2.14.x
Choose a base branch
from

Conversation

ostrolucky
Copy link
Member

Fixes #1884

$id = $reference->__toString();
foreach ($container->getDefinition($id)->getTag('doctrine.orm.entity_listener') as $attributes) {
foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener') as $id => $tags) {
usort($tags, static fn (array $a, array $b) => ($a['priority'] ?? 0) <=> ($b['priority'] ?? 0));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be

Suggested change
usort($tags, static fn (array $a, array $b) => ($a['priority'] ?? 0) <=> ($b['priority'] ?? 0));
usort($tags, static fn (array $a, array $b) => ($b['priority'] ?? 0) <=> ($a['priority'] ?? 0));

i.e. tags with a larger number as priority come first!?

see PriorityTaggedServiceTrait#L113

foreach ($resolvers as $reference) {
$id = $reference->__toString();
foreach ($container->getDefinition($id)->getTag('doctrine.orm.entity_listener') as $attributes) {
foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener') as $id => $tags) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next line will sort the tags within the service by priority but the services are now simply in the order they are returned by $container->findTaggedServiceIds(), i.e., in the order they were defined.
But for the listeners to be registered in the correct order the tags of all services need to be ordered together.

This probably needs to restructure the code to a single foreach over all tags of all returned services?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So something like this:

        $serviceTags = [];
        foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener') as $id => $tags) {
            foreach ($tags as $attributes) {
                $serviceTags[] = [
                    'serviceId' => $id,
                    'attributes' => $attributes,
                ];
            }
        }

        usort($serviceTags, static fn (array $a, array $b) => ($b['attributes']['priority'] ?? 0) <=> ($a['attributes']['priority'] ?? 0));

        foreach ($serviceTags as $tag) {
            $id            = $tag['serviceId'];
            $attributes    = $tag['attributes'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[EntityListener] with multiple listeners per class only the first priority is used
2 participants