How to extend the configuration of another package in Symfony2?

I know that I can overwrite templates or extend classes of other packages. But can I expand the configs too? I was hoping I could load other namespaces from the configuration into the DependenyInjection/AcmeExtension.php load method, but I did not find anything there.

Example:

I have an AcmeBundle that defines the following in config:

 acme: a: 1 

I want to expand this package (in the new AwesomeAcmeBundle package) and be able to define other variables either by adding them to the original namespace:

 acme: a: 1 b: 2 

or by moving the original namespace to a new one and adding new variables:

 awesome_acme: a: 1 b: 2 
+6
source share
3 answers

I had similar needs, and I solved them as follows:

1) Extend the parent 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 your own extension that can really handle 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 app\config\config.yml , which you can use in your new foo attribute - set all the parameters that derp (as a parent set) has, plus any of your new parameters that you defined in Configuration.php .

+2
source

If you are talking about .yml s, you can import the AcmeBundle into the AwesomeAcmeBundle configuration with

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

and then overwrite the necessary parameters.

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

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

Source: https://habr.com/ru/post/899226/


All Articles