如何覆盖bundle的DependencyInjection部分?

时间:2013-04-03 20:50:15

标签: symfony

我试图覆盖FOSElasticaExtension https://github.com/FriendsOfSymfony/FOSElasticaBundle的部分内容,通过bundle继承(稍后在这里描述http://symfony.com/doc/master/cookbook/bundles/override.html)略微区别地处理配置和参数。不幸的是,缺少这方面的文档,并且在尝试覆盖时我得到了奇怪的错误(比如配置根本没有注入)。

vendor/acme/Acme/ElasticaBundle/AcmeElasticaBundle.php

<?php
namespace Acme\ElasticaBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
 * Extends FOS Elastica bundle.
 */
class AcmeElasticaBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSElasticaBundle';
    }
}

vendor/acme/Acme/ElasticaBundle/DependencyInjection/Configuration.php

<?php
namespace Acme\ElasticaBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

use FOS\ElasticaBundle\DependencyInjection\Configuration as BaseConfiguration;

class Configuration extends BaseConfiguration
{
}

vendor/acme/Acme/ElasticaBundle/DependencyInjection/AcmeElasticaExtension.php

<?php
namespace Acme\ElasticaBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;

use FOS\ElasticaBundle\DependencyInjection\FOSElasticaExtension as Extension;

/**
 * Overrides some parts of FOS Elastica extension.
 */
class AcmeElasticaExtension extends Extension
{
}

app/AppKernel.php

<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new FOS\ElasticaBundle\FOSElasticaBundle(),
            new Acme\ElasticaBundle\PhoenixElasticaBundle(),
        );
    }
}

错误:

ErrorException: Notice: Undefined index: indexes in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 367

就是这一行:https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/DependencyInjection/Configuration.php#L367

我没有更改配置,所以我不确定原因是什么。我认为他们的代码验证配置运行正常并检索值,但MergeExtensionConfigurationPass似乎不适合这个:

in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 367
at ErrorHandler->handle('8', 'Undefined index: indexes', '/home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php', '367', array('nestings' => array())) in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 367
at Configuration->getNestings() in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 283
at Configuration->getMappingsNode() in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 265
at Configuration->getTypesNode() in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 188
at Configuration->addIndexesSection(object(ArrayNodeDefinition)) in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/Configuration.php line 30
at Configuration->getConfigTreeBuilder() in /home/tatsh/test-site/vendor/symfony/symfony/src/Symfony/Component/Config/Definition/Processor.php line 52
at Processor->processConfiguration(object(Configuration), array(array())) in /home/tatsh/test-site/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/Extension.php line 103
at Extension->processConfiguration(object(Configuration), array(array())) in /home/tatsh/test-site/vendor/friendsofsymfony/elastica-bundle/FOS/ElasticaBundle/DependencyInjection/FOSElasticaExtension.php line 23
at FOSElasticaExtension->load(array(array()), object(ContainerBuilder)) in /home/tatsh/test-site/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php line 42
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in /home/tatsh/test-site/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DependencyInjection/MergeExtensionConfigurationPass.php line 39
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in /home/tatsh/test-site/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/Compiler.php line 119
at Compiler->compile(object(ContainerBuilder)) in /home/tatsh/test-site/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ContainerBuilder.php line 453
at ContainerBuilder->compile() in /home/tatsh/test-site/app/bootstrap.php.cache line 950
at Kernel->buildContainer() in /home/tatsh/test-site/app/bootstrap.php.cache line 859
at Kernel->initializeContainer() in /home/tatsh/test-site/app/bootstrap.php.cache line 571
at Kernel->boot() in /home/tatsh/test-site/app/bootstrap.php.cache line 614
at Kernel->handle(object(Request)) in /home/tatsh/test-site/web/app_dev.php line 28

1 个答案:

答案 0 :(得分:0)

简便的修复方法:即使存根也实现load()方法。不要调用父load()方法。

<?php
/**
 * Overrides some parts of FOS Elastica extension.
 */
class AcmeElasticaExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
    }
}