Skip to content

Commit 7a36eee

Browse files
committed
[DependencyInjection] add explanation for attribute option in factories.rst
1 parent 8059765 commit 7a36eee

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

service_container/factories.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ create its object:
3939

4040
.. configuration-block::
4141

42+
.. code-block:: php-attributes
43+
44+
// src/Email/NewsletterManager.php
45+
namespace App\Email;
46+
47+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
48+
49+
// the first argument is the class and the second argument is the static method
50+
#[Autoconfigure(factory: ['App\Email\NewsletterManagerStaticFactory', 'createNewsletterManager'])]
51+
class NewsletterManager
52+
{
53+
}
54+
4255
.. code-block:: yaml
4356
4457
# config/services.yaml
@@ -121,6 +134,18 @@ You can omit the class on the factory declaration:
121134

122135
.. configuration-block::
123136

137+
.. code-block:: php-attributes
138+
139+
// src/Email/NewsletterManager.php
140+
namespace App\Email;
141+
142+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
143+
144+
#[Autoconfigure(bind: ['$sender' => 'fabien@symfony.com'], factory: [null, 'createNewsletterManager'])]
145+
class NewsletterManager
146+
{
147+
}
148+
124149
.. code-block:: yaml
125150
126151
# config/services.yaml
@@ -239,6 +264,21 @@ Configuration of the service container then looks like this:
239264

240265
.. configuration-block::
241266

267+
.. code-block:: php-attributes
268+
269+
// src/Email/NewsletterManager.php
270+
namespace App\Email;
271+
272+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
273+
274+
// it's necessary to create a service for the factory
275+
// use the factory service as the first argument of the 'factory'
276+
// option and the factory method as the second argument
277+
#[Autoconfigure(factory: ['@App\Email\NewsletterManagerFactory', 'createNewsletterManager'])]
278+
class NewsletterManager
279+
{
280+
}
281+
242282
.. code-block:: yaml
243283
244284
# config/services.yaml
@@ -325,6 +365,18 @@ method name:
325365

326366
.. configuration-block::
327367

368+
.. code-block:: php-attributes
369+
370+
// src/Email/NewsletterManager.php
371+
namespace App\Email;
372+
373+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
374+
375+
#[Autoconfigure(factory: '@App\Email\InvokableNewsletterManagerFactory')]
376+
class NewsletterManager
377+
{
378+
}
379+
328380
.. code-block:: yaml
329381
330382
# config/services.yaml
@@ -378,6 +430,20 @@ e.g. change the service based on a parameter:
378430

379431
.. configuration-block::
380432

433+
.. code-block:: php-attributes
434+
435+
// src/Email/NewsletterManager.php
436+
namespace App\Email;
437+
438+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
439+
440+
// use the "tracable_newsletter" service when debug is enabled, "newsletter" otherwise.
441+
// "@=" indicates that this is an expression
442+
#[Autoconfigure(factory: '@=parameter("kernel.debug") ? service("tracable_newsletter") : service("newsletter")')]
443+
interface NewsletterManagerInterface
444+
{
445+
}
446+
381447
.. code-block:: yaml
382448
383449
# config/services.yaml
@@ -457,6 +523,18 @@ previous examples takes the ``templating`` service as an argument:
457523

458524
.. configuration-block::
459525

526+
.. code-block:: php-attributes
527+
528+
// src/Email/NewsletterManager.php
529+
namespace App\Email;
530+
531+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
532+
533+
#[Autoconfigure(bind: ['$template', '@templating'], factory: ['@App\Email\NewsletterManagerFactory', 'createNewsletterManager'])]
534+
class NewsletterManager
535+
{
536+
}
537+
460538
.. code-block:: yaml
461539
462540
# config/services.yaml

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