Skip to content

More clear description of factory service creation #7377

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

Closed
wants to merge 1 commit into from
Closed
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
105 changes: 78 additions & 27 deletions service_container/factories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ the service container to call a method on the factory rather than directly
instantiating the class.

Suppose you have a factory that configures and returns a new ``NewsletterManager``
object::
object by calling the static ``createNewsletterManager()`` method::

class NewsletterManagerFactory
class NewsletterManagerStaticFactory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class name looks ugly ... but at the same time it's perfectly understandable. I'm divided about it 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly my thought, I'm happy to change it either way ;].

{
public static function createNewsletterManager()
{
Expand All @@ -29,45 +29,98 @@ object::

To make the ``NewsletterManager`` object available as a service, you can
configure the service container to use the
``NewsletterManagerFactory::createNewsletterManager()`` factory method:
``NewsletterManagerStaticFactory::createNewsletterManager()`` factory method:

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml

services:
app.newsletter_manager:
class: AppBundle\Email\NewsletterManager
# call a static method
factory: ['AppBundle\Email\NewsletterManager', create]
# call the static method
factory: ['AppBundle\Email\NewsletterManagerStaticFactory', createNewsletterManager]

.. code-block:: xml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a comment containing the filename like this:

<!-- app/config/services.xml -->

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add this for the other formats too (using config.yml and config.php respectively)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done this. Am however wondering whether it's not app/config/services.yml and app/config/services.php to be exactly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, you are absolutely right. Sorry for the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries ;].

<!-- app/config/services.xml -->

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
<!-- call the static method -->
<factory class="AppBundle\Email\NewsletterManagerStaticFactory" method="createNewsletterManager" />
</service>
</services>
</container>

.. code-block:: php

// app/config/services.php

use AppBundle\Email\NewsletterManager;
use AppBundle\Email\NewsletterManagerStaticFactory;
use Symfony\Component\DependencyInjection\Definition;
// ...

$definition = new Definition(NewsletterManager::class);
// call the static method
$definition->setFactory(array(NewsletterManagerStaticFactory::class, 'createNewsletterManager'));

$container->setDefinition('app.newsletter_manager', $definition);

.. note::

When using a factory to create services, the value chosen for the ``class``
option has no effect on the resulting service. The actual class name
only depends on the object that is returned by the factory. However,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this:

However, the configured class name may be used by compiler passes and therefore should be set to a sensible value.

Should I open a Symfony code issue to improve this behavior and make this class optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This note was already there, don't know why it is marked as new by the diff.)

I do however believe the note is still valid, because it might be handy to use the class definition somewhere in a compiler pass.

On the other hand, it might be good to check why no active validation is done on the return type of the creation method...

the configured class name may be used by compiler passes and therefore
should be set to a sensible value.

If your factory is not using a static function to configure and create your
service, but a regular method, you can instantiate the factory itself as a
service too. Later, in the ":ref:`factories-passing-arguments-factory-method`"
section, you learn how you can inject arguments in this method.

Configuration of the service container then looks like this:

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml

services:
app.newsletter_manager_factory:
class: AppBundle\Email\NewsletterManagerFactory

app.newsletter_manager:
class: AppBundle\Email\NewsletterManager
# call a method on the specified service
# call a method on the specified factory service
factory: 'app.newsletter_manager_factory:createNewsletterManager'

.. code-block:: xml

<!-- app/config/services.xml -->

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
<!-- call a static method -->
<factory class="AppBundle\Email\NewsletterManager" method="create" />
</service>

<service id="app.newsletter_manager_factory"
class="AppBundle\Email\NewsletterManagerFactory"
/>

<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
<!-- call a method on the specified service -->
<!-- call a method on the specified factory service -->
<factory service="app.newsletter_manager_factory"
method="createNewsletterManager"
/>
Expand All @@ -77,50 +130,42 @@ configure the service container to use the

.. code-block:: php

// app/config/services.php

use AppBundle\Email\NewsletterManager;
use AppBundle\Email\NewsletterManagerFactory;
use Symfony\Component\DependencyInjection\Definition;
// ...

$definition = new Definition(NewsletterManager::class);
// call a static method
$definition->setFactory(array(NewsletterManager::class, 'create'));

$container->setDefinition('app.newsletter_manager', $definition);

$container->register('app.newsletter_manager_factory', NewsletterManagerFactory::class);

$newsletterManager = new Definition(NewsletterManager::class);

// call a method on the specified service
// call a method on the specified factory service
$newsletterManager->setFactory(array(
new Reference('app.newsletter_manager_factory'),
'createNewsletterManager'
));

$container->setDefinition('app.newsletter_manager', $newsletterManager);

.. note::

When using a factory to create services, the value chosen for the ``class``
option has no effect on the resulting service. The actual class name
only depends on the object that is returned by the factory. However,
the configured class name may be used by compiler passes and therefore
should be set to a sensible value.

.. note::

The traditional configuration syntax in YAML files used an array to define
the factory service and the method name:

.. code-block:: yaml

# app/config/services.yml

app.newsletter_manager:
# new syntax
factory: 'app.newsletter_manager_factory:createNewsletterManager'
# old syntax
factory: ['@app.newsletter_manager_factory', createNewsletterManager]

.. _factories-passing-arguments-factory-method:

Passing Arguments to the Factory Method
---------------------------------------

Expand All @@ -132,6 +177,8 @@ method in the previous example takes the ``templating`` service as an argument:

.. code-block:: yaml

# app/config/services.yml

services:
# ...

Expand All @@ -142,6 +189,8 @@ method in the previous example takes the ``templating`` service as an argument:

.. code-block:: xml

<!-- app/config/services.xml -->

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -159,6 +208,8 @@ method in the previous example takes the ``templating`` service as an argument:

.. code-block:: php

// app/config/services.php

use AppBundle\Email\NewsletterManager;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
Expand Down