-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Fix PriorityTaggedServiceTrait
for AsTaggedItem
#47406
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
Conversation
a18f9a5
to
bd1273b
Compare
Hey! I think @ruudk has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
Could you please add some tests cases that show this fix is needed. |
bd1273b
to
620b0ba
Compare
If `$tagName` is string, `$defaultPriorityMethod` will be null, and `PriorityTaggedServiceUtil::getDefault` won't work for tagged services. This will cause `AsTaggedItem` not working for these tag names.
620b0ba
to
cda7d7f
Compare
@ruudk I have added assertions for this condition. |
@ruudk are there any updates with this pr? |
I checked it out but don't really understand what this fixes and how it's needed. But I just wanted you to add tests because that is something the Symfony reviewers will require anyway. So you have to ask somebody from the core team to have a look. |
Please describe exactly the case that is broken right now (with examples for instance). Evaluating a fix for an issue that is not fully understood is hard. |
@stof I have added a test case to check this problem, you can find it in PriorityTaggedServiceTraitTest.php. The problem is quite simple: when iterating services with |
@stof any update? |
PING @stof |
I can confirm this problem but this solution did not work for me. I have an interface marked with Are we talking about the same problem? |
I've created a reproducer that demonstrates my problem: https://github.com/kbond/symfony-reproducer/tree/as-tagged-item (commit). When running |
@kbond In your reproduce repo, I added a command and used the following code to inject implementations: #[AsCommand('app:test')]
class TestCommand extends Command {
public function __construct(#[TaggedIterator('some_tag')] iterable $services) {
var_dump(iterator_to_array($services));
parent::__construct();
}
public function execute(InputInterface $input, OutputInterface $output): int { return Command::SUCCESS; }
} Adjusted priority attributed on As a supplement, here is the Kernel class for your repo to reproduce the problem this PR fixes: class Kernel extends BaseKernel {
use MicroKernelTrait;
protected function build(ContainerBuilder $container) {
$container->addCompilerPass(new class implements CompilerPassInterface {
use PriorityTaggedServiceTrait;
public function process(ContainerBuilder $container) {
var_dump($this->findAndSortTaggedServices(
'some_tag', // BAD
$container
));
var_dump($this->findAndSortTaggedServices(
new TaggedIteratorArgument('some_tag'), // GOOD
$container
));
}
});
}
} After applying my patch, the two But I can confirm there is a bug in DebugBundle (already noticed when I was composing this PR), |
Ah, thanks @moesoha! I've confirmed the To clarify, my issue is is a separate problem in |
@kbond exactly. |
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.
I'm sorry but this looks incorrect to me: AsTaggedItem
is supposed to work together with TaggedIteratorArgument
. That's what the word "Tagged" means in the name of the attribute. When not requesting for "tagged" items, the returned array should be a regular list.
When did you encounter this behavior? If it was in a compiler pass of your own, you need to give this method a TaggedIteratorArgument
to get the indexed array.
@kbond your example is the same: AsTaggedItem applies only when requesting for a TaggedIterator, not when "just" getting the list of tags. I'm closing here because this works as designed. I understand it might not match your expectations, but changing this should be considered as a new feature I guess - not as a bugfix for sure. Thanks for submitting. |
One last note on this: if we were to implement something like you expected, I'd suggest doing it via a tag, eg (or maybe |
If
$tagName
is string,$defaultPriorityMethod
will be null, andPriorityTaggedServiceUtil::getDefault
won't work for tagged services. This will causeAsTaggedItem
not working for these tag names.