Contributed by Nicolas Grekas in #45512.
The service container provided by the DependencyInjection component allows you to configure the creation of objects. However, sometimes you need to apply the factory design pattern to delegate the object creation to some special object called "the factory". In Symfony 6.1 we're improving the service container to allow you to use expressions as service factories. This can help you in advanced cases such as selecting the factory based on the value of an environment variable. The syntax to use depends on the configuration format: in YAML: factory: '@=service("foo").bar()' in XML: in PHP: ->factory(expr('service("foo").bar()')) The following example shows how to select the factory to use based on the value of a configuration parameter:
# config/services.yaml
services: App\Mailer: factory: "@=parameter('some_param') ? service('some_service') : arg(0)" arguments:
- '@some_other_service'
The arg() function returns the value of arguments passed to the factory (e.g. arg(0) returns the first argument, arg(1) returns the second argument, etc.)
Sponsor the Symfony project.
Chcete-li přidat komentář, přihlaste se
Ostatní příspěvky v této skupině

Contributed by Hubert Lenoir in

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:
June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Contributed by Kévin Dunglas in

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:
June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:
June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe