Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[DependencyInjection] Update syntax for#[AutowireLocator] and add#[AutowireIterator]#18993
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsreference/attributes.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
52 changes: 37 additions & 15 deletionsservice_container/service_subscribers_locators.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -298,9 +298,10 @@ This is done by having ``getSubscribedServices()`` return an array of | ||
| The above example requires using ``3.2`` version or newer of ``symfony/service-contracts``. | ||
| .. _service-locator_autowire-locator: | ||
| .. _service-locator_autowire-iterator: | ||
| The AutowireLocatorand AutowireIterator Attributes | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| Another way to define a service locator is to use the | ||
| :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` | ||
| @@ -317,26 +318,31 @@ attribute:: | ||
| class CommandBus | ||
| { | ||
| public function __construct( | ||
| #[AutowireLocator([ | ||
| FooHandler::class, | ||
| BarHandler::class, | ||
| ])] | ||
| private ContainerInterface $handlers, | ||
| ) { | ||
| } | ||
| public function handle(Command $command): mixed | ||
| { | ||
| $commandClass = get_class($command); | ||
| if ($this->handlers->has($commandClass)) { | ||
| $handler = $this->handlers->get($commandClass); | ||
| return $handler->handle($command); | ||
| } | ||
| } | ||
| } | ||
| Just like with the ``getSubscribedServices()`` method, it is possible | ||
| to define aliased services thanks to the array keys, as well as optional | ||
| services, plus you can nest it with | ||
| :class:`Symfony\\Contracts\\Service\\Attribute\\SubscribedService` | ||
| attribute:: | ||
| // src/CommandBus.php | ||
| namespace App; | ||
| @@ -345,23 +351,25 @@ services:: | ||
| use App\CommandHandler\BazHandler; | ||
| use App\CommandHandler\FooHandler; | ||
| use Psr\Container\ContainerInterface; | ||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
| use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; | ||
OskarStark marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
| class CommandBus | ||
| { | ||
| public function __construct( | ||
| #[AutowireLocator([ | ||
| 'foo' => FooHandler::class, | ||
| 'bar' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')), | ||
| 'optionalBaz' =>'?'.BazHandler::class, | ||
| ])] | ||
| private ContainerInterface $handlers, | ||
| ) { | ||
| } | ||
| public function handle(Command $command): mixed | ||
| { | ||
| $fooHandler = $this->handlers->get('foo'); | ||
| // ... | ||
| } | ||
| @@ -373,6 +381,20 @@ services:: | ||
| :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` | ||
| attribute was introduced in Symfony 6.4. | ||
| .. note:: | ||
| To receive an iterable instead of a service locator, you can switch the | ||
| :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` | ||
| attribute to | ||
| :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireIterator` | ||
| attribute. | ||
| .. versionadded:: 6.4 | ||
| The | ||
| :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireIterator` | ||
| attribute was introduced in Symfony 6.4. | ||
| .. _service-subscribers-locators_defining-service-locator: | ||
| Defining a Service Locator | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.