Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Closed
pcabreus wants to merge13 commits intosymfony:4.4frompcabreus:patch-1

Conversation

@pcabreus
Copy link

@pcabreuspcabreus commentedJun 7, 2019
edited
Loading

QA
Branch?4.4 for features
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#...
LicenseMIT

With autoconfigure functions we don't have to do much to inject tagged services into a collector using interfaces, using!tagged is 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 methodgetDefaultPriority exists 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:

<?phpnamespace 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    }}?>

PD: First PR please by nice!

vudaltsov reacted with thumbs up emoji
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        }    }    ?>
Copy link
Member

@ycerutoyceruto left a comment
edited
Loading

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 :) 🎉

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.
Copy link
Member

@ycerutoyceruto left a 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:

pcabreusand others added5 commitsJune 11, 2019 10:10
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>
pcabreus added a commit to pcabreus/symfony-docs that referenced this pull requestJun 20, 2019
This documentssymfony/symfony#31943 about adding a custom method to define the priority of services
@nicolas-grekasnicolas-grekas changed the titleDefine default priority inside service class[DependencyInjection] Define default priority inside service classSep 3, 2020
Copy link
Member

@nicolas-grekasnicolas-grekas left a 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

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

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

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);

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;

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
Copy link
Member

Wait, this has already been implemented in#33628!
Thanks for pushing this forward and sorry for the confusion.

@nicolas-grekasnicolas-grekas removed this from thenext milestoneOct 5, 2020
@nicolas-grekasnicolas-grekas added this to the5.2 milestoneOct 5, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@ycerutoycerutoyceruto left review comments

+2 more reviewers

@kevin-verschaevekevin-verschaevekevin-verschaeve left review comments

@vudaltsovvudaltsovvudaltsov requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

5.2

Development

Successfully merging this pull request may close these issues.

6 participants

@pcabreus@nicolas-grekas@yceruto@kevin-verschaeve@vudaltsov@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp