@@ -15,8 +15,8 @@ creation.
1515A Service Configurator can be used, for example, when you a have a service that
1616requires complex setup based on configuration settings coming from different
1717sources/services. Using an external configurator, you can maintain the service
18- implementationclean and decoupled from the other objects that provide the
19- configuration for theservice .
18+ implementationcleanly andkeep it decoupled from the other objects that provide
19+ theconfiguration needed .
2020
2121Another interesting use case is when you have multiple objects that share a
2222common configuration or that should be configured in a similar way at runtime.
@@ -66,17 +66,22 @@ and also a ``GreetingCardManager`` class::
6666 }
6767
6868
69- As commented before, the formatters should be set at runtime depending on
70- application settings, so we also have a ``EmailFormatterManager `` class which is
71- responsible for loading and validating formatters enabled in the application::
69+ As mentioned before, the goal is to set the formatters at runtime depending on
70+ application settings. To do this, you also have an ``EmailFormatterManager ``
71+ class which is responsible for loading and validating formatters enabled
72+ in the application::
7273
7374 class EmailFormatterManager
7475 {
7576 protected $enabledFormatters;
7677
7778 public function loadFormatters()
7879 {
80+ // code to configure which formatters to use
81+ $enabledFormatters = array();
7982 // ...
83+
84+ $this->enabledFormatters = $enabledFormatters;
8085 }
8186
8287 public function getEnabledFormatters()
@@ -110,7 +115,7 @@ create a configurator class to configure these instances::
110115 // ...
111116 }
112117
113- ``EmailConfigurator `` task is to inject enabled filtersto ``NewsletterManager ``
118+ The ``EmailConfigurator ``'s job is to injectthe enabled filtersinto ``NewsletterManager ``
114119and ``GreetingCardManager `` because they are not aware of where the enabled
115120filters come from. In the other hand, the ``EmailFormatterManager `` holds the
116121knowledge about the enabled formatters and how to load them, keeping the single
@@ -125,67 +130,52 @@ The service config for the above classes would look something like this:
125130
126131 ..code-block ::yaml
127132
128- parameters :
129- # ...
130- newsletter_manager.class :NewsletterManager
131- greeting_card_manager.class :GreetingCardManager
132- email_formatter_manager.class :EmailFormatterManager
133- email_configurator.class :EmailConfigurator
134-
135133services :
136134my_mailer :
137135# ...
138136
139137email_formatter_manager :
140- class :" %email_formatter_manager.class% "
138+ class :EmailFormatterManager
141139# ...
142140
143141email_configurator :
144- class :" %email_configurator.class% "
145- arguments :[@email_formatter_manager]
142+ class :EmailConfigurator
143+ arguments :[" @email_formatter_manager" ]
146144# ...
147145
148146newsletter_manager :
149- class :" %newsletter_manager.class% "
147+ class :NewsletterManager
150148calls :
151- -[setMailer, [@my_mailer]]
152- configurator :[@email_configurator, configure]
149+ -[setMailer, [" @my_mailer" ]]
150+ configurator :[" @email_configurator" , configure]
153151
154152greeting_card_manager :
155- class :" %greeting_card_manager.class% "
153+ class :GreetingCardManager
156154calls :
157- -[setMailer, [@my_mailer]]
158- configurator :[@email_configurator, configure]
155+ -[setMailer, [" @my_mailer" ]]
156+ configurator :[" @email_configurator" , configure]
159157
160158
161159 ..code-block ::xml
162160
163- <parameters >
164- <!-- ...-->
165- <parameter key =" newsletter_manager.class" >NewsletterManager</parameter >
166- <parameter key =" greeting_card_manager.class" >GreetingCardManager</parameter >
167- <parameter key =" email_formatter_manager.class" >EmailFormatterManager</parameter >
168- <parameter key =" email_configurator.class" >EmailConfigurator</parameter >
169- </parameters >
170-
171161 <services >
172162 <service id =" my_mailer" ...>
173163<!-- ...-->
174164 </service >
175- <service id =" email_formatter_manager" class =" %email_formatter_manager.class% " >
165+ <service id =" email_formatter_manager" class =" EmailFormatterManager " >
176166<!-- ...-->
177167 </service >
178- <service id =" email_configurator" class =" %email_configurator.class% " >
168+ <service id =" email_configurator" class =" EmailConfigurator " >
179169 <argument type =" service" id =" email_formatter_manager" />
180170<!-- ...-->
181171 </service >
182- <service id =" newsletter_manager" class =" %newsletter_manager.class% " >
172+ <service id =" newsletter_manager" class =" NewsletterManager " >
183173 <call method =" setMailer" >
184174 <argument type =" service" id =" my_mailer" />
185175 </call >
186176 <configurator service =" email_configurator" method =" configure" />
187177 </service >
188- <service id =" greeting_card_manager" class =" %greeting_card_manager.class% " >
178+ <service id =" greeting_card_manager" class =" GreetingCardManager " >
189179 <call method =" setMailer" >
190180 <argument type =" service" id =" my_mailer" />
191181 </call >
@@ -199,24 +189,23 @@ The service config for the above classes would look something like this:
199189 use Symfony\Component\DependencyInjection\Reference;
200190
201191 // ...
202- $container->setParameter('newsletter_manager.class', 'NewsletterManager');
203- $container->setParameter('greeting_card_manager.class', 'GreetingCardManager');
204- $container->setParameter('email_formatter_manager.class', 'EmailFormatterManager');
205- $container->setParameter('email_configurator.class', 'EmailConfigurator');
206-
207192 $container->setDefinition('my_mailer', ...);
208- $container->setDefinition('email_formatter_manager', ...);
209- $container->setDefinition('email_configurator', ...);
193+ $container->setDefinition('email_formatter_manager', new Definition(
194+ 'EmailFormatterManager'
195+ ));
196+ $container->setDefinition('email_configurator', new Definition(
197+ 'EmailConfigurator'
198+ ));
210199 $container->setDefinition('newsletter_manager', new Definition(
211- '%newsletter_manager.class% '
200+ 'NewsletterManager '
212201 ))->addMethodCall('setMailer', array(
213202 new Reference('my_mailer'),
214203 ))->setConfigurator(array(
215204 new Reference('email_configurator'),
216205 'configure',
217206 )));
218207 $container->setDefinition('greeting_card_manager', new Definition(
219- '%greeting_card_manager.class% '
208+ 'GreetingCardManager '
220209 ))->addMethodCall('setMailer', array(
221210 new Reference('my_mailer'),
222211 ))->setConfigurator(array(