[DependencyInjection] Define default priority inside service class#31943
[DependencyInjection] Define default priority inside service class#31943pcabreus wants to merge 13 commits intosymfony:4.4from
Conversation
With autoconfigure functions we don't have to do anything to inject tagged services into a collector using interfaces to define tags. But if we need some order we have to use priority, the problem is that priority only can be set by configuration file when it is a simple value.
With this change you could instead of put 0 by defualt take a look if the static method `getDefaultPriority` exists in the service class and return its default value for the service.
Example:
<?php
namespace App;
use App\ServiceTagged;
class Service implement ServiceTagged
{
public static function getDefaultPriority(): int
{
return 50; //This will be the default priority if it's not defined in a configuration file
}
}
?>
It looks like `YamlFileLoader` makes `index_by` mandatory when you define the tagged service using `arguments: [!tagged { tag: 'app.handler'}]` syntax. It's needed to accept others parameters like the new `default_priority_method` without specify `index_by` and the `XmlFielLoader` seems to accept it.
yceruto
left a comment
There was a problem hiding this comment.
Others code standard that aren't cover by fabbot.io:
Co-Authored-By: Valentin Udaltsov <[email protected]>
Co-Authored-By: Valentin Udaltsov <[email protected]>
Co-Authored-By: Valentin Udaltsov <[email protected]>
Co-Authored-By: Kevin Verschaeve <[email protected]>
This documents symfony/symfony#31943 about adding a custom method to define the priority of services
nicolas-grekas
left a comment
There was a problem hiding this comment.
Thanks and sorry for the late review.
Here are some comments. Please rebase also.
Looks good to me otherwise.
| * @deprecated since Symfony 4.4, to be removed in 5.0, use "tagged_iterator" instead. | ||
| */ | ||
| function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): TaggedIteratorArgument | ||
| function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false, string $defaultPriorityMethod = null): TaggedIteratorArgument |
There was a problem hiding this comment.
should be reverted, this function is deprecated, no need to alter it
| * Creates a lazy iterator by tag name. | ||
| */ | ||
| function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): TaggedIteratorArgument | ||
| function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false, string $defaultPriorityMethod = null): TaggedIteratorArgument |
There was a problem hiding this comment.
$needsIndexes should be removed, it's always false for iterators
| * Creates a service locator by tag name. | ||
| */ | ||
| function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null): ServiceLocatorArgument | ||
| function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null, bool $needsIndexes = true, string $defaultPriorityMethod = null): ServiceLocatorArgument |
There was a problem hiding this comment.
$needsIndexes should be removed, it's always true for locators
| $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; | ||
| $class = $container->getDefinition($serviceId)->getClass(); | ||
| $class = $container->getParameterBag()->resolveValue($class) ?: null; | ||
| $reflectionClass = $container->getReflectionClass($class); |
There was a problem hiding this comment.
this line must be executed only if null !== $defaultPriorityMethod and $attributes[0]['priority'] is not set, so that we don't autoload a class for nothing when not needed
| } | ||
|
|
||
| $class = $r->name; | ||
| $class = $reflectionClass->name; |
There was a problem hiding this comment.
let's keep $r as the name here, that will reduce the diff, thus reduce future merge conflicts
|
Wait, this has already been implemented in #33628! |
With autoconfigure functions we don't have to do much to inject tagged services into a collector using interfaces, using
!taggedis the easy way. But if we need some order we have to use priority, the problem is that priority only can be set by configuration file when it is a simple value.This PR take a look if the static method
getDefaultPriorityexists in the service class and return its default value for the service instead of current 0 by defualt.Example of a service with default priority:
PD: First PR please by nice!