Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #18482 Created a trait to sort tagged services (iltar)
This PR was squashed before being merged into the 3.2-dev branch (closes #18482). Discussion ---------- Created a trait to sort tagged services | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ When writing the `ControllerArgumentValueResolverPass`, I needed a sorting on `priority`. I ended up copying a method from another class. I noticed this was done more often and 99% of the code was the same. I've moved the most common notation into the trait and "used" the trait in the priority aware passes. This increases horizontal re-use and means people can also use this in their bundles. The cases that were slightly different, are still working completely. I had to fix some tests because they returned an invalid value from the mocked find method. Commits ------- 778a70b Created a trait to sort tagged services
- Loading branch information
Showing
9 changed files
with
158 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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) | ||
{ | ||
$services = $container->findTaggedServiceIds($tagName); | ||
|
||
$queue = new \SplPriorityQueue(); | ||
|
||
foreach ($services as $serviceId => $tags) { | ||
foreach ($tags as $attributes) { | ||
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0; | ||
$queue->insert(new Reference($serviceId), $priority * -1); | ||
} | ||
} | ||
|
||
return iterator_to_array($queue); | ||
} | ||
} |
Oops, something went wrong.