加载基于其他捆绑包的服务,Symfony2 DI


Load Services based on other bundles, Symfony2 DI

在我的Extension类中,我只想在安装了其他捆绑包时才加载一些服务。但是,由于传递给 load 方法的ContainerBuilder是临时空的,因此我无法使用诸如$container->hasExtension("foo_bar")如何实现的方法?

所有捆绑包都在 kernel.bundles 参数中。使用它来检查是否有可用。例如:

public function load(ContainerBuilder $container, array $configs)
{
    $bundles = $container->getParameter('kernel.bundles');
    if (isset($bundles['AcmeDemoBundle'])) {
        // ...
    }
    /* or
    if (in_array('Acme'DemoBundle'AcmeDemoBundle', $bundles)) {
        // ...
    }
    */
}

如果要调整其他捆绑包的服务,或者需要读取所有其他服务,则需要使用编译器传递。