7

In Symfony2's config.yml you can add an "import" such as:

imports:
    - { resource: services.yml }

Inside my services.yml I then have:

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

However the alternative way to declare services for a bundle is by using a DependencyInjection Extension thus eliminating the need to import anything into config.yml manually thus decoupling 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');
    }

}

The Question This works fine for service declarations but say for instance you want a bundle to configure another bundle such as adding LiipImagineBundle (it's like AvalancheImagineBundle) filters:

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

Symfony then complains that

There is no extension able to load the configuration for "liip_imagine"

So does anyone know if there is a way to add configuration for third party bundle from another bundle without touching config.yml?

j0k
  • 21,914
  • 28
  • 75
  • 84
Kasheen
  • 5,331
  • 2
  • 29
  • 41

3 Answers3

9

In Symfony 2.2 it is possible with help of PrependExtensionInterface.

Take a look at the "How to simplify configuration of multiple Bundles" cookbook entry:

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

dr.scre
  • 2,137
  • 1
  • 20
  • 22
  • 1
    I needed this for FOSUserBundle! I can now create my extension on it and preload it with my configuration. Example: $container->prependExtensionConfig('fos_user', array( 'db_driver' => 'orm' )); – Steffen Brem Nov 19 '13 at 16:30
0

I think that is possible, using the DependencyInjection\YourBundleExtension class in your bundle, and then doing a

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

But I don't really know if it's best practice or not...

Yoann Chambonnet
  • 1,275
  • 15
  • 22
  • This will not work because what the original poster is attempting to override is not a parameter. This would only work if you can convince the developer of liip_imagine to refactor their bundle to work off of a series of parameters instead of semantic configuration (http://symfony.com/doc/current/cookbook/bundles/extension.html). That would be quite a bit of work for almost no benefit, and probably no benefit at all to liip_imagine. – Asa Ayers Apr 02 '12 at 21:58
  • I think that the answer may be that it isn't possible. For now I'm just importing a bundle level config.yml into the master config.yml so that my liip_imagine config is still encapsulated inside the bundle that it is relevant to. – Kasheen Apr 18 '12 at 10:26
-1

I found a solution to put filters inside the bundle instead of in the root 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/config/config.xml

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

Finally, in your bundle :

parameters:
    imagine.filters:
        image_main:
            type:    thumbnail
            options: { size: [490, 310], mode: outbound }
Thommas
  • 124
  • 1
  • 8
  • very bad idea, ImagineBundle's extension class will have load() method called twice - once by container and once by your code – Adam Zielinski Jan 13 '14 at 10:23