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

Appearance settings

[DependencyInjection] Fix TaggedLocator attribute without indexAttribute argument #42019

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

Merged
merged 1 commit into from
Jul 10, 2021

Conversation

lbae
Copy link
Contributor

@lbae lbae commented Jul 7, 2021

Q A
Branch? 5.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

The new TaggedLocator attribute does not index services correctly without the indexAttribute argument

Example

public function __construct(
    #[TaggedLocator('app.tag')]
    ServiceLocator $serviceLocator
) {
    // ...
}

Expected

$serviceLocator->getProvidedServices();
// [
//    "App\Service1" => "..."
//    "App\Service2" => "..."
// ]

$serviceLocator->has(App\Service1::class);
// true

$serviceLocator->get(App\Service1::class);
// Instance of App\Service1

Actual

$services = $serviceLocator->getProvidedServices();
// [
//    0 => [...]
//    1 => [...]
// ]

$serviceLocator->has(App\Service1::class);
// false

$serviceLocator->get(App\Service1::class);
// Exception

Same issue with ContainerInterface instead of ServiceLocator

Proposed solution

This fix allows services to be indexed by their fully qualified name instead of a numeric index if indexAttribute argument is not provided.

The solution is oriented towards the implemenation in the YamlFileLoader (Symfony\Component\DependencyInjection\Loader\YamlFileLoader). The YamlFileLoader fulfills the expected behaviour above:

// Symfony\Component\DependencyInjection\Loader\YamlFileLoader

// Line 850
$forLocator = 'tagged_locator' === $value->getTag();

// ...

// Line 857
$argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator, $argument['default_priority_method'] ?? null);

With this fix, services in service locators are indexed by their fully qualified name instead of a numeric index if `indexAttribute` argument is not provided
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@lbae
Copy link
Contributor Author

lbae commented Jul 7, 2021

I'm not sure about the failed coding standards test.

The test fails on the exception message of AutowiringFailedException, which was not touched in this PR

@derrabus
Copy link
Member

derrabus commented Jul 7, 2021

which was not touched in this PR

If you didn't break it, you don't have to fix it. ✌🏻

@nicolas-grekas
Copy link
Member

Good catch, thanks @lbae.

@nicolas-grekas nicolas-grekas merged commit e33714b into symfony:5.3 Jul 10, 2021
@fabpot fabpot mentioned this pull request Jul 26, 2021
svetlana-s pushed a commit to svetlana-gor/symfony-demo-app that referenced this pull request Jan 14, 2022
svetlana-s pushed a commit to svetlana-gor/symfony-demo-app that referenced this pull request Jan 14, 2022
svetlana-s pushed a commit to svetlana-gor/symfony-demo-app that referenced this pull request Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.