@@ -218,23 +218,64 @@ This class can now be used in your ``load()`` method to merge configurations and
218
218
force validation (e.g. if an additional option was passed, an exception will be
219
219
thrown)::
220
220
221
+ // src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
222
+
221
223
public function load(array $configs, ContainerBuilder $container)
222
224
{
223
225
$configuration = new Configuration();
224
226
225
227
$config = $this->processConfiguration($configuration, $configs);
226
228
227
- // Example configuration parameter usage: Set configuration variables as
228
- // parameters in the container.
229
- $container->setParameter('twitter.client_id', $config['twitter']['client_id']);
230
- $container->setParameter('twitter.client_secret', $config['twitter']['client_secret']);
231
-
229
+ // you now have these 2 config keys
230
+ // $config['twitter']['client_id'] and $config['twitter']['client_secret']
232
231
}
233
232
234
233
The ``processConfiguration() `` method uses the configuration tree you've defined
235
234
in the ``Configuration `` class to validate, normalize and merge all the
236
235
configuration arrays together.
237
236
237
+ Now, you can use the ``$config `` variable to modify a service provided by your bundle.
238
+ For example, imagine your bundle has the following example config:
239
+
240
+ .. code-block :: xml
241
+
242
+ <!-- src/Acme/SocialBundle/Resources/config/services.xml -->
243
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
244
+ <container xmlns =" http://symfony.com/schema/dic/services"
245
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
246
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
247
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
248
+
249
+ <services >
250
+ <service id =" acme.social.twitter_client" class =" Acme\SocialBundle\TwitterClient" >
251
+ <argument ></argument > <!-- will be filled in with client_id dynamically -->
252
+ <argument ></argument > <!-- will be filled in with client_secret dynamically -->
253
+ </service >
254
+ </services >
255
+ </container >
256
+
257
+ In your extension, you can load this and dynamically set its arguments::
258
+
259
+ // src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
260
+ // ...
261
+
262
+ use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
263
+ use Symfony\Component\Config\FileLocator;
264
+
265
+ public function load(array $configs, ContainerBuilder $container)
266
+ {
267
+ $loader = new XmlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources/config'));
268
+ $loader->load('services.xml');
269
+
270
+ $configuration = new Configuration();
271
+ $config = $this->processConfiguration($configuration, $configs);
272
+
273
+ $def = $container->getDefinition('acme.social.twitter_client');
274
+ $def->replaceArgument(0, $config['twitter']['client_id']);
275
+ $def->replaceArgument(1, $config['twitter']['client_secret']);
276
+ }
277
+
278
+
238
279
.. tip ::
239
280
240
281
Instead of calling ``processConfiguration() `` in your extension each time you
@@ -258,9 +299,7 @@ configuration arrays together.
258
299
}
259
300
260
301
This class uses the ``getConfiguration() `` method to get the Configuration
261
- instance. You should override it if your Configuration class is not called
262
- ``Configuration `` or if it is not placed in the same namespace as the
263
- extension.
302
+ instance.
264
303
265
304
.. sidebar :: Processing the Configuration yourself
266
305
0 commit comments