@@ -41,9 +41,18 @@ configure the service container to use the
41
41
.. code-block :: yaml
42
42
43
43
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]
47
56
48
57
.. code-block :: xml
49
58
@@ -53,21 +62,46 @@ configure the service container to use the
53
62
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
54
63
55
64
<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
+ />
58
79
</service >
59
80
</services >
60
81
</container >
61
82
62
83
.. code-block :: php
63
84
64
85
use Symfony\Component\DependencyInjection\Definition;
65
-
66
86
// ...
67
- $definition = new Definition('NewsletterManager');
68
- $definition->setFactory(array('NewsletterManagerFactory', 'createNewsletterManager'));
69
87
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);
71
105
72
106
.. note ::
73
107
@@ -77,73 +111,24 @@ configure the service container to use the
77
111
the configured class name may be used by compiler passes and therefore
78
112
should be set to a sensible value.
79
113
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
-
127
114
Passing Arguments to the Factory Method
128
115
---------------------------------------
129
116
130
117
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() ``
132
119
method in the previous example takes the ``templating `` service as an argument:
133
120
134
121
.. configuration-block ::
135
122
136
123
.. code-block :: yaml
137
124
138
125
services :
139
- newsletter_manager.factory :
140
- class : NewsletterManagerFactory
126
+ # ...
141
127
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']
147
132
148
133
.. code-block :: xml
149
134
@@ -153,10 +138,10 @@ method in the previous example takes the ``templating`` service as an argument:
153
138
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
154
139
155
140
<services >
156
- <service id = " newsletter_manager.factory " class = " NewsletterManagerFactory " / >
141
+ <!-- ... -- >
157
142
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" />
160
145
<argument type =" service" id =" templating" />
161
146
</service >
162
147
</services >
@@ -168,14 +153,11 @@ method in the previous example takes the ``templating`` service as an argument:
168
153
use Symfony\Component\DependencyInjection\Definition;
169
154
170
155
// ...
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
+ ));
177
159
$newsletterManager->setFactory(array(
178
- new Reference('newsletter_manager.factory '),
160
+ new Reference('app.newsletter_manager_factory '),
179
161
'createNewsletterManager'
180
162
));
181
- $container->setDefinition('newsletter_manager', $newsletterManager);
163
+ $container->setDefinition('app. newsletter_manager', $newsletterManager);
0 commit comments