@@ -13,9 +13,9 @@ the service container to call a method on the factory rather than directly
13
13
instantiating the class.
14
14
15
15
Suppose you have a factory that configures and returns a new ``NewsletterManager ``
16
- object::
16
+ object by calling the static `` createNewsletterManager() `` method ::
17
17
18
- class NewsletterManagerFactory
18
+ class NewsletterManagerStaticFactory
19
19
{
20
20
public static function createNewsletterManager()
21
21
{
@@ -29,45 +29,98 @@ object::
29
29
30
30
To make the ``NewsletterManager `` object available as a service, you can
31
31
configure the service container to use the
32
- ``NewsletterManagerFactory ::createNewsletterManager() `` factory method:
32
+ ``NewsletterManagerStaticFactory ::createNewsletterManager() `` factory method:
33
33
34
34
.. configuration-block ::
35
35
36
36
.. code-block :: yaml
37
37
38
+ # app/config/services.yml
39
+
38
40
services :
39
41
app.newsletter_manager :
40
42
class : AppBundle\Email\NewsletterManager
41
- # call a static method
42
- factory : ['AppBundle\Email\NewsletterManager', create]
43
+ # call the static method
44
+ factory : ['AppBundle\Email\NewsletterManagerStaticFactory', createNewsletterManager]
45
+
46
+ .. code-block :: xml
47
+
48
+ <!-- app/config/services.xml -->
49
+
50
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
51
+ <container xmlns =" http://symfony.com/schema/dic/services"
52
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
53
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
54
+
55
+ <services >
56
+ <service id =" app.newsletter_manager" class =" AppBundle\Email\NewsletterManager" >
57
+ <!-- call the static method -->
58
+ <factory class =" AppBundle\Email\NewsletterManagerStaticFactory" method =" createNewsletterManager" />
59
+ </service >
60
+ </services >
61
+ </container >
62
+
63
+ .. code-block :: php
64
+
65
+ // app/config/services.php
66
+
67
+ use AppBundle\Email\NewsletterManager;
68
+ use AppBundle\Email\NewsletterManagerStaticFactory;
69
+ use Symfony\Component\DependencyInjection\Definition;
70
+ // ...
71
+
72
+ $definition = new Definition(NewsletterManager::class);
73
+ // call the static method
74
+ $definition->setFactory(array(NewsletterManagerStaticFactory::class, 'createNewsletterManager'));
75
+
76
+ $container->setDefinition('app.newsletter_manager', $definition);
77
+
78
+ .. note ::
79
+
80
+ When using a factory to create services, the value chosen for the ``class ``
81
+ option has no effect on the resulting service. The actual class name
82
+ only depends on the object that is returned by the factory. However,
83
+ the configured class name may be used by compiler passes and therefore
84
+ should be set to a sensible value.
85
+
86
+ If your factory is not using a static function to configure and create your
87
+ service, but a regular method, you can instantiate the factory itself as a
88
+ service too. Later, in the ":ref: `factories-passing-arguments-factory-method `"
89
+ section, you learn how you can inject arguments in this method.
90
+
91
+ Configuration of the service container then looks like this:
92
+
93
+ .. configuration-block ::
94
+
95
+ .. code-block :: yaml
43
96
97
+ # app/config/services.yml
98
+
99
+ services :
44
100
app.newsletter_manager_factory :
45
101
class : AppBundle\Email\NewsletterManagerFactory
46
102
47
103
app.newsletter_manager :
48
104
class : AppBundle\Email\NewsletterManager
49
- # call a method on the specified service
105
+ # call a method on the specified factory service
50
106
factory : ' app.newsletter_manager_factory:createNewsletterManager'
51
107
52
108
.. code-block :: xml
53
109
110
+ <!-- app/config/services.xml -->
111
+
54
112
<?xml version =" 1.0" encoding =" UTF-8" ?>
55
113
<container xmlns =" http://symfony.com/schema/dic/services"
56
114
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
57
115
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
58
116
59
117
<services >
60
- <service id =" app.newsletter_manager" class =" AppBundle\Email\NewsletterManager" >
61
- <!-- call a static method -->
62
- <factory class =" AppBundle\Email\NewsletterManager" method =" create" />
63
- </service >
64
-
65
118
<service id =" app.newsletter_manager_factory"
66
119
class =" AppBundle\Email\NewsletterManagerFactory"
67
120
/>
68
121
69
122
<service id =" app.newsletter_manager" class =" AppBundle\Email\NewsletterManager" >
70
- <!-- call a method on the specified service -->
123
+ <!-- call a method on the specified factory service -->
71
124
<factory service =" app.newsletter_manager_factory"
72
125
method =" createNewsletterManager"
73
126
/>
@@ -77,50 +130,42 @@ configure the service container to use the
77
130
78
131
.. code-block :: php
79
132
133
+ // app/config/services.php
134
+
80
135
use AppBundle\Email\NewsletterManager;
81
136
use AppBundle\Email\NewsletterManagerFactory;
82
137
use Symfony\Component\DependencyInjection\Definition;
83
138
// ...
84
139
85
- $definition = new Definition(NewsletterManager::class);
86
- // call a static method
87
- $definition->setFactory(array(NewsletterManager::class, 'create'));
88
-
89
- $container->setDefinition('app.newsletter_manager', $definition);
90
-
91
140
$container->register('app.newsletter_manager_factory', NewsletterManagerFactory::class);
92
141
93
142
$newsletterManager = new Definition(NewsletterManager::class);
94
143
95
- // call a method on the specified service
144
+ // call a method on the specified factory service
96
145
$newsletterManager->setFactory(array(
97
146
new Reference('app.newsletter_manager_factory'),
98
147
'createNewsletterManager'
99
148
));
100
149
101
150
$container->setDefinition('app.newsletter_manager', $newsletterManager);
102
151
103
- .. note ::
104
-
105
- When using a factory to create services, the value chosen for the ``class ``
106
- option has no effect on the resulting service. The actual class name
107
- only depends on the object that is returned by the factory. However,
108
- the configured class name may be used by compiler passes and therefore
109
- should be set to a sensible value.
110
-
111
152
.. note ::
112
153
113
154
The traditional configuration syntax in YAML files used an array to define
114
155
the factory service and the method name:
115
156
116
157
.. code-block :: yaml
117
158
159
+ # app/config/services.yml
160
+
118
161
app.newsletter_manager :
119
162
# new syntax
120
163
factory : ' app.newsletter_manager_factory:createNewsletterManager'
121
164
# old syntax
122
165
factory : ['@app.newsletter_manager_factory', createNewsletterManager]
123
166
167
+ .. _factories-passing-arguments-factory-method :
168
+
124
169
Passing Arguments to the Factory Method
125
170
---------------------------------------
126
171
@@ -132,6 +177,8 @@ method in the previous example takes the ``templating`` service as an argument:
132
177
133
178
.. code-block :: yaml
134
179
180
+ # app/config/services.yml
181
+
135
182
services :
136
183
# ...
137
184
@@ -142,6 +189,8 @@ method in the previous example takes the ``templating`` service as an argument:
142
189
143
190
.. code-block :: xml
144
191
192
+ <!-- app/config/services.xml -->
193
+
145
194
<?xml version =" 1.0" encoding =" UTF-8" ?>
146
195
<container xmlns =" http://symfony.com/schema/dic/services"
147
196
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -159,6 +208,8 @@ method in the previous example takes the ``templating`` service as an argument:
159
208
160
209
.. code-block :: php
161
210
211
+ // app/config/services.php
212
+
162
213
use AppBundle\Email\NewsletterManager;
163
214
use Symfony\Component\DependencyInjection\Reference;
164
215
use Symfony\Component\DependencyInjection\Definition;
0 commit comments