Skip to content

Commit a5f99d8

Browse files
hvtxabbuh
authored andcommitted
More clear description of factory service creation
1 parent 2259cc6 commit a5f99d8

File tree

1 file changed

+78
-27
lines changed

1 file changed

+78
-27
lines changed

service_container/factories.rst

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ instantiating the class.
1818
syntax for factories prior to 2.6.
1919

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

23-
class NewsletterManagerFactory
23+
class NewsletterManagerStaticFactory
2424
{
2525
public static function createNewsletterManager()
2626
{
@@ -34,45 +34,98 @@ object::
3434

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

3939
.. configuration-block::
4040

4141
.. code-block:: yaml
4242
43+
# app/config/services.yml
44+
4345
services:
4446
app.newsletter_manager:
4547
class: AppBundle\Email\NewsletterManager
46-
# call a static method
47-
factory: ['AppBundle\Email\NewsletterManager', create]
48+
# call the static method
49+
factory: ['AppBundle\Email\NewsletterManagerStaticFactory', createNewsletterManager]
50+
51+
.. code-block:: xml
52+
53+
<!-- app/config/services.xml -->
54+
55+
<?xml version="1.0" encoding="UTF-8" ?>
56+
<container xmlns="http://symfony.com/schema/dic/services"
57+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
58+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
59+
60+
<services>
61+
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
62+
<!-- call the static method -->
63+
<factory class="AppBundle\Email\NewsletterManagerStaticFactory" method="createNewsletterManager" />
64+
</service>
65+
</services>
66+
</container>
67+
68+
.. code-block:: php
69+
70+
// app/config/services.php
71+
72+
use AppBundle\Email\NewsletterManager;
73+
use AppBundle\Email\NewsletterManagerStaticFactory;
74+
use Symfony\Component\DependencyInjection\Definition;
75+
// ...
76+
77+
$definition = new Definition(NewsletterManager::class);
78+
// call the static method
79+
$definition->setFactory(array(NewsletterManagerStaticFactory::class, 'createNewsletterManager'));
80+
81+
$container->setDefinition('app.newsletter_manager', $definition);
82+
83+
.. note::
84+
85+
When using a factory to create services, the value chosen for the ``class``
86+
option has no effect on the resulting service. The actual class name
87+
only depends on the object that is returned by the factory. However,
88+
the configured class name may be used by compiler passes and therefore
89+
should be set to a sensible value.
90+
91+
If your factory is not using a static function to configure and create your
92+
service, but a regular method, you can instantiate the factory itself as a
93+
service too. Later, in the ":ref:`factories-passing-arguments-factory-method`"
94+
section, you learn how you can inject arguments in this method.
95+
96+
Configuration of the service container then looks like this:
97+
98+
.. configuration-block::
99+
100+
.. code-block:: yaml
48101
102+
# app/config/services.yml
103+
104+
services:
49105
app.newsletter_manager_factory:
50106
class: AppBundle\Email\NewsletterManagerFactory
51107
52108
app.newsletter_manager:
53109
class: AppBundle\Email\NewsletterManager
54-
# call a method on the specified service
110+
# call a method on the specified factory service
55111
factory: 'app.newsletter_manager_factory:createNewsletterManager'
56112
57113
.. code-block:: xml
58114
115+
<!-- app/config/services.xml -->
116+
59117
<?xml version="1.0" encoding="UTF-8" ?>
60118
<container xmlns="http://symfony.com/schema/dic/services"
61119
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
62120
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
63121
64122
<services>
65-
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
66-
<!-- call a static method -->
67-
<factory class="AppBundle\Email\NewsletterManager" method="create" />
68-
</service>
69-
70123
<service id="app.newsletter_manager_factory"
71124
class="AppBundle\Email\NewsletterManagerFactory"
72125
/>
73126
74127
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
75-
<!-- call a method on the specified service -->
128+
<!-- call a method on the specified factory service -->
76129
<factory service="app.newsletter_manager_factory"
77130
method="createNewsletterManager"
78131
/>
@@ -82,50 +135,42 @@ configure the service container to use the
82135
83136
.. code-block:: php
84137
138+
// app/config/services.php
139+
85140
use AppBundle\Email\NewsletterManager;
86141
use AppBundle\Email\NewsletterManagerFactory;
87142
use Symfony\Component\DependencyInjection\Definition;
88143
// ...
89144
90-
$definition = new Definition(NewsletterManager::class);
91-
// call a static method
92-
$definition->setFactory(array(NewsletterManager::class, 'create'));
93-
94-
$container->setDefinition('app.newsletter_manager', $definition);
95-
96145
$container->register('app.newsletter_manager_factory', NewsletterManagerFactory::class);
97146
98147
$newsletterManager = new Definition(NewsletterManager::class);
99148
100-
// call a method on the specified service
149+
// call a method on the specified factory service
101150
$newsletterManager->setFactory(array(
102151
new Reference('app.newsletter_manager_factory'),
103152
'createNewsletterManager'
104153
));
105154
106155
$container->setDefinition('app.newsletter_manager', $newsletterManager);
107156
108-
.. note::
109-
110-
When using a factory to create services, the value chosen for the ``class``
111-
option has no effect on the resulting service. The actual class name
112-
only depends on the object that is returned by the factory. However,
113-
the configured class name may be used by compiler passes and therefore
114-
should be set to a sensible value.
115-
116157
.. note::
117158

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

121162
.. code-block:: yaml
122163
164+
# app/config/services.yml
165+
123166
app.newsletter_manager:
124167
# new syntax
125168
factory: 'app.newsletter_manager_factory:createNewsletterManager'
126169
# old syntax
127170
factory: ['@app.newsletter_manager_factory', createNewsletterManager]
128171
172+
.. _factories-passing-arguments-factory-method:
173+
129174
Passing Arguments to the Factory Method
130175
---------------------------------------
131176

@@ -137,6 +182,8 @@ method in the previous example takes the ``templating`` service as an argument:
137182

138183
.. code-block:: yaml
139184
185+
# app/config/services.yml
186+
140187
services:
141188
# ...
142189
@@ -147,6 +194,8 @@ method in the previous example takes the ``templating`` service as an argument:
147194
148195
.. code-block:: xml
149196
197+
<!-- app/config/services.xml -->
198+
150199
<?xml version="1.0" encoding="UTF-8" ?>
151200
<container xmlns="http://symfony.com/schema/dic/services"
152201
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -164,6 +213,8 @@ method in the previous example takes the ``templating`` service as an argument:
164213
165214
.. code-block:: php
166215
216+
// app/config/services.php
217+
167218
use AppBundle\Email\NewsletterManager;
168219
use Symfony\Component\DependencyInjection\Reference;
169220
use Symfony\Component\DependencyInjection\Definition;

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