File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -125,3 +125,44 @@ via a ``container`` variable. Here's another example:
125125
126126 Expressions can be used in ``arguments ``, ``properties ``, as arguments with
127127``configurator `` and as arguments to ``calls `` (method calls).
128+
129+ You can also use expressions as factories to create services based on an expression:
130+
131+ .. configuration-block ::
132+
133+ .. code-block :: yaml
134+
135+ # config/services.yaml
136+ services :
137+ App\Mailer :
138+ factory : " @=parameter('some_param') ? service('some_service') : service('some_other_service')"
139+
140+ .. code-block :: xml
141+
142+ <!-- config/services.xml -->
143+ <?xml version =" 1.0" encoding =" UTF-8" ?>
144+ <container xmlns =" http://symfony.com/schema/dic/services"
145+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
146+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
147+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
148+
149+ <services >
150+ <service id =" App\Mailer" >
151+ <factory expression =" parameter('some_param') ? service('some_service') : service('some_other_service')" />
152+ </service >
153+ </services >
154+ </container >
155+
156+ .. code-block :: php
157+
158+ // config/services.php
159+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
160+
161+ use App\Mailer;
162+
163+ return function(ContainerConfigurator $configurator) {
164+ $services = $configurator->services();
165+
166+ $services->set(Mailer::class)
167+ ->factory(expr("parameter('some_param') ? service('some_service') : service('some_other_service')"));
168+ };
You can’t perform that action at this time.
0 commit comments