8000 [DependencyInjection] Use "!tagged" with "index_by" collect service only once when used multiple tags · Issue #34462 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[DependencyInjection] Use "!tagged" with "index_by" collect service only once when used multiple tags #34462
Closed
@andrew-demb

Description

@andrew-demb

Symfony version(s) affected: 4.3.8

Description
Use "!tagged" with "index_by" collect service only once when used multiple tags.

Expected: service will be added to argument for every tag with different indexes.
Actual: only first entry used.

How to reproduce

<?php

require 'vendor/autoload.php';

$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();

$registryDefinition = (new \Symfony\Component\DependencyInjection\Definition())
    ->setClass(IteratorIterator::class)
    ->setPublic(true)
    ->addArgument(
        new \Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument(
            'bug',
            'type'
        )
    );
$entryDefinition = (new \Symfony\Component\DependencyInjection\Definition())
    ->setClass(stdClass::class)
    ->addTag('bug', ['type' => 'first'])
    ->addTag('bug', ['type' => 'second']);

$container->setDefinition('registry', $registryDefinition);
$container->setDefinition('entry', $entryDefinition);

$container->compile();

/** @var IteratorIterator $registry */
$registry = $container->get('registry');

$received = [];
foreach ($registry as $index => $entry) {
    $received[] = $index;
}

if (['first', 'second'] !== $received) {
    // $received is ['first']
    echo 'Failed' . PHP_EOL . PHP_EOL . 'Received:' . PHP_EOL . print_r($received, true);

    exit(1);
}

echo 'Success';

Possible Solution
Iterate over attributes here, instead picking first

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

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0