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