How to Simplify Configuration of Multiple Bundles

How to Simplify Configuration of Multiple Bundles

When building reusable and extensible applications, developers are often faced with a choice: either create a single large bundle or multiple smaller bundles. Creating a single bundle has the drawback that it’s impossible for users to choose to remove functionality they are not using. Creating multiple bundles has the drawback that configuration becomes more tedious and settings often need to be repeated for various bundles.

It is possible to remove the disadvantage of the multiple bundle approach by enabling a single Extension to prepend the settings for any bundle. It can use the settings defined in the config/* files to prepend settings just as if they had been written explicitly by the user in the application configuration.

For example, this could be used to configure the entity manager name to use in multiple bundles. Or it can be used to enable an optional feature that depends on another bundle being loaded as well.

To give an Extension the power to do this, it needs to implement Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface:

  1. // src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
  2. namespace Acme\HelloBundle\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\ContainerBuilder;
  4. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  5. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  6. class AcmeHelloExtension extends Extension implements PrependExtensionInterface
  7. {
  8. // ...
  9. public function prepend(ContainerBuilder $container)
  10. {
  11. // ...
  12. }
  13. }

Inside the [prepend()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/DependencyInjection/Extension/PrependExtensionInterface.php "Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface::prepend()") method, developers have full access to the Symfony\Component\DependencyInjection\ContainerBuilder instance just before the [load()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/DependencyInjection/Extension/ExtensionInterface.php "Symfony\Component\DependencyInjection\Extension\ExtensionInterface::load()") method is called on each of the registered bundle Extensions. In order to prepend settings to a bundle extension developers can use the [prependExtensionConfig()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/DependencyInjection/ContainerBuilder.php "Symfony\Component\DependencyInjection\ContainerBuilder::prependExtensionConfig()") method on the Symfony\Component\DependencyInjection\ContainerBuilder instance. As this method only prepends settings, any other settings done explicitly inside the config/* files would override these prepended settings.

The following example illustrates how to prepend a configuration setting in multiple bundles as well as disable a flag in multiple bundles in case a specific other bundle is not registered:

  1. // src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
  2. public function prepend(ContainerBuilder $container)
  3. {
  4. // get all bundles
  5. $bundles = $container->getParameter('kernel.bundles');
  6. // determine if AcmeGoodbyeBundle is registered
  7. if (!isset($bundles['AcmeGoodbyeBundle'])) {
  8. // disable AcmeGoodbyeBundle in bundles
  9. $config = ['use_acme_goodbye' => false];
  10. foreach ($container->getExtensions() as $name => $extension) {
  11. switch ($name) {
  12. case 'acme_something':
  13. case 'acme_other':
  14. // set use_acme_goodbye to false in the config of
  15. // acme_something and acme_other
  16. //
  17. // note that if the user manually configured
  18. // use_acme_goodbye to true in config/services.yaml
  19. // then the setting would in the end be true and not false
  20. $container->prependExtensionConfig($name, $config);
  21. break;
  22. }
  23. }
  24. }
  25. // process the configuration of AcmeHelloExtension
  26. $configs = $container->getExtensionConfig($this->getAlias());
  27. // resolve config parameters e.g. %kernel.debug% to its boolean value
  28. $resolvingBag = $container->getParameterBag();
  29. $configs = $resolvingBag->resolveValue($configs);
  30. // use the Configuration class to generate a config array with
  31. // the settings "acme_hello"
  32. $config = $this->processConfiguration(new Configuration(), $configs);
  33. // check if entity_manager_name is set in the "acme_hello" configuration
  34. if (isset($config['entity_manager_name'])) {
  35. // prepend the acme_something settings with the entity_manager_name
  36. $config = ['entity_manager_name' => $config['entity_manager_name']];
  37. $container->prependExtensionConfig('acme_something', $config);
  38. }
  39. }

The above would be the equivalent of writing the following into the config/packages/acme_something.yaml in case AcmeGoodbyeBundle is not registered and the entity_manager_name setting for acme_hello is set to non_default:

  • YAML

    1. # config/packages/acme_something.yaml
    2. acme_something:
    3. # ...
    4. use_acme_goodbye: false
    5. entity_manager_name: non_default
    6. acme_other:
    7. # ...
    8. use_acme_goodbye: false
  • XML

    1. <!-- config/packages/acme_something.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:acme-something="http://example.org/schema/dic/acme_something"
    6. xmlns:acme-other="http://example.org/schema/dic/acme_other"
    7. xsi:schemaLocation="http://symfony.com/schema/dic/services
    8. https://symfony.com/schema/dic/services/services-1.0.xsd
    9. http://example.org/schema/dic/acme_something
    10. https://example.org/schema/dic/acme_something/acme_something-1.0.xsd
    11. http://example.org/schema/dic/acme_other
    12. https://example.org/schema/dic/acme_something/acme_other-1.0.xsd">
    13. <acme-something:config use-acme-goodbye="false">
    14. <!-- ... -->
    15. <acme-something:entity-manager-name>non_default</acme-something:entity-manager-name>
    16. </acme-something:config>
    17. <acme-other:config use-acme-goodbye="false"/>
    18. </container>
  • PHP

    1. // config/packages/acme_something.php
    2. $container->loadFromExtension('acme_something', [
    3. // ...
    4. 'use_acme_goodbye' => false,
    5. 'entity_manager_name' => 'non_default',
    6. ]);
    7. $container->loadFromExtension('acme_other', [
    8. // ...
    9. 'use_acme_goodbye' => false,
    10. ]);

More than one Bundle using PrependExtensionInterface

If there is more than one bundle that prepends the same extension and defines the same key, the bundle that is registered first will take priority: next bundles won’t override this specific config setting.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.