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

Commit50ead19

Browse files
committed
[DependencyInjection] added Ability to define a priority method for tagged service
1 parent161975d commit50ead19

19 files changed

+125
-26
lines changed

‎src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class TaggedIteratorArgument extends IteratorArgument
2424
private$needsIndexes =false;
2525

2626
/**
27-
* @param string $tag The name of the tag identifying the target services
28-
* @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection
29-
* @param string|null $defaultIndexMethod The static method that should be called to get each service's key when their tag doesn't define the previous attribute
30-
* @param bool $needsIndexes Whether indexes are required and should be generated when computing the map
27+
* @param string $tag The name of the tag identifying the target services
28+
* @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection
29+
* @param string|null $defaultIndexMethod The static method that should be called to get each service's key when their tag doesn't define the previous attribute
30+
* @param bool $needsIndexes Whether indexes are required and should be generated when computing the map
31+
* @param string|null $defaultPriorityMethod The static method that should be called to get each service's priority when their tag doesn't define the "priority" attribute
3132
*/
32-
publicfunction__construct(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null,bool$needsIndexes =false)
33+
publicfunction__construct(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null,bool$needsIndexes =false,string$defaultPriorityMethod =null)
3334
{
3435
parent::__construct([]);
3536

@@ -41,6 +42,7 @@ public function __construct(string $tag, string $indexAttribute = null, string $
4142
$this->indexAttribute =$indexAttribute;
4243
$this->defaultIndexMethod =$defaultIndexMethod ?: ('getDefault'.str_replace('','',ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/','',$indexAttribute ??''))).'Name');
4344
$this->needsIndexes =$needsIndexes;
45+
$this->defaultPriorityMethod =$defaultPriorityMethod ?: ('getDefault'.str_replace('','',ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/','',$indexAttribute ??''))).'Priority');
4446
}
4547

4648
publicfunctiongetTag()
@@ -62,4 +64,9 @@ public function needsIndexes(): bool
6264
{
6365
return$this->needsIndexes;
6466
}
67+
68+
publicfunctiongetDefaultPriorityMethod(): ?string
69+
{
70+
return$this->defaultPriorityMethod;
71+
}
6572
}

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* deprecated passing an instance of`Symfony\Component\DependencyInjection\Parameter` as class name to`Symfony\Component\DependencyInjection\Definition`
1212
* added support for binding iterable and tagged services
1313
* made singly-implemented interfaces detection be scoped by file
14+
* added ability to define a static priority method for tagged service
1415

1516
4.3.0
1617
-----

‎src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,65 @@ trait PriorityTaggedServiceTrait
4040
*/
4141
privatefunctionfindAndSortTaggedServices($tagName,ContainerBuilder$container):array
4242
{
43-
$indexAttribute =$defaultIndexMethod =$needsIndexes =null;
43+
$indexAttribute =$defaultIndexMethod =$needsIndexes =$defaultPriorityMethod =null;
4444

4545
if ($tagNameinstanceof TaggedIteratorArgument) {
4646
$indexAttribute =$tagName->getIndexAttribute();
4747
$defaultIndexMethod =$tagName->getDefaultIndexMethod();
4848
$needsIndexes =$tagName->needsIndexes();
49+
$defaultPriorityMethod =$tagName->getDefaultPriorityMethod();
4950
$tagName =$tagName->getTag();
5051
}
5152

5253
$services = [];
5354

5455
foreach ($container->findTaggedServiceIds($tagName,true)as$serviceId =>$attributes) {
55-
$priority =isset($attributes[0]['priority']) ?$attributes[0]['priority'] :0;
56+
$class =$r =null;
57+
if (isset($attributes[0]['priority'])) {
58+
$priority =$attributes[0]['priority'];
59+
}elseif ($defaultPriorityMethod) {
60+
$class =$container->getDefinition($serviceId)->getClass();
61+
$class =$container->getParameterBag()->resolveValue($class) ?:null;
62+
63+
if (($r =$container->getReflectionClass($class)) &&$r->hasMethod($defaultPriorityMethod)) {
64+
if (!($rm =$r->getMethod($defaultPriorityMethod))->isStatic()) {
65+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s".',$class,$defaultPriorityMethod,$tagName,$serviceId));
66+
}
67+
68+
if (!$rm->isPublic()) {
69+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s".',$class,$defaultPriorityMethod,$tagName,$serviceId));
70+
}
71+
72+
$priority =$rm->invoke(null);
73+
74+
if (!\is_int($priority)) {
75+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s".',$class,$defaultPriorityMethod,\gettype($priority),$tagName,$serviceId));
76+
}
77+
}else {
78+
$priority =0;
79+
}
80+
}else {
81+
$priority =0;
82+
}
5683

5784
if (null ===$indexAttribute && !$needsIndexes) {
5885
$services[$priority][] =newReference($serviceId);
5986

6087
continue;
6188
}
6289

63-
$class =$container->getDefinition($serviceId)->getClass();
64-
$class =$container->getParameterBag()->resolveValue($class) ?:null;
90+
if (!$class) {
91+
$class =$container->getDefinition($serviceId)->getClass();
92+
$class =$container->getParameterBag()->resolveValue($class) ?:null;
93+
}
6594

6695
if (null !==$indexAttribute &&isset($attributes[0][$indexAttribute])) {
6796
$services[$priority][$attributes[0][$indexAttribute]] =newTypedReference($serviceId,$class, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE,$attributes[0][$indexAttribute]);
6897

6998
continue;
7099
}
71100

72-
if (!$r =$container->getReflectionClass($class)) {
101+
if (!$r&& !$r=$container->getReflectionClass($class)) {
73102
thrownewInvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.',$class,$serviceId));
74103
}
75104

‎src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ private function convertParameters(array $parameters, string $type, \DOMElement
274274
if (null !==$tag->getDefaultIndexMethod()) {
275275
$element->setAttribute('default-index-method',$tag->getDefaultIndexMethod());
276276
}
277+
if (null !==$tag->getDefaultPriorityMethod()) {
278+
$element->setAttribute('default-priority-method',$tag->getDefaultPriorityMethod());
279+
}
277280
}
278281
}elseif ($valueinstanceof IteratorArgument) {
279282
$element->setAttribute('type','iterator');

‎src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ private function dumpValue($value)
244244
if (null !==$tag->getDefaultIndexMethod()) {
245245
$content['default_index_method'] =$tag->getDefaultIndexMethod();
246246
}
247+
if (null !==$tag->getDefaultPriorityMethod()) {
248+
$content['default_priority_method'] =$tag->getDefaultPriorityMethod();
249+
}
247250
}
248251

249252
returnnewTaggedValue($valueinstanceof TaggedIteratorArgument ?'tagged_iterator' :'tagged_locator',$content);

‎src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ function tagged(string $tag, string $indexAttribute = null, string $defaultIndex
128128
/**
129129
* Creates a lazy iterator by tag name.
130130
*/
131-
functiontagged_iterator(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null):TaggedIteratorArgument
131+
functiontagged_iterator(string$tag,string$indexAttribute =null,string$defaultIndexMethod =null,string$defaultPriorityMethod =null):TaggedIteratorArgument
132132
{
133-
returnnewTaggedIteratorArgument($tag,$indexAttribute,$defaultIndexMethod);
133+
returnnewTaggedIteratorArgument($tag,$indexAttribute,$defaultIndexMethod,false,$defaultPriorityMethod);
134134
}
135135

136136
/**

‎src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file
513513
thrownewInvalidArgumentException(sprintf('Tag "<%s>" with type="%s" has no or empty "tag" attribute in "%s".',$name,$type,$file));
514514
}
515515

516-
$arguments[$key] =newTaggedIteratorArgument($arg->getAttribute('tag'),$arg->getAttribute('index-by') ?:null,$arg->getAttribute('default-index-method') ?:null,$forLocator);
516+
$arguments[$key] =newTaggedIteratorArgument($arg->getAttribute('tag'),$arg->getAttribute('index-by') ?:null,$arg->getAttribute('default-index-method') ?:null,$forLocator,$arg->getAttribute('default-priority-method') ?:null);
517517

518518
if ($forLocator) {
519519
$arguments[$key] =newServiceLocatorArgument($arguments[$key]);

‎src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,11 @@ private function resolveServices($value, string $file, bool $isParameter = false
721721
$forLocator ='tagged_locator' ===$value->getTag();
722722

723723
if (\is_array($argument) &&isset($argument['tag']) &&$argument['tag']) {
724-
if ($diff =array_diff(array_keys($argument), ['tag','index_by','default_index_method'])) {
725-
thrownewInvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by"and "default_index_method".',$value->getTag(),implode('"", "',$diff)));
724+
if ($diff =array_diff(array_keys($argument), ['tag','index_by','default_index_method','default_priority_method'])) {
725+
thrownewInvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by", "default_index_method",and "default_priority_method".',$value->getTag(),implode('"", "',$diff)));
726726
}
727727

728-
$argument =newTaggedIteratorArgument($argument['tag'],$argument['index_by'] ??null,$argument['default_index_method'] ??null,$forLocator);
728+
$argument =newTaggedIteratorArgument($argument['tag'],$argument['index_by'] ??null,$argument['default_index_method'] ??null,$forLocator,$argument['default_priority_method'] ??null);
729729
}elseif (\is_string($argument) &&$argument) {
730730
$argument =newTaggedIteratorArgument($argument,null,null,$forLocator);
731731
}else {

‎src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
<xsd:attributename="tag"type="xsd:string" />
237237
<xsd:attributename="index-by"type="xsd:string" />
238238
<xsd:attributename="default-index-method"type="xsd:string" />
239+
<xsd:attributename="default-priority-method"type="xsd:string" />
239240
</xsd:complexType>
240241

241242
<xsd:complexTypename="call">

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
useSymfony\Component\DependencyInjection\ServiceSubscriberInterface;
2424
useSymfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
2525
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
26+
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
2627
useSymfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
2728

2829
/**
@@ -289,6 +290,30 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
289290
$this->assertSame(['bar_tab_class_with_defaultmethod' =>$container->get(BarTagClass::class),'foo' =>$container->get(FooTagClass::class)],$param);
290291
}
291292

293+
publicfunctiontestTaggedServiceWithDefaultPriorityMethod()
294+
{
295+
$container =newContainerBuilder();
296+
$container->register(BarTagClass::class)
297+
->setPublic(true)
298+
->addTag('foo_bar')
299+
;
300+
$container->register(FooTagClass::class)
301+
->setPublic(true)
302+
->addTag('foo_bar', ['foo' =>'foo'])
303+
;
304+
$container->register(FooBarTaggedForDefaultPriorityClass::class)
305+
->addArgument(newTaggedIteratorArgument('foo_bar',null,null,false,'getPriority'))
306+
->setPublic(true)
307+
;
308+
309+
$container->compile();
310+
311+
$s =$container->get(FooBarTaggedForDefaultPriorityClass::class);
312+
313+
$param =iterator_to_array($s->getParam()->getIterator());
314+
$this->assertSame([$container->get(FooTagClass::class),$container->get(BarTagClass::class)],$param);
315+
}
316+
292317
publicfunctiontestTaggedServiceLocatorWithIndexAttribute()
293318
{
294319
$container =newContainerBuilder();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp