Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] Define default priority inside service class#31943
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
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 } } ?>
yceruto left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Hola! Congratulations for your first attempt :) 🎉
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Others code standard that aren't cover by fabbot.io:
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Co-Authored-By: Valentin Udaltsov <udaltsov.valentin@gmail.com>
Co-Authored-By: Valentin Udaltsov <udaltsov.valentin@gmail.com>
Co-Authored-By: Valentin Udaltsov <udaltsov.valentin@gmail.com>
src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Co-Authored-By: Kevin Verschaeve <kevin.verschaeve@live.fr>
This documentssymfony/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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
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. | ||
| */ | ||
| functiontagged(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null):TaggedIteratorArgument | ||
| functiontagged(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null,bool$needsIndexes =false,string$defaultPriorityMethod =null):TaggedIteratorArgument |
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.
should be reverted, this function is deprecated, no need to alter it
| * Creates a lazy iterator by tag name. | ||
| */ | ||
| functiontagged_iterator(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null):TaggedIteratorArgument | ||
| functiontagged_iterator(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null,bool$needsIndexes =false,string$defaultPriorityMethod =null):TaggedIteratorArgument |
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.
$needsIndexes should be removed, it's always false for iterators
| * Creates a service locator by tag name. | ||
| */ | ||
| functiontagged_locator(string$tag,string$indexAttribute,string$defaultIndexMethod =null):ServiceLocatorArgument | ||
| functiontagged_locator(string$tag,string$indexAttribute,string$defaultIndexMethod =null,bool$needsIndexes =true,string$defaultPriorityMethod =null):ServiceLocatorArgument |
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.
$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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
this line must be executed only ifnull !== $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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
let's keep$r as the name here, that will reduce the diff, thus reduce future merge conflicts
nicolas-grekas commentedSep 4, 2020
Wait, this has already been implemented in#33628! |
Uh oh!
There was an error while loading.Please reload this page.
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!