From 7a36eeef94e35d8109abfbf39e4e3de0c4f5cfeb Mon Sep 17 00:00:00 2001 From: Santiago San Martin Date: Fri, 20 Jun 2025 19:25:18 -0300 Subject: [PATCH] [DependencyInjection] add explanation for attribute option in `factories.rst` --- service_container/factories.rst | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/service_container/factories.rst b/service_container/factories.rst index 0c6a4724609..9117b91f23c 100644 --- a/service_container/factories.rst +++ b/service_container/factories.rst @@ -39,6 +39,19 @@ create its object: .. configuration-block:: + .. code-block:: php-attributes + + // src/Email/NewsletterManager.php + namespace App\Email; + + use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; + + // the first argument is the class and the second argument is the static method + #[Autoconfigure(factory: ['App\Email\NewsletterManagerStaticFactory', 'createNewsletterManager'])] + class NewsletterManager + { + } + .. code-block:: yaml # config/services.yaml @@ -121,6 +134,18 @@ You can omit the class on the factory declaration: .. configuration-block:: + .. code-block:: php-attributes + + // src/Email/NewsletterManager.php + namespace App\Email; + + use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; + + #[Autoconfigure(bind: ['$sender' => 'fabien@symfony.com'], factory: [null, 'createNewsletterManager'])] + class NewsletterManager + { + } + .. code-block:: yaml # config/services.yaml @@ -239,6 +264,21 @@ Configuration of the service container then looks like this: .. configuration-block:: + .. code-block:: php-attributes + + // src/Email/NewsletterManager.php + namespace App\Email; + + use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; + + // it's necessary to create a service for the factory + // use the factory service as the first argument of the 'factory' + // option and the factory method as the second argument + #[Autoconfigure(factory: ['@App\Email\NewsletterManagerFactory', 'createNewsletterManager'])] + class NewsletterManager + { + } + .. code-block:: yaml # config/services.yaml @@ -325,6 +365,18 @@ method name: .. configuration-block:: + .. code-block:: php-attributes + + // src/Email/NewsletterManager.php + namespace App\Email; + + use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; + + #[Autoconfigure(factory: '@App\Email\InvokableNewsletterManagerFactory')] + class NewsletterManager + { + } + .. code-block:: yaml # config/services.yaml @@ -378,6 +430,20 @@ e.g. change the service based on a parameter: .. configuration-block:: + .. code-block:: php-attributes + + // src/Email/NewsletterManager.php + namespace App\Email; + + use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; + + // use the "tracable_newsletter" service when debug is enabled, "newsletter" otherwise. + // "@=" indicates that this is an expression + #[Autoconfigure(factory: '@=parameter("kernel.debug") ? service("tracable_newsletter") : service("newsletter")')] + interface NewsletterManagerInterface + { + } + .. code-block:: yaml # config/services.yaml @@ -457,6 +523,18 @@ previous examples takes the ``templating`` service as an argument: .. configuration-block:: + .. code-block:: php-attributes + + // src/Email/NewsletterManager.php + namespace App\Email; + + use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; + + #[Autoconfigure(bind: ['$template', '@templating'], factory: ['@App\Email\NewsletterManagerFactory', 'createNewsletterManager'])] + class NewsletterManager + { + } + .. code-block:: yaml # config/services.yaml