@@ -13,9 +13,9 @@ the service container to call a method on the factory rather than directly
1313instantiating the class.
1414
1515Suppose you have a factory that configures and returns a new ``NewsletterManager ``
16- object::
16+ object by calling the static `` createNewsletterManager() `` method ::
1717
18- classNewsletterManagerFactory
18+ classNewsletterManagerStaticFactory
1919 {
2020 public static function createNewsletterManager()
2121 {
@@ -29,45 +29,98 @@ object::
2929
3030To make the ``NewsletterManager `` object available as a service, you can
3131configure the service container to use the
32- ``NewsletterManagerFactory ::createNewsletterManager() `` factory method:
32+ ``NewsletterManagerStaticFactory ::createNewsletterManager() `` factory method:
3333
3434..configuration-block ::
3535
3636 ..code-block ::yaml
3737
38+ # app/config/services.yml
39+
3840services :
3941app.newsletter_manager :
4042class :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
4396
97+ # app/config/services.yml
98+
99+ services :
44100app.newsletter_manager_factory :
45101class :AppBundle\Email\NewsletterManagerFactory
46102
47103app.newsletter_manager :
48104class :AppBundle\Email\NewsletterManager
49- # call a method on the specified service
105+ # call a method on the specifiedfactory service
50106factory :' app.newsletter_manager_factory:createNewsletterManager'
51107
52108 ..code-block ::xml
53109
110+ <!-- app/config/services.xml-->
111+
54112 <?xml version =" 1.0" encoding =" UTF-8" ?>
55113 <container xmlns =" http://symfony.com/schema/dic/services"
56114xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
57115xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
58116
59117 <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-
65118 <service id =" app.newsletter_manager_factory"
66119class =" AppBundle\Email\NewsletterManagerFactory"
67120 />
68121
69122 <service id =" app.newsletter_manager" class =" AppBundle\Email\NewsletterManager" >
70- <!-- call a method on the specified service-->
123+ <!-- call a method on the specifiedfactory service-->
71124 <factory service =" app.newsletter_manager_factory"
72125method =" createNewsletterManager"
73126 />
@@ -77,50 +130,42 @@ configure the service container to use the
77130
78131 ..code-block ::php
79132
133+ // app/config/services.php
134+
80135 use AppBundle\Email\NewsletterManager;
81136 use AppBundle\Email\NewsletterManagerFactory;
82137 use Symfony\Component\DependencyInjection\Definition;
83138 // ...
84139
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-
91140 $container->register('app.newsletter_manager_factory', NewsletterManagerFactory::class);
92141
93142 $newsletterManager = new Definition(NewsletterManager::class);
94143
95- // call a method on the specified service
144+ // call a method on the specifiedfactory service
96145 $newsletterManager->setFactory(array(
97146 new Reference('app.newsletter_manager_factory'),
98147 'createNewsletterManager'
99148 ));
100149
101150 $container->setDefinition('app.newsletter_manager', $newsletterManager);
102151
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-
111152 ..note ::
112153
113154 The traditional configuration syntax in YAML files used an array to define
114155 the factory service and the method name:
115156
116157 ..code-block ::yaml
117158
159+ # app/config/services.yml
160+
118161app.newsletter_manager :
119162# new syntax
120163factory :' app.newsletter_manager_factory:createNewsletterManager'
121164# old syntax
122165factory :['@app.newsletter_manager_factory', createNewsletterManager]
123166
167+ .. _factories-passing-arguments-factory-method :
168+
124169Passing Arguments to the Factory Method
125170---------------------------------------
126171
@@ -132,6 +177,8 @@ method in the previous example takes the ``templating`` service as an argument:
132177
133178 ..code-block ::yaml
134179
180+ # app/config/services.yml
181+
135182services :
136183# ...
137184
@@ -142,6 +189,8 @@ method in the previous example takes the ``templating`` service as an argument:
142189
143190 ..code-block ::xml
144191
192+ <!-- app/config/services.xml-->
193+
145194 <?xml version =" 1.0" encoding =" UTF-8" ?>
146195 <container xmlns =" http://symfony.com/schema/dic/services"
147196xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -159,6 +208,8 @@ method in the previous example takes the ``templating`` service as an argument:
159208
160209 ..code-block ::php
161210
211+ // app/config/services.php
212+
162213 use AppBundle\Email\NewsletterManager;
163214 use Symfony\Component\DependencyInjection\Reference;
164215 use Symfony\Component\DependencyInjection\Definition;