Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] add explanation for attribute option in factories.rst #21108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions service_container/factories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' => '[email protected]'], factory: [null, 'createNewsletterManager'])]
class NewsletterManager
{
}

.. code-block:: yaml

# config/services.yaml
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down