[DependencyInjection] Add ability to define an index for service in an injected service locator argument#30348
Conversation
15453f1 to
81739dc
Compare
81739dc to
4cdcff7
Compare
src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php
Outdated
Show resolved
Hide resolved
nicolas-grekas
left a comment
There was a problem hiding this comment.
(once @stof's comment is addressed)
|
Is there any possibility to index by a class name? |
|
@vudaltsov sure: implement getDefautKeyName and make it return self::class. |
|
That will be a new static method in the interface which all tagged services implement... |
|
@vudaltsov then make all such classes extends some base class, and make the method of that class return static::class. That's almost what is done right now for form types. |
|
@XuruDragon , what about |
4cc6c2c to
9618752
Compare
|
@vudaltsov thanks for facts checking :) Indexing by FQCN is now implemented as a fallback when the attribute or the method are either not defined in the tag or not found on the service. |
cbfbe0e to
1ae543b
Compare
Signed-off-by: Anthony MARTIN <[email protected]>
1ae543b to
cb3c56b
Compare
|
branch has been rebase on up-to-date master |
|
Thank you @XuruDragon. |
…or service in an injected service locator argument (XuruDragon, nicolas-grekas) This PR was merged into the 4.3-dev branch. Discussion ---------- [DependencyInjection] Add ability to define an index for service in an injected service locator argument | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | in progress / symfony/symfony-docs#... It's more or less the same thing then the PR #30257 but for a service locator argument Add a simple way to specify an index based on a tag attribute to simplify retrieving a specific service when injecting a service locator as argument into services, but also a way to fallback to a static method on the service class. Yaml: ```yaml services: foo_service: class: Foo tags: - {name: foo_tag, key: foo_service} foo_service_tagged: class: Bar arguments: - !tagged_locator tag: 'foo_tag' index_by: 'key' default_index_method: 'static_method' ``` XML: ```xml <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="foo" class="Foo"> <tag name="foo_tag" key="foo_service" /> </service> <service id="foo_tagged_iterator" class="Bar" public="true"> <argument type="tagged_locator" tag="foo_tag" index-by="key" default-index-method="static_method" /> </service> </services> </container> ``` PHP: ```php // config/services.php use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; $container->register(Foo::class) ->addTag('foo_tag', ['key' => 'foo_service']); $container->register(App\Handler\HandlerCollection::class) // inject all services tagged with app.handler as first argument ->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('app.handler', 'key'))); ``` Usage: ```php // src/Handler/HandlerCollection.php namespace App\Handler; use Symfony\Component\DependencyInjection\ServiceLocator; class HandlerCollection { public function __construct(ServiceLocator $serviceLocator) { $foo = $serviceLocator->get('foo_service'): } } ``` Tasks * [x] Support PHP loader/dumper * [x] Support YAML loader/dumper * [x] Support XML loader/dumper (and update XSD too) * [x] Add tests * [x] Documentation Commits ------- cb3c56b Support indexing tagged locators by FQCN as fallback 250a2c8 [DI] Allow tagged_locator tag to be used as an argument
…or service locators (Anthony MARTIN) This PR was submitted for the master branch but it was merged into the 4.3 branch instead (closes #11042). Discussion ---------- [DependencyInjection] Doc for Allow to choose an index for service locators Here is the doc for the new feature implemented in : [symfony/symfony#30348](symfony/symfony#30348) that follow the feature [symfony/symfony#30257](symfony/symfony#30257) Commits ------- d63c298 [DependencyInjection] Doc for Allow to choose an index for service locator collection
It's more or less the same thing then the PR #30257 but for a service locator argument
Add a simple way to specify an index based on a tag attribute to simplify retrieving a specific service when injecting a service locator as argument into services, but also a way to fallback to a static method on the service class.
Yaml:
XML:
PHP:
Usage:
Tasks