You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #35342 [DI] Fix support for multiple tags for locators and iterators (Alexandre Parent)
This PR was merged into the 4.4 branch.
Discussion
----------
[DI] Fix support for multiple tags for locators and iterators
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Tickets | Fix#34462, Fix#35326
| License | MIT
| Doc PR | none
Fix PriorityTaggedServiceTrait::findAndSortTaggedServices to work with multiple explicitely tagged services as would be expected by !tagged_locator. Also reorganize PriorityTaggedServiceTrait::findAndSortTaggedServices to be simpler and easier to understand.
Commits
-------
6fc91eb [DI] Fix support for multiple tags for locators and iterators
if (($r = $container->getReflectionClass($class)) && $r->hasMethod($defaultPriorityMethod)) {
65
-
if (!($rm = $r->getMethod($defaultPriorityMethod))->isStatic()) {
66
-
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s".', $class, $defaultPriorityMethod, $tagName, $serviceId));
67
-
}
68
-
69
-
if (!$rm->isPublic()) {
70
-
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s".', $class, $defaultPriorityMethod, $tagName, $serviceId));
71
-
}
72
-
73
-
$priority = $rm->invoke(null);
74
-
75
-
if (!\is_int($priority)) {
76
-
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s".', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId));
if (!$r && !$r = $container->getReflectionClass($class)) {
99
-
thrownewInvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId));
100
-
}
70
+
if (($r = ($r ?? $container->getReflectionClass($class))) && $r->hasMethod($defaultPriorityMethod)) {
71
+
if (!($rm = $r->getMethod($defaultPriorityMethod))->isStatic()) {
72
+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s".', $class, $defaultPriorityMethod, $tagName, $serviceId));
73
+
}
101
74
102
-
$class = $r->name;
75
+
if (!$rm->isPublic()) {
76
+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s".', $class, $defaultPriorityMethod, $tagName, $serviceId));
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s".', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId));
83
+
}
84
+
}
109
85
}
110
86
111
-
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" not found: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
112
-
}
87
+
$priority = $priority ?? $defaultPriority ?? 0;
88
+
89
+
if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
if (($r = ($r ?? $container->getReflectionClass($class))) && $r->hasMethod($defaultIndexMethod)) {
101
+
if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) {
102
+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
103
+
}
104
+
105
+
if (!$rm->isPublic()) {
106
+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
107
+
}
108
+
109
+
$defaultIndex = $rm->invoke(null);
110
+
111
+
if (!\is_string($defaultIndex)) {
112
+
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should return a string, got %s: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, \gettype($defaultIndex), $tagName, $serviceId, $indexAttribute));
113
+
}
114
+
}
113
115
114
-
if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) {
115
-
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
116
-
}
116
+
$defaultIndex = $defaultIndex ?? $serviceId;
117
+
}
117
118
118
-
if (!$rm->isPublic()) {
119
-
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
thrownewInvalidArgumentException(sprintf('Method "%s::%s()" should return a string, got %s: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, \gettype($key), $tagName, $serviceId, $indexAttribute));
$this->assertTrue($s->getParam() instanceof ServiceLocator, sprintf('Wrong instance, should be an instance of ServiceLocator, %s given', \is_object($serviceLocator) ? \get_class($serviceLocator) : \gettype($serviceLocator)));
0 commit comments