Skip to content

Commit 8292f92

Browse files
committed
[#5621] Enhancing example of using bundle config
1 parent d9e7f22 commit 8292f92

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

bundles/configuration.rst

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,64 @@ This class can now be used in your ``load()`` method to merge configurations and
218218
force validation (e.g. if an additional option was passed, an exception will be
219219
thrown)::
220220

221+
// src/Acme/SocialBundle/DependencyInjection/AcmeSocialExtension.php
222+
221223
public function load(array $configs, ContainerBuilder $container)
222224
{
223225
$configuration = new Configuration();
224226

225227
$config = $this->processConfiguration($configuration, $configs);
226228
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']
232231
}
233232

234233
The ``processConfiguration()`` method uses the configuration tree you've defined
235234
in the ``Configuration`` class to validate, normalize and merge all the
236235
configuration arrays together.
237236

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+
238279
.. tip::
239280

240281
Instead of calling ``processConfiguration()`` in your extension each time you
@@ -258,9 +299,7 @@ configuration arrays together.
258299
}
259300

260301
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.
264303

265304
.. sidebar:: Processing the Configuration yourself
266305

0 commit comments

Comments
 (0)