@@ -18,9 +18,9 @@ instantiating the class.
18
18
syntax for factories prior to 2.6.
19
19
20
20
Suppose you have a factory that configures and returns a new ``NewsletterManager ``
21
- object::
21
+ object by calling the static `` createNewsletterManager() `` method ::
22
22
23
- class NewsletterManagerFactory
23
+ class NewsletterManagerStaticFactory
24
24
{
25
25
public static function createNewsletterManager()
26
26
{
@@ -34,45 +34,98 @@ object::
34
34
35
35
To make the ``NewsletterManager `` object available as a service, you can
36
36
configure the service container to use the
37
- ``NewsletterManagerFactory ::createNewsletterManager() `` factory method:
37
+ ``NewsletterManagerStaticFactory ::createNewsletterManager() `` factory method:
38
38
39
39
.. configuration-block ::
40
40
41
41
.. code-block :: yaml
42
42
43
+ # app/config/services.yml
44
+
43
45
services :
44
46
app.newsletter_manager :
45
47
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
48
101
102
+ # app/config/services.yml
103
+
104
+ services :
49
105
app.newsletter_manager_factory :
50
106
class : AppBundle\Email\NewsletterManagerFactory
51
107
52
108
app.newsletter_manager :
53
109
class : AppBundle\Email\NewsletterManager
54
- # call a method on the specified service
110
+ # call a method on the specified factory service
55
111
factory : ' app.newsletter_manager_factory:createNewsletterManager'
56
112
57
113
.. code-block :: xml
58
114
115
+ <!-- app/config/services.xml -->
116
+
59
117
<?xml version =" 1.0" encoding =" UTF-8" ?>
60
118
<container xmlns =" http://symfony.com/schema/dic/services"
61
119
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
62
120
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
63
121
64
122
<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
-
70
123
<service id =" app.newsletter_manager_factory"
71
124
class =" AppBundle\Email\NewsletterManagerFactory"
72
125
/>
73
126
74
127
<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 -->
76
129
<factory service =" app.newsletter_manager_factory"
77
130
method =" createNewsletterManager"
78
131
/>
@@ -82,50 +135,42 @@ configure the service container to use the
82
135
83
136
.. code-block :: php
84
137
138
+ // app/config/services.php
139
+
85
140
use AppBundle\Email\NewsletterManager;
86
141
use AppBundle\Email\NewsletterManagerFactory;
87
142
use Symfony\Component\DependencyInjection\Definition;
88
143
// ...
89
144
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
-
96
145
$container->register('app.newsletter_manager_factory', NewsletterManagerFactory::class);
97
146
98
147
$newsletterManager = new Definition(NewsletterManager::class);
99
148
100
- // call a method on the specified service
149
+ // call a method on the specified factory service
101
150
$newsletterManager->setFactory(array(
102
151
new Reference('app.newsletter_manager_factory'),
103
152
'createNewsletterManager'
104
153
));
105
154
106
155
$container->setDefinition('app.newsletter_manager', $newsletterManager);
107
156
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
-
116
157
.. note ::
117
158
118
159
The traditional configuration syntax in YAML files used an array to define
119
160
the factory service and the method name:
120
161
121
162
.. code-block :: yaml
122
163
164
+ # app/config/services.yml
165
+
123
166
app.newsletter_manager :
124
167
# new syntax
125
168
factory : ' app.newsletter_manager_factory:createNewsletterManager'
126
169
# old syntax
127
170
factory : ['@app.newsletter_manager_factory', createNewsletterManager]
128
171
172
+ .. _factories-passing-arguments-factory-method :
173
+
129
174
Passing Arguments to the Factory Method
130
175
---------------------------------------
131
176
@@ -137,6 +182,8 @@ method in the previous example takes the ``templating`` service as an argument:
137
182
138
183
.. code-block :: yaml
139
184
185
+ # app/config/services.yml
186
+
140
187
services :
141
188
# ...
142
189
@@ -147,6 +194,8 @@ method in the previous example takes the ``templating`` service as an argument:
147
194
148
195
.. code-block :: xml
149
196
197
+ <!-- app/config/services.xml -->
198
+
150
199
<?xml version =" 1.0" encoding =" UTF-8" ?>
151
200
<container xmlns =" http://symfony.com/schema/dic/services"
152
201
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:
164
213
165
214
.. code-block :: php
166
215
216
+ // app/config/services.php
217
+
167
218
use AppBundle\Email\NewsletterManager;
168
219
use Symfony\Component\DependencyInjection\Reference;
169
220
use Symfony\Component\DependencyInjection\Definition;
0 commit comments