Skip to content

Commit 5280d5d

Browse files
bug #19049 [DependencyInjection] fix the sorting by priority (xabbuh)
This PR was merged into the 3.2-dev branch. Discussion ---------- [DependencyInjection] fix the sorting by priority | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18482 | License | MIT | Doc PR | Commits ------- 6f72657 [DependencyInjection] fix the sorting by priority
2 parents 7581f71 + 6f72657 commit 5280d5d

File tree

4 files changed

+81
-88
lines changed

4 files changed

+81
-88
lines changed

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/asset": "~2.8|~3.0",
2121
"symfony/cache": "~3.1",
2222
"symfony/class-loader": "~2.8|~3.0",
23-
"symfony/dependency-injection": "~3.1",
23+
"symfony/dependency-injection": "~3.2",
2424
"symfony/config": "~2.8|~3.0",
2525
"symfony/event-dispatcher": "~2.8|~3.0",
2626
"symfony/http-foundation": "~3.1",

src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
3838
foreach ($services as $serviceId => $tags) {
3939
foreach ($tags as $attributes) {
4040
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
41-
$queue->insert(new Reference($serviceId), $priority * -1);
41+
$queue->insert(new Reference($serviceId), $priority);
4242
}
4343
}
4444

45-
return iterator_to_array($queue);
45+
return iterator_to_array($queue, false);
4646
}
4747
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTrait.php

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
class PriorityTaggedServiceTraitTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testThatCacheWarmersAreProcessedInPriorityOrder()
21+
{
22+
$services = array(
23+
'my_service1' => array('my_custom_tag' => array('priority' => 100)),
24+
'my_service2' => array('my_custom_tag' => array('priority' => 200)),
25+
'my_service3' => array('my_custom_tag' => array('priority' => -501)),
26+
'my_service4' => array('my_custom_tag' => array()),
27+
'my_service5' => array('my_custom_tag' => array('priority' => -1)),
28+
'my_service6' => array('my_custom_tag' => array('priority' => -500)),
29+
'my_service7' => array('my_custom_tag' => array('priority' => -499)),
30+
'my_service8' => array('my_custom_tag' => array('priority' => 1)),
31+
'my_service9' => array('my_custom_tag' => array('priority' => -2)),
32+
'my_service10' => array('my_custom_tag' => array('priority' => -1000)),
33+
'my_service11' => array('my_custom_tag' => array('priority' => -1001)),
34+
'my_service12' => array('my_custom_tag' => array('priority' => -1002)),
35+
'my_service13' => array('my_custom_tag' => array('priority' => -1003)),
36+
);
37+
38+
$container = new ContainerBuilder();
39+
40+
foreach ($services as $id => $tags) {
41+
$definition = $container->register($id);
42+
43+
foreach ($tags as $name => $attributes) {
44+
$definition->addTag($name, $attributes);
45+
}
46+
}
47+
48+
$expected = array(
49+
new Reference('my_service2'),
50+
new Reference('my_service1'),
51+
new Reference('my_service8'),
52+
new Reference('my_service4'),
53+
new Reference('my_service5'),
54+
new Reference('my_service9'),
55+
new Reference('my_service7'),
56+
new Reference('my_service6'),
57+
new Reference('my_service3'),
58+
new Reference('my_service10'),
59+
new Reference('my_service11'),
60+
new Reference('my_service12'),
61+
new Reference('my_service13'),
62+
);
63+
64+
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
65+
66+
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test('my_custom_tag', $container));
67+
}
68+
}
69+
70+
class PriorityTaggedServiceTraitImplementation
71+
{
72+
use PriorityTaggedServiceTrait;
73+
74+
public function test($tagName, ContainerBuilder $container)
75+
{
76+
return $this->findAndSortTaggedServices($tagName, $container);
77+
}
78+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy