-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Created a trait to sort tagged services #18482
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ public function testThatCheckersAreProcessedInPriorityOrder() | |
$services = array( | ||
'checker_2' => array(0 => array('priority' => 100)), | ||
'checker_1' => array(0 => array('priority' => 200)), | ||
'checker_3' => array(), | ||
'checker_3' => array(0 => array()), | ||
); | ||
|
||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); | ||
|
@@ -52,7 +52,6 @@ public function testThatCheckersAreProcessedInPriorityOrder() | |
|
||
public function testThatCheckersCanBeMissing() | ||
{ | ||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); | ||
$container = $this->getMock( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was unused |
||
'Symfony\Component\DependencyInjection\ContainerBuilder', | ||
array('findTaggedServiceIds') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Trait that allows a generic method to find and sort service by priority option in the tag. | ||
* | ||
* @author Iltar van der Berg <kjarli@gmail.com> | ||
*/ | ||
trait PriorityTaggedServiceTrait | ||
{ | ||
/** | ||
* Finds all services with the given tag name and order them by their priority. | ||
* | ||
* @param string $tagName | ||
* @param ContainerBuilder $container | ||
* | ||
* @return Reference[] | ||
*/ | ||
private function findAndSortTaggedServices($tagName, ContainerBuilder $container) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so as they are public to the class they are used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would need an update on the bc policy! |
||
{ | ||
$services = $container->findTaggedServiceIds($tagName); | ||
|
||
$sortedServices = array(); | ||
foreach ($services as $serviceId => $tags) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use SplPriorityQueue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to admit that I've not used it before, but it looks interesting. On Fri, 8 Apr 2016 16:32 Javier Eguiluz, notifications@github.com wrote:
|
||
foreach ($tags as $attributes) { | ||
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0; | ||
$sortedServices[$priority][] = new Reference($serviceId); | ||
} | ||
} | ||
|
||
if (empty($sortedServices)) { | ||
return array(); | ||
} | ||
|
||
krsort($sortedServices); | ||
|
||
// Flatten the array | ||
return call_user_func_array('array_merge', $sortedServices); | ||
} | ||
} |
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.
had to fix this in the tests because the container method wouldn't return like this, same goes for the other test.