Symfony2: Is it possible to add configuration for another package through DependencyInjection?

In Symfony2 config.yml you can add an โ€œimportโ€, for example:

imports: - { resource: services.yml } 

Inside my services.yml I have:

 imports: security_bundle: resource: @AcmeSecurityBundle/Resources/config/services.yml 

However, an alternative way to declare services for a package is to use the DependencyInjection Extension , which eliminates the need to import something into config.yml manually, thus untying the code.

 namespace Acme\Bundle\SecurityBundle\DependencyInjection; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; class AcmeSecurityExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { $loader = new YamlFileLoader( $container, new FileLocator(__DIR__ . '/../Resources/config') ); $loader->load('services.yml'); } } 

Question This is great for service declarations, but, for example, you want the package to configure another package, for example, add LiipImagineBundle filters (it is like AvalancheImagineBundle ):

 liip_imagine: filter_sets: security_avatar_thumbnail: quality: 75 filters: thumbnail: { size: [140, 140], mode: inset } 

Symfony then complains that

There is no extension that can load the configuration for "liip_imagine"

Does anyone know if there is a way to add configuration for a third-party package from another package without touching config.yml ?

+7
source share
3 answers

In Symfony 2.2, this is possible with PrependExtensionInterface.

Take a look at the entry "How to simplify the configuration of several bundles":

http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html

+9
source

I think this is possible using the DependencyInjection \ YourBundleExtension class in your package, and then do

 public function load(array $configs, ContainerBuilder $container) { ... $container->setParameter('the_bundle_parameter.you.want.to.override',$itsValue); ... } 

But I donโ€™t know if this is better or not ...

0
source

I found a solution for placing filters inside the package, and not in the root of config.yml

 avalanche_imagine: web_root: %kernel.root_dir%/../web cache_prefix: media/cache driver: gd bundle: PathToYourBundleClass 

AvalancheImagineExtension: load Add this:

  $bundleClass = $container->getParameter("imagine.bundle"); if ($bundleClass) { $bundle = new $bundleClass(); $bundle->getContainerExtension()->load(array(), $container); } 

AvalancheImagineExtension / Resources / Configurations / config.xml

 <parameter key="imagine.bundle"></parameter> 

Finally, in your kit:

 parameters: imagine.filters: image_main: type: thumbnail options: { size: [490, 310], mode: outbound } 
-one
source

All Articles