-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Missing priority for events in doctrine bridge #21977
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
Comments
You are right: we allow to define priorities everywhere ... so I guess it should be OK to add priorities here too. 👍 from me! |
Doctrine is using its own EventManager: https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/EventManager.php This one doesn't have the priority feature, so it's not something we can add in Symfony. It should first be added in the Doctrine EventManager. |
@chapterjason after @wouterj's comment, I'm afraid we need to close this as "can't fix" because it depends on Doctrine project. If you still want this feature, please open an issue in https://github.com/doctrine/common/issues Thanks! |
Aren't the listeners and subscribers already registered in the correct order when specifying priorities on the tag? |
Thank you @dmaicher I found out that https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php#L91 does not take care of priorities what makes no sense cause the priorities will sorted later in https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php#L95 My prove:
My result:
|
@javiereguiluz we have to re-open this unfortunately 😢 I double checked and there is indeed something broken with handling the priorities. This only ever worked correctly if there is exactly one tag per service as otherwise the priorities are lost or handled incorrectly. Thanks @chapterjason for debugging it 👍 Test case that we have for it currently: public function testProcessEventListenersWithPriorities()
{
$container = $this->createBuilder();
$container
->register('a', 'stdClass')
->addTag('doctrine.event_listener', array(
'event' => 'foo',
'priority' => -5,
))
->addTag('doctrine.event_listener', array(
'event' => 'bar',
))
;
$container
->register('b', 'stdClass')
->addTag('doctrine.event_listener', array(
'event' => 'foo',
))
;
$this->process($container);
$this->assertEquals(array('b', 'a'), $this->getServiceOrder($container, 'addEventListener'));
$calls = $container->getDefinition('doctrine.dbal.default_connection.event_manager')->getMethodCalls();
$this->assertEquals(array('foo', 'bar'), $calls[1][1][0]);
} If we switch the tag order for service a like $container
->register('a', 'stdClass')
->addTag('doctrine.event_listener', array(
'event' => 'bar',
))
->addTag('doctrine.event_listener', array(
'event' => 'foo',
'priority' => -5,
))
; Then the tests suddenly fail as the priorities are lost: There was 1 failure:
1) Symfony\Bridge\Doctrine\Tests\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPassTest::testProcessEventListenersWithPriorities
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => 'foo'
- 1 => 'bar'
+ 0 => 'bar'
+ 1 => 'foo'
) |
@chapterjason I proposed a fix here: #22001 |
…s (dmaicher) This PR was merged into the 2.7 branch. Discussion ---------- [Doctrine Bridge] fix priority for doctrine event listeners | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21977 | License | MIT | Doc PR | - This fixes handling the priorities for doctrine event listeners. As found out by @chapterjason in #21977 the priority was incorrectly handled as soon as a listener had more than one tag (so listening to multiple events). With this changes all tagged listeners are globally sorted by priority (using the same stable sort approach as in the later available `PriorityTaggedServiceTrait`) and then added one by one to the event manager. I also updated the tests a bit as it was not covering all cases. We also have to extend the docs for it I think as it does not mention the `priority` and `lazy` option at all? http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html Commits ------- 9d9d4ef [Doctrine Bridge] fix priority for doctrine event listeners
…s (dmaicher) This PR was merged into the 2.7 branch. Discussion ---------- [Doctrine Bridge] fix priority for doctrine event listeners | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21977 | License | MIT | Doc PR | - This fixes handling the priorities for doctrine event listeners. As found out by @chapterjason in symfony/symfony#21977 the priority was incorrectly handled as soon as a listener had more than one tag (so listening to multiple events). With this changes all tagged listeners are globally sorted by priority (using the same stable sort approach as in the later available `PriorityTaggedServiceTrait`) and then added one by one to the event manager. I also updated the tests a bit as it was not covering all cases. We also have to extend the docs for it I think as it does not mention the `priority` and `lazy` option at all? http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html Commits ------- 9d9d4efb88 [Doctrine Bridge] fix priority for doctrine event listeners
…s (dmaicher) This PR was merged into the 3.4 branch. Discussion ---------- [Doctrine Bridge] fix priority for doctrine event listeners | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21977 | License | MIT | Doc PR | - As discussed in #27126 this ports changes from #22001 to 3.4 that were dropped when merging 2.8 into 3.2 here: dc66960#diff-27d2e9b071d766df504c3fe4131e7abf I took my original changeset from 2.8 and applied all commits since then on top of that. Commits ------- b3ac938 [Doctrine Bridge] fix priority for doctrine event listeners
I just have two listeners and they have to be in the correct order. My first approach was to set the priority in the
tags
for the listeners, I noticed that nothing changed the order. After I checked the code in Symfony/Bridge/Doctrine/ContainerAwareEventManager.php#L101 and compared it to Symfony/Component/Form/FormConfigBuilderInterface.php#L33 I just realized that the priority is missing.Yeah I know that Component/Form and the Bridge/Doctrine are two different parts.
The text was updated successfully, but these errors were encountered: