Skip to content

Commit 8ab3595

Browse files
committed
Moved the Factories article to the topic
1 parent 1467d3c commit 8ab3595

File tree

4 files changed

+61
-78
lines changed

4 files changed

+61
-78
lines changed

_build/redirection_map

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/cookbook/tools/autoloader /components/class_loader
1414
/cookbook/tools/finder /components/finder
1515
/cookbook/service_container/parentservices /components/dependency_injection/parentservices
16-
/cookbook/service_container/factories /components/dependency_injection/factories
16+
/cookbook/service_container/factories /service_container/factories
1717
/cookbook/service_container/tags /components/dependency_injection/tags
1818
/reference/configuration/mongodb /bundles/DoctrineMongoDBBundle/config
1919
/reference/YAML /components/yaml
@@ -251,6 +251,7 @@
251251
/components/dependency_injection/definitions /service_container/definitions
252252
/components/dependency_injection/introduction /components/dependency_injection
253253
/components/dependency_injection/index /components/dependency_injection
254+
/components/dependency_injection/factories /service_container/factories
254255
/components/event_dispatcher/introduction /components/event_dispatcher
255256
/components/expression_language/introduction /components/expression_language
256257
/components/expression_language/index /components/expression_language

service_container/definitions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ any method calls in the definitions as well::
111111

112112
There are more examples of specific ways of working with definitions
113113
in the PHP code blocks of the Service Container articles such as
114-
:doc:`/components/dependency_injection/factories` and
114+
:doc:`/service_container/factories` and
115115
:doc:`/components/dependency_injection/parentservices`.
116116

117117
.. note::

components/dependency_injection/factories.rst renamed to service_container/factories.rst

Lines changed: 57 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,18 @@ configure the service container to use the
4141
.. code-block:: yaml
4242
4343
services:
44-
newsletter_manager:
45-
class: NewsletterManager
46-
factory: [NewsletterManagerFactory, createNewsletterManager]
44+
app.newsletter_manager:
45+
class: AppBundle\Email\NewsletterManager
46+
# call a static method
47+
factory: ['AppBundle\Email\NewsletterManager', create]
48+
49+
app.newsletter_manager_factory:
50+
class: AppBundle\Email\NewsletterManagerFactory
51+
52+
app.newsletter_manager:
53+
class: AppBundle\Email\NewsletterManager
54+
# call a method on the specified service
55+
factory: ['@app.newsletter_manager_factory', createNewsletterManager]
4756
4857
.. code-block:: xml
4958
@@ -53,21 +62,46 @@ configure the service container to use the
5362
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5463
5564
<services>
56-
<service id="newsletter_manager" class="NewsletterManager">
57-
<factory class="NewsletterManagerFactory" method="createNewsletterManager" />
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+
70+
<service id="app.newsletter_manager_factory"
71+
class="AppBundle\Email\NewsletterManagerFactory"
72+
/>
73+
74+
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
75+
<!-- call a method on the specified service -->
76+
<factory service="app.newsletter_manager_factory"
77+
method="createNewsletterManager"
78+
/>
5879
</service>
5980
</services>
6081
</container>
6182
6283
.. code-block:: php
6384
6485
use Symfony\Component\DependencyInjection\Definition;
65-
6686
// ...
67-
$definition = new Definition('NewsletterManager');
68-
$definition->setFactory(array('NewsletterManagerFactory', 'createNewsletterManager'));
6987
70-
$container->setDefinition('newsletter_manager', $definition);
88+
$definition = new Definition('AppBundle\Email\NewsletterManager');
89+
// call a static method
90+
$definition->setFactory(array('AppBundle\Email\NewsletterManager', 'create'));
91+
92+
$container->setDefinition('app.newsletter_manager', $definition);
93+
94+
$container->register('app.newsletter_manager_factory', 'AppBundle\Email\NewsletterManagerFactory');
95+
96+
$newsletterManager = new Definition();
97+
98+
// call a method on the specified service
99+
$newsletterManager->setFactory(array(
100+
new Reference('app.newsletter_manager_factory'),
101+
'createNewsletterManager'
102+
));
103+
104+
$container->setDefinition('app.newsletter_manager', $newsletterManager);
71105
72106
.. note::
73107

@@ -77,73 +111,24 @@ configure the service container to use the
77111
the configured class name may be used by compiler passes and therefore
78112
should be set to a sensible value.
79113

80-
Now, the method will be called statically. If the factory class itself should
81-
be instantiated and the resulting object's method called, configure the factory
82-
itself as a service. In this case, the method (e.g. get) should be changed to
83-
be non-static.
84-
85-
.. configuration-block::
86-
87-
.. code-block:: yaml
88-
89-
services:
90-
newsletter_manager.factory:
91-
class: NewsletterManagerFactory
92-
newsletter_manager:
93-
class: NewsletterManager
94-
factory: ["@newsletter_manager.factory", createNewsletterManager]
95-
96-
.. code-block:: xml
97-
98-
<?xml version="1.0" encoding="UTF-8" ?>
99-
<container xmlns="http://symfony.com/schema/dic/services"
100-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
101-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
102-
103-
<services>
104-
<service id="newsletter_manager.factory" class="NewsletterManagerFactory" />
105-
106-
<service id="newsletter_manager" class="NewsletterManager">
107-
<factory service="newsletter_manager.factory" method="createNewsletterManager" />
108-
</service>
109-
</services>
110-
</container>
111-
112-
.. code-block:: php
113-
114-
use Symfony\Component\DependencyInjection\Reference;
115-
use Symfony\Component\DependencyInjection\Definition;
116-
117-
// ...
118-
$container->register('newsletter_manager.factory', 'NewsletterManagerFactory');
119-
120-
$newsletterManager = new Definition();
121-
$newsletterManager->setFactory(array(
122-
new Reference('newsletter_manager.factory'),
123-
'createNewsletterManager'
124-
));
125-
$container->setDefinition('newsletter_manager', $newsletterManager);
126-
127114
Passing Arguments to the Factory Method
128115
---------------------------------------
129116

130117
If you need to pass arguments to the factory method, you can use the ``arguments``
131-
options inside the service container. For example, suppose the ``createNewsletterManager``
118+
options inside the service container. For example, suppose the ``createNewsletterManager()``
132119
method in the previous example takes the ``templating`` service as an argument:
133120

134121
.. configuration-block::
135122

136123
.. code-block:: yaml
137124
138125
services:
139-
newsletter_manager.factory:
140-
class: NewsletterManagerFactory
126+
# ...
141127
142-
newsletter_manager:
143-
class: NewsletterManager
144-
factory: ["@newsletter_manager.factory", createNewsletterManager]
145-
arguments:
146-
- '@templating'
128+
app.newsletter_manager:
129+
class: AppBundle\Email\NewsletterManager
130+
factory: ['@newsletter_manager_factory', createNewsletterManager]
131+
arguments: ['@templating']
147132
148133
.. code-block:: xml
149134
@@ -153,10 +138,10 @@ method in the previous example takes the ``templating`` service as an argument:
153138
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
154139
155140
<services>
156-
<service id="newsletter_manager.factory" class="NewsletterManagerFactory"/>
141+
<!-- ... -->
157142
158-
<service id="newsletter_manager" class="NewsletterManager">
159-
<factory service="newsletter_manager.factory" method="createNewsletterManager"/>
143+
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
144+
<factory service="app.newsletter_manager_factory" method="createNewsletterManager"/>
160145
<argument type="service" id="templating"/>
161146
</service>
162147
</services>
@@ -168,14 +153,11 @@ method in the previous example takes the ``templating`` service as an argument:
168153
use Symfony\Component\DependencyInjection\Definition;
169154
170155
// ...
171-
$container->register('newsletter_manager.factory', 'NewsletterManagerFactory');
172-
173-
$newsletterManager = new Definition(
174-
'NewsletterManager',
175-
array(new Reference('templating'))
176-
);
156+
$newsletterManager = new Definition('AppBundle\Email\NewsletterManager', array(
157+
new Reference('templating')
158+
));
177159
$newsletterManager->setFactory(array(
178-
new Reference('newsletter_manager.factory'),
160+
new Reference('app.newsletter_manager_factory'),
179161
'createNewsletterManager'
180162
));
181-
$container->setDefinition('newsletter_manager', $newsletterManager);
163+
$container->setDefinition('app.newsletter_manager', $newsletterManager);

testing/database.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class.
2727
.. tip::
2828

2929
It is possible (and a good idea) to inject your repository directly by
30-
registering your repository as a :doc:`factory service </components/dependency_injection/factories>`.
30+
registering your repository as a :doc:`factory service </service_container/factories>`.
3131
This is a little bit more work to setup, but makes testing easier as you
3232
only need to mock the repository.
3333

0 commit comments

Comments
 (0)