-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e2138ad
bd88e1e
2df617e
4fed2a3
b6069b2
5498824
8ab9cfd
f48eea9
c021fc8
2d8fd1f
6b51ac5
b9dd5eb
41f5e99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,42 +41,61 @@ trait PriorityTaggedServiceTrait | |
*/ | ||
private function findAndSortTaggedServices($tagName, ContainerBuilder $container) | ||
{ | ||
$indexAttribute = $defaultIndexMethod = $needsIndexes = null; | ||
$indexAttribute = $defaultIndexMethod = $needsIndexes = $defaultPriorityMethod = null; | ||
|
||
if ($tagName instanceof TaggedIteratorArgument) { | ||
$indexAttribute = $tagName->getIndexAttribute(); | ||
$defaultIndexMethod = $tagName->getDefaultIndexMethod(); | ||
$needsIndexes = $tagName->needsIndexes(); | ||
$defaultPriorityMethod = $tagName->getDefaultPriorityMethod(); | ||
$tagName = $tagName->getTag(); | ||
} | ||
|
||
$services = []; | ||
|
||
foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) { | ||
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; | ||
$class = $container->getDefinition($serviceId)->getClass(); | ||
$class = $container->getParameterBag()->resolveValue($class) ?: null; | ||
$reflectionClass = $container->getReflectionClass($class); | ||
|
||
$priority = 0; | ||
if (null !== $defaultPriorityMethod && null !== $reflectionClass && $reflectionClass->hasMethod($defaultPriorityMethod)) { | ||
if (!($priorityReflMethod = $reflectionClass->getMethod($defaultPriorityMethod))->isStatic()) { | ||
throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" must be static.', $class, $defaultPriorityMethod, $tagName, $serviceId)); | ||
} | ||
|
||
if (!$priorityReflMethod->isPublic()) { | ||
throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" should be public.', $class, $defaultPriorityMethod, $tagName, $serviceId)); | ||
} | ||
|
||
$priority = $priorityReflMethod->invoke(null); | ||
|
||
if (!\is_int($priority)) { | ||
throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" should return an integer, got %s.', $class, $defaultPriorityMethod, $tagName, $serviceId, \gettype($priority))); | ||
} | ||
} | ||
|
||
$priority = $attributes[0]['priority'] ?? $priority; | ||
|
||
if (null === $indexAttribute && !$needsIndexes) { | ||
$services[$priority][] = new Reference($serviceId); | ||
|
||
continue; | ||
} | ||
|
||
$class = $container->getDefinition($serviceId)->getClass(); | ||
$class = $container->getParameterBag()->resolveValue($class) ?: null; | ||
|
||
if (null !== $indexAttribute && isset($attributes[0][$indexAttribute])) { | ||
$services[$priority][$attributes[0][$indexAttribute]] = new TypedReference($serviceId, $class, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, $attributes[0][$indexAttribute]); | ||
|
||
continue; | ||
} | ||
|
||
if (!$r = $container->getReflectionClass($class)) { | ||
if (null === $reflectionClass) { | ||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); | ||
} | ||
|
||
$class = $r->name; | ||
$class = $reflectionClass->name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's keep |
||
|
||
if (!$r->hasMethod($defaultIndexMethod)) { | ||
if (!$reflectionClass->hasMethod($defaultIndexMethod)) { | ||
if ($needsIndexes) { | ||
$services[$priority][$serviceId] = new TypedReference($serviceId, $class); | ||
|
||
|
@@ -86,7 +105,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container | |
throw new InvalidArgumentException(sprintf('Method "%s::%s()" not found: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute)); | ||
} | ||
|
||
if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) { | ||
if (!($rm = $reflectionClass->getMethod($defaultIndexMethod))->isStatic()) { | ||
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,27 +118,27 @@ function iterator(array $values): IteratorArgument | |
* | ||
* @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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be reverted, this function is deprecated, no need to alter it |
||
{ | ||
@trigger_error(__NAMESPACE__.'\tagged() is deprecated since Symfony 4.4 and will be removed in 5.0, use '.__NAMESPACE__.'\tagged_iterator() instead.', E_USER_DEPRECATED); | ||
|
||
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod); | ||
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, $needsIndexes, $defaultPriorityMethod); | ||
} | ||
|
||
/** | ||
* 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $needsIndexes should be removed, it's always false for iterators |
||
{ | ||
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod); | ||
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, $needsIndexes, $defaultPriorityMethod); | ||
} | ||
|
||
/** | ||
* 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $needsIndexes should be removed, it's always true for locators |
||
{ | ||
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true)); | ||
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, $needsIndexes, $defaultPriorityMethod)); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/> | ||
<service id="foo" class="Foo"> | ||
<tag name="foo_tag"/> | ||
</service> | ||
<service id="foo_tagged_iterator" class="Bar" public="true"> | ||
<argument type="tagged_iterator" tag="foo_tag" default-priority-method="foopriority"/> | ||
</service> | ||
<service id="foo_tagged_locator" class="Bar" public="true"> | ||
<argument type="tagged_locator" tag="foo_tag" default-priority-method="foopriority"/> | ||
</service> | ||
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/> | ||
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
services: | ||
service_container: | ||
class: Symfony\Component\DependencyInjection\ContainerInterface | ||
public: true | ||
synthetic: true | ||
foo_service: | ||
class: Foo | ||
tags: | ||
- { name: foo } | ||
foo_service_tagged_iterator: | ||
class: Bar | ||
arguments: [!tagged_iterator { tag: foo, default_priority_method: foopriority }] | ||
foo_service_tagged_locator: | ||
class: Bar | ||
arguments: [!tagged_locator { tag: foo, default_priority_method: foopriority }] | ||
Psr\Container\ContainerInterface: | ||
alias: service_container | ||
public: false | ||
Symfony\Component\DependencyInjection\ContainerInterface: | ||
alias: service_container | ||
public: false |
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 if
null !== $defaultPriorityMethod
and$attributes[0]['priority']
is not set, so that we don't autoload a class for nothing when not needed