Skip to content

[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

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,34 @@ trait PriorityTaggedServiceTrait
/**
* Finds all services with the given tag name and order them by their priority.
*
* The order of additions must be respected for services having the same priority,
* and knowing that the \SplPriorityQueue class does not respect the FIFO method,
* we should not use this class.
*
* @see https://bugs.php.net/bug.php?id=53710
* @see https://bugs.php.net/bug.php?id=60926
*
* @param string $tagName
* @param ContainerBuilder $container
*
* @return Reference[]
*/
private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
{
$services = $container->findTaggedServiceIds($tagName);

$queue = new \SplPriorityQueue();
$services = array();

foreach ($services as $serviceId => $tags) {
foreach ($container->findTaggedServiceIds($tagName) as $serviceId => $tags) {
foreach ($tags as $attributes) {
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
$queue->insert(new Reference($serviceId), $priority);
$services[$priority][] = new Reference($serviceId);
}
}

return iterator_to_array($queue, false);
if ($services) {
krsort($services);
$services = call_user_func_array('array_merge', $services);
}

return $services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function testThatCacheWarmersAreProcessedInPriorityOrder()
'my_service11' => array('my_custom_tag' => array('priority' => -1001)),
'my_service12' => array('my_custom_tag' => array('priority' => -1002)),
'my_service13' => array('my_custom_tag' => array('priority' => -1003)),
'my_service14' => array('my_custom_tag' => array('priority' => -1000)),
'my_service15' => array('my_custom_tag' => array('priority' => 1)),
'my_service16' => array('my_custom_tag' => array('priority' => -1)),
'my_service17' => array('my_custom_tag' => array('priority' => 200)),
'my_service18' => array('my_custom_tag' => array('priority' => 100)),
'my_service19' => array('my_custom_tag' => array()),
);

$container = new ContainerBuilder();
Expand All @@ -47,15 +53,21 @@ public function testThatCacheWarmersAreProcessedInPriorityOrder()

$expected = array(
new Reference('my_service2'),
new Reference('my_service17'),
new Reference('my_service1'),
new Reference('my_service18'),
new Reference('my_service8'),
new Reference('my_service15'),
new Reference('my_service4'),
new Reference('my_service19'),
new Reference('my_service5'),
new Reference('my_service16'),
new Reference('my_service9'),
new Reference('my_service7'),
new Reference('my_service6'),
new Reference('my_service3'),
new Reference('my_service10'),
new Reference('my_service14'),
new Reference('my_service11'),
new Reference('my_service12'),
new Reference('my_service13'),
Expand Down