Ondrej Slinták
Ondrej Slinták

Reputation: 31970

How to extend config of different bundle in Symfony2?

I know I can overwrite templates or extend classes of other bundles. But can I extend also configs? I was hoping to be able to load other namespaces from config in DependenyInjection/AcmeExtension.php's load method, but I haven't found anything about it anywhere.

Example:

I have AcmeBundle which defines following in config:

acme:
    a: 1

I want to extend this bundle (in new bundle called AwesomeAcmeBundle) and be able to define another variables either by adding them to original namespace:

acme:
    a: 1
    b: 2

or by wrapping original namespace to new one and adding new variables there:

awesome_acme:
    a: 1
    b: 2

Upvotes: 8

Views: 5667

Answers (3)

axasanya
axasanya

Reputation: 41

I had similar needs and I have solved them in the following way:

1) Extend the parent's Configuration class

//FooBundle\DependencyInjection\Configuration.php

use DerpBundle\DependencyInjection\Configuration as BaseConfiguration;

class Configuration extends BaseConfiguration
{

    public function getConfigTreeBuilder()
    {
        $treeBuilder = parent::getConfigTreeBuilder();

        //protected attribute access workaround
        $reflectedClass = new \ReflectionObject($treeBuilder);
        $property = $reflectedClass->getProperty("root");
        $property->setAccessible(true);

        $rootNode = $property->getValue($treeBuilder);

        $rootNode
           ->children()
           ...

        return $treeBuilder;
     }
}

2) Create own extension that actually can handle the new configuration entries

class FooExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $container)
    {

        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        //custom parameters
        $container->setParameter('new_param_container_name', $config['new_param_name']);

     ...
     }
}

3) in the app\config\config.yml you can use in your new foo attribute-set all the parameters that derp (as a parent bundle) has plus any of your new params that you have defined in the Configuration.php.

Upvotes: 3

Mick
Mick

Reputation: 31959

imports:
 - { resource: @YourBundle/Resources/config/services.yml }

Upvotes: 0

prehfeldt
prehfeldt

Reputation: 2172

If you're talking about the .ymls, you can import the AcmeBundles confing in the AwesomeAcmeBundle config with

imports:
    - { resource: path/to/AcmeBundles/config.yml }

and then overwrite the parameters you want.

Symfony is doing the same in the config_dev.yml with the framework/router parameter.

Upvotes: 0

Related Questions