Commit e33714b
committed
bug #42019 [DependencyInjection] Fix TaggedLocator attribute without indexAttribute argument (Lucas Bäuerle)
This PR was merged into the 5.3 branch.
Discussion
----------
[DependencyInjection] Fix TaggedLocator attribute without indexAttribute argument
| 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
```php
public function __construct(
#[TaggedLocator('app.tag')]
ServiceLocator $serviceLocator
) {
// ...
}
```
### Expected
```php
$serviceLocator->getProvidedServices();
// [
// "App\Service1" => "..."
// "App\Service2" => "..."
// ]
$serviceLocator->has(App\Service1::class);
// true
$serviceLocator->get(App\Service1::class);
// Instance of App\Service1
```
### Actual
```php
$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:
```php
// 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);
```
Commits
-------
0499fff [DependencyInjection] Fix TaggedLocator attribute without index argumentFile tree
3 files changed
+60
-1
lines changed- src/Symfony/Component/DependencyInjection
- Compiler
- Tests
- Compiler
- Fixtures
3 files changed
+60
-1
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
221 | | - | |
| 221 | + | |
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
| |||
Lines changed: 30 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
403 | 404 | | |
404 | 405 | | |
405 | 406 | | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
406 | 436 | | |
407 | 437 | | |
408 | 438 | | |
| |||
Lines changed: 29 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
0 commit comments