From f8fc3d60b47bcf0ed34def8d80fb488660ac4bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vasseur?= Date: Wed, 16 Feb 2022 12:57:36 +0100 Subject: [PATCH] Add documentation for using expressions as service factories --- service_container/expression_language.rst | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/service_container/expression_language.rst b/service_container/expression_language.rst index 972d7286c88..a9d5705c5b9 100644 --- a/service_container/expression_language.rst +++ b/service_container/expression_language.rst @@ -125,3 +125,54 @@ via a ``container`` variable. Here's another example: Expressions can be used in ``arguments``, ``properties``, as arguments with ``configurator`` and as arguments to ``calls`` (method calls). + +You can also use expressions as service factories: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + App\Mailer: + factory: "@=parameter('some_param') ? service('some_service') : arg(0)" + arguments: + - '@some_other_service' + + .. code-block:: xml + + + + + + + + + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + use App\Mailer; + + return function(ContainerConfigurator $configurator) { + $services = $configurator->services(); + + $services->set(Mailer::class) + ->factory(expr("parameter('some_param') ? service('some_service') : arg(0)")) + ->args([service('some_other_service')]); + }; + +In this context, you have access to the ``arg`` function that allows getting the value of arguments passed to the factory. + +.. versionadded:: 6.1 + + Using expressions as factories was introduced in Symfony 6.1.