Skip to content

Commit e686ded

Browse files
committed
minor #11014 [DependencyInjection] Invokable Factory Services (zanbaldwin)
This PR was merged into the master branch. Discussion ---------- [DependencyInjection] Invokable Factory Services Document new invokable factories (and configurators) implemented in symfony/symfony#30255. There are now four ways to reference factories, perhaps making them more clear might be something else to do. - `function` - `Class::method` - `Service:method` - `@InvokableService` Commits ------- 2d7709f [DependencyInjection] Invokable Factory Services
2 parents 6954e8d + 2d7709f commit e686ded

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

service_container/configurators.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,78 @@ the service id and the method name:
195195
# old syntax
196196
configurator: ['@App\Mail\EmailConfigurator', configure]
197197
198+
.. _configurators-invokable:
199+
200+
.. versionadded:: 4.3
201+
202+
Invokable configurators for services were introduced in Symfony 4.3.
203+
204+
Services can be configured via invokable configurators (replacing the
205+
``configure()`` method with ``__invoke()``) by omitting the method name, just as
206+
route definitions can reference :ref:`invokable
207+
controllers <controller-service-invoke>`.
208+
209+
.. code-block:: yaml
210+
211+
# app/config/services.yml
212+
services:
213+
# ...
214+
215+
# Registers all 4 classes as services, including AppBundle\Mail\EmailConfigurator
216+
AppBundle\:
217+
resource: '../../src/AppBundle/*'
218+
# ...
219+
220+
# override the services to set the configurator
221+
AppBundle\Mail\NewsletterManager:
222+
configurator: '@AppBundle\Mail\EmailConfigurator'
223+
224+
AppBundle\Mail\GreetingCardManager:
225+
configurator: '@AppBundle\Mail\EmailConfigurator'
226+
227+
.. code-block:: xml
228+
229+
<!-- app/config/services.xml -->
230+
<?xml version="1.0" encoding="UTF-8" ?>
231+
<container xmlns="http://symfony.com/schema/dic/services"
232+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
233+
xsi:schemaLocation="http://symfony.com/schema/dic/services
234+
http://symfony.com/schema/dic/services/services-1.0.xsd">
235+
236+
<services>
237+
<prototype namespace="AppBundle\" resource="../../src/AppBundle/*" />
238+
239+
<service id="AppBundle\Mail\NewsletterManager">
240+
<configurator service="AppBundle\Mail\EmailConfigurator" />
241+
</service>
242+
243+
<service id="AppBundle\Mail\GreetingCardManager">
244+
<configurator service="AppBundle\Mail\EmailConfigurator" />
245+
</service>
246+
</services>
247+
</container>
248+
249+
.. code-block:: php
250+
251+
// app/config/services.php
252+
use AppBundle\Mail\GreetingCardManager;
253+
use AppBundle\Mail\NewsletterManager;
254+
use Symfony\Component\DependencyInjection\Definition;
255+
use Symfony\Component\DependencyInjection\Reference;
256+
257+
// Same as before
258+
$definition = new Definition();
259+
260+
$definition->setAutowired(true);
261+
262+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*');
263+
264+
$container->getDefinition(NewsletterManager::class)
265+
->setConfigurator(new Reference(EmailConfigurator::class));
266+
267+
$container->getDefinition(GreetingCardManager::class)
268+
->setConfigurator(new Reference(EmailConfigurator::class));
269+
198270
That's it! When requesting the ``App\Mail\NewsletterManager`` or
199271
``App\Mail\GreetingCardManager`` service, the created instance will first be
200272
passed to the ``EmailConfigurator::configure()`` method.

service_container/factories.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,76 @@ Configuration of the service container then looks like this:
158158
# old syntax
159159
factory: ['@App\Email\NewsletterManagerFactory', createNewsletterManager]
160160
161+
.. _factories-invokable:
162+
163+
Suppose you now change your factory method to ``__invoke()`` so that your
164+
factory service can be used as a callback::
165+
166+
class InvokableNewsletterManagerFactory
167+
{
168+
public function __invoke()
169+
{
170+
$newsletterManager = new NewsletterManager();
171+
172+
// ...
173+
174+
return $newsletterManager;
175+
}
176+
}
177+
178+
.. versionadded:: 4.3
179+
180+
Invokable factories for services were introduced in Symfony 4.3.
181+
182+
Services can be created and configured via invokable factories by omitting the
183+
method name, just as route definitions can reference :ref:`invokable
184+
controllers <controller-service-invoke>`.
185+
186+
.. configuration-block::
187+
188+
.. code-block:: yaml
189+
190+
# app/config/services.yml
191+
192+
services:
193+
# ...
194+
195+
AppBundle\Email\NewsletterManager:
196+
class: AppBundle\Email\NewsletterManager
197+
factory: '@AppBundle\Email\NewsletterManagerFactory'
198+
199+
.. code-block:: xml
200+
201+
<!-- app/config/services.xml -->
202+
203+
<?xml version="1.0" encoding="UTF-8" ?>
204+
<container xmlns="http://symfony.com/schema/dic/services"
205+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
206+
xsi:schemaLocation="http://symfony.com/schema/dic/services
207+
http://symfony.com/schema/dic/services/services-1.0.xsd">
208+
209+
<services>
210+
<!-- ... -->
211+
212+
<service id="AppBundle\Email\NewsletterManager"
213+
class="AppBundle\Email\NewsletterManager">
214+
<factory service="AppBundle\Email\NewsletterManagerFactory" />
215+
</service>
216+
</services>
217+
</container>
218+
219+
.. code-block:: php
220+
221+
// app/config/services.php
222+
223+
use AppBundle\Email\NewsletterManager;
224+
use AppBundle\Email\NewsletterManagerFactory;
225+
use Symfony\Component\DependencyInjection\Reference;
226+
227+
// ...
228+
$container->register(NewsletterManager::class, NewsletterManager::class)
229+
->setFactory(new Reference(NewsletterManagerFactory::class));
230+
161231
.. _factories-passing-arguments-factory-method:
162232

163233
Passing Arguments to the Factory Method

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy