Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 154d667

Browse files
committed
[#16516] Move section to service_container/factories
1 parent 2b58adb commit 154d667

File tree

2 files changed

+79
-49
lines changed

2 files changed

+79
-49
lines changed

service_container/expression_language.rst

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -130,55 +130,9 @@ via a ``container`` variable. Here's another example:
130130
};
131131
132132
Expressions can be used in ``arguments``, ``properties``, as arguments with
133-
``configurator`` and as arguments to ``calls`` (method calls).
134-
135-
You can also use expressions as service factories:
136-
137-
.. configuration-block::
138-
139-
.. code-block:: yaml
140-
141-
# config/services.yaml
142-
services:
143-
App\Mailer:
144-
factory: "@=parameter('some_param') ? service('some_service') : arg(0)"
145-
arguments:
146-
- '@some_other_service'
147-
148-
.. code-block:: xml
149-
150-
<!-- config/services.xml -->
151-
<?xml version="1.0" encoding="UTF-8" ?>
152-
<container xmlns="http://symfony.com/schema/dic/services"
153-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
154-
xsi:schemaLocation="http://symfony.com/schema/dic/services
155-
https://symfony.com/schema/dic/services/services-1.0.xsd">
156-
157-
<services>
158-
<service id="App\Mailer">
159-
<factory expression="parameter('some_param') ? service('some_service') : arg(0)"/>
160-
<argument type="service" id="some_other_service"/>
161-
</service>
162-
</services>
163-
</container>
164-
165-
.. code-block:: php
166-
167-
// config/services.php
168-
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
169-
170-
use App\Mailer;
171-
172-
return function(ContainerConfigurator $configurator) {
173-
$services = $configurator->services();
174-
175-
$services->set(Mailer::class)
176-
->factory(expr("parameter('some_param') ? service('some_service') : arg(0)"))
177-
->args([service('some_other_service')]);
178-
};
179-
180-
In this context, you have access to the ``arg`` function that allows getting the value of arguments passed to the factory.
133+
``configurator``, as arguments to ``calls`` (method calls) and in
134+
``factories`` (:doc:`service factories </service_container/factories>`).
181135

182136
.. versionadded:: 6.1
183137

184-
Using expressions as factories was introduced in Symfony 6.1.
138+
Using expressions in ``factories`` was introduced in Symfony 6.1.

service_container/factories.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,82 @@ method name:
239239
->factory(service(InvokableNewsletterManagerFactory::class));
240240
};
241241
242+
Using Expressions in Email Factories
243+
--------------------------------------
244+
245+
.. versionadded:: 6.1
246+
247+
Using expressions as factories was introduced in Symfony 6.1.
248+
249+
Instead of using PHP classes as a factory, you can also use
250+
:doc:`expressions </service_container/expressions>`. This allows you to
251+
e.g. change the service based on a parameter:
252+
253+
.. configuration-block::
254+
255+
.. code-block:: yaml
256+
257+
# config/services.yaml
258+
services:
259+
App\Email\NewsletterManagerInterface:
260+
# use the "tracable_newsletter" service when debug is enabled, "newsletter" otherwise.
261+
# "@=" indicates that this is an expression
262+
factory: '@=parameter("kernel.debug") ? service("tracable_newsletter") : service("newsletter")'
263+
264+
# you can use the arg() function to retrieve an argument from the definition
265+
App\Email\NewsletterManagerInterface:
266+
factory: "@=arg(0).createNewsletterManager() ?: service("default_newsletter_manager")"
267+
arguments:
268+
- '@App\Email\NewsletterManagerFactory'
269+
270+
.. code-block:: xml
271+
272+
<!-- config/services.xml -->
273+
<?xml version="1.0" encoding="UTF-8" ?>
274+
<container xmlns="http://symfony.com/schema/dic/services"
275+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
276+
xsi:schemaLocation="http://symfony.com/schema/dic/services
277+
https://symfony.com/schema/dic/services/services-1.0.xsd">
278+
279+
<services>
280+
<service id="App\Email\NewsletterManagerInterface">
281+
<!-- use the "tracable_newsletter" service when debug is enabled, "newsletter" otherwise -->
282+
<factory expression="parameter('kernel.debug') ? service('tracable_newsletter') : service('newsletter')"/>
283+
</service>
284+
285+
<!-- you can use the arg() function to retrieve an argument from the definition -->
286+
<service id="App\Email\NewsletterManagerInterface">
287+
<factory expression="arg(0).createNewsletterManager() ?: service("default_newsletter_manager")"/>
288+
<argument type="service" id="App\Email\NewsletterManagerFactory"/>
289+
</service>
290+
</services>
291+
</container>
292+
293+
.. code-block:: php
294+
295+
// config/services.php
296+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
297+
298+
use App\Email\NewsletterManagerInterface;
299+
use App\Email\NewsletterManagerFactory;
300+
301+
return function(ContainerConfigurator $configurator) {
302+
$services = $configurator->services();
303+
304+
$services->set(NewsletterManagerInterface::class)
305+
// use the "tracable_newsletter" service when debug is enabled, "newsletter" otherwise.
306+
->factory(expr("parameter('kernel.debug') ? service('tracable_newsletter') : service('newsletter')"))
307+
;
308+
309+
// you can use the arg() function to retrieve an argument from the definition
310+
$services->set(NewsletterManagerInterface::class)
311+
->factory(expr("arg(0).createNewsletterManager() ?: service('default_newsletter_manager')"))
312+
->args([
313+
service(NewsletterManagerFactory::class),
314+
])
315+
;
316+
};
317+
242318
.. _factories-passing-arguments-factory-method:
243319

244320
Passing Arguments to the Factory Method

0 commit comments

Comments
 (0)