From 2d7709f6056dfad3bff8a3747b32c7507c1371fd Mon Sep 17 00:00:00 2001 From: Zan Baldwin Date: Tue, 19 Feb 2019 15:58:00 +0000 Subject: [PATCH] [DependencyInjection] Invokable Factory Services Document new invokable factories (and configurators) implemented in symfony/symfony#30255. --- service_container/configurators.rst | 72 +++++++++++++++++++++++++++++ service_container/factories.rst | 70 ++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/service_container/configurators.rst b/service_container/configurators.rst index b8b1a28f742..921c751909e 100644 --- a/service_container/configurators.rst +++ b/service_container/configurators.rst @@ -195,6 +195,78 @@ the service id and the method name: # old syntax configurator: ['@App\Mail\EmailConfigurator', configure] +.. _configurators-invokable: + +.. versionadded:: 4.3 + + Invokable configurators for services were introduced in Symfony 4.3. + +Services can be configured via invokable configurators (replacing the +``configure()`` method with ``__invoke()``) by omitting the method name, just as +route definitions can reference :ref:`invokable +controllers `. + +.. code-block:: yaml + + # app/config/services.yml + services: + # ... + + # Registers all 4 classes as services, including AppBundle\Mail\EmailConfigurator + AppBundle\: + resource: '../../src/AppBundle/*' + # ... + + # override the services to set the configurator + AppBundle\Mail\NewsletterManager: + configurator: '@AppBundle\Mail\EmailConfigurator' + + AppBundle\Mail\GreetingCardManager: + configurator: '@AppBundle\Mail\EmailConfigurator' + +.. code-block:: xml + + + + + + + + + + + + + + + + + + +.. code-block:: php + + // app/config/services.php + use AppBundle\Mail\GreetingCardManager; + use AppBundle\Mail\NewsletterManager; + use Symfony\Component\DependencyInjection\Definition; + use Symfony\Component\DependencyInjection\Reference; + + // Same as before + $definition = new Definition(); + + $definition->setAutowired(true); + + $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*'); + + $container->getDefinition(NewsletterManager::class) + ->setConfigurator(new Reference(EmailConfigurator::class)); + + $container->getDefinition(GreetingCardManager::class) + ->setConfigurator(new Reference(EmailConfigurator::class)); + That's it! When requesting the ``App\Mail\NewsletterManager`` or ``App\Mail\GreetingCardManager`` service, the created instance will first be passed to the ``EmailConfigurator::configure()`` method. diff --git a/service_container/factories.rst b/service_container/factories.rst index 39524ccc80c..37a30f29e21 100644 --- a/service_container/factories.rst +++ b/service_container/factories.rst @@ -157,6 +157,76 @@ Configuration of the service container then looks like this: # old syntax factory: ['@App\Email\NewsletterManagerFactory', createNewsletterManager] +.. _factories-invokable: + +Suppose you now change your factory method to ``__invoke()`` so that your +factory service can be used as a callback:: + + class InvokableNewsletterManagerFactory + { + public function __invoke() + { + $newsletterManager = new NewsletterManager(); + + // ... + + return $newsletterManager; + } + } + +.. versionadded:: 4.3 + + Invokable factories for services were introduced in Symfony 4.3. + +Services can be created and configured via invokable factories by omitting the +method name, just as route definitions can reference :ref:`invokable +controllers `. + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/services.yml + + services: + # ... + + AppBundle\Email\NewsletterManager: + class: AppBundle\Email\NewsletterManager + factory: '@AppBundle\Email\NewsletterManagerFactory' + + .. code-block:: xml + + + + + + + + + + + + + + + + .. code-block:: php + + // app/config/services.php + + use AppBundle\Email\NewsletterManager; + use AppBundle\Email\NewsletterManagerFactory; + use Symfony\Component\DependencyInjection\Reference; + + // ... + $container->register(NewsletterManager::class, NewsletterManager::class) + ->setFactory(new Reference(NewsletterManagerFactory::class)); + .. _factories-passing-arguments-factory-method: Passing Arguments to the Factory Method 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