-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Fix the priority order of compiler pass trait #20995
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
[DependencyInjection] Fix the priority order of compiler pass trait #20995
Conversation
Please add a test to prevent future regressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice with a test case :)
} | ||
} | ||
|
||
return iterator_to_array($queue, false); | ||
if (count($services) > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($services)
has my pref, the count wrapper adds nothing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
@xabbuh I do it right away. |
@nicolas-grekas @xabbuh Test added. |
The test does not work with |
👍 |
1 similar comment
👍 |
maybe better also add note iside trait why |
Will this be added back for 4.0? As @francoispluchino said we're using this in other places and we're promoting it with a special trait. |
@javiereguiluz the SplPriorityQueue does not give any additional feature compared to the implementation used here (and which was used almost everywhere in Symfony before adding the trait btw, which is why switching to SplPriorityQueue was a BC break due to unstable sorting). So there is no drawback keeping this code in 4.0 (except it could use |
@javiereguiluz See this commit. I do not think we need to use |
@Koc It's done. |
Another minor comment: if the problem is that |
Yes and no. Let me explain, ideally, I agree with you, but in reality, when you use different Bundles, it happens very often (if not all the time) that no priority is defined in the services. Priority is defined only when it is really necessary. I'm not saying it's a good practice or not, but it's just an observation. However, there is some kind of implicit priority, namely the order of adding the bundles (and others, as explained in this comment). It is therefore necessary to keep this order for services with the same priority. In addition, we can more easily debug a priority error, because from what I understood on the tracker of PHP, is that the |
👍 Status: Reviewed |
@@ -24,24 +24,34 @@ | |||
/** | |||
* Finds all services with the given tag name and order them by their priority. | |||
* | |||
* The order of additions must to be respected for services having the same priority, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: must to be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops! Fixed!
Update: I change my vote to "abstain". Thanks!
|
Why some tests of Form Component are marked incomplete, while I have corrected a one line phpdoc, and that the previous tests were ok? |
@javiereguiluz Why? Your arguments? |
Am I missing something? |
@francoispluchino This has nothing to do with your PR, some tests are skipped in the form component depending of the installed version of ICU. |
Yes, the addition of the new priority option is present, and in addition, we maintain order for services with the same priority. |
@javiereguiluz there was no priority for form extensions in Symfony 3.1, and so bundles were not defining a priority, but the order of registration was preserved. This PR is about reverting a BC break where a project would stop working when updating to 3.2 because the order of extensions defined without priority would be different (and unknown btw, making things hard to debug as it could change over time) |
@javiereguiluz please reconsider. Stof is right. |
👍, this should also fix FriendsOfSymfony/FOSRestBundle#1602. To complete the other arguments, even inside a bundle, someone might rely on the order of his services definition (when using normalizers for instance): services:
foo:
// ...
tags: [ serializer.normalizer ]
bar:
// ...
tags: [ serializer.normalizer ]
|
The most disturbing, is that the behavior of the 2 tests to illustrate this affirmation: class SplPriorityQueueTest extends \PHPUnit_Framework_TestCase
{
public function testSinglePriority()
{
$queue = new \SplPriorityQueue();
$values = array();
$queue->insert('foo', 1000);// 0
$queue->insert('bar', 1000);// 1
$queue->insert('baz', 1000);// 2
$queue->insert('bat', 1000);// 3
$queue->insert('tar', 1000);// 4
foreach ($queue as $value) {
$values[] = $value;
}
// The obtained result
$this->assertSame(array(
'foo',// 0
'tar',// 4
'bat',// 3
'baz',// 2
'bar',// 1
), $values);
// The expected result
$this->assertSame(array(
'foo',// 0
'bar',// 1
'baz',// 2
'bat',// 3
'tar',// 4
), $values);
}
public function testSeveralPriorities()
{
$queue = new \SplPriorityQueue();
$values = array();
$queue->insert('foo', 1000);// 0
$queue->insert('bar', 1000);// 1
$queue->insert('baz', 1000);// 2
$queue->insert('bat', 1000);// 3
$queue->insert('tar', 2000);// 4
foreach ($queue as $value) {
$values[] = $value;
}
// The obtained result
$this->assertSame(array(
'tar',// 4
'bar',// 1
'bat',// 3
'baz',// 2
'foo',// 0
), $values);
// The expected result
$this->assertSame(array(
'tar',// 4
'foo',// 0
'bar',// 1
'baz',// 2
'bat',// 3
), $values);
}
} @nicolas-grekas To answer of your comment, the result is same in PHP 5.6 and PHP 7.0. And the PHP doc is not very detailed on this, there is only the description for the |
I actually noticed this behaviour when working on the priorities for form type extensions and had some concerns regarding BC 😢 |
There is other compiler passes that uses either
If we later simplify the code using the |
Thanks for providing more examples and comments about this. I've removed my |
Cool.. that's mine :) I also settled with arrays back then. Good fix 👍 |
Thank you @francoispluchino. |
…ass trait (francoispluchino) This PR was merged into the 3.2 branch. Discussion ---------- [DependencyInjection] Fix the priority order of compiler pass trait | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20332, #20993 | License | MIT | Doc PR | ~ A regression has appeared between the version `3.1` and `3.2` on the compilation passes using a priority option (see #20332). Indeed, the order is no longer preserved for service definitions with the same priority. This is caused by the `SplPriorityQueue` class that does not have a FIFO implementation (see this [comment](#20332 (comment))). The PR #20993 fixes the problem but only for Forms, not for all compiler passes using the `PriorityTaggedServiceTrait` trait since `3.2`. Commits ------- aac9a7e Fix the priority order of compiler pass trait
A regression has appeared between the version
3.1
and3.2
on the compilation passes using a priority option (see #20332).Indeed, the order is no longer preserved for service definitions with the same priority. This is caused by the
SplPriorityQueue
class that does not have a FIFO implementation (see this comment).The PR #20993 fixes the problem but only for Forms, not for all compiler passes using the
PriorityTaggedServiceTrait
trait since3.2
.