【问题标题】:How to override DependencyInjection part of bundle?如何覆盖捆绑包的 DependencyInjection 部分?
【发布时间】:2013-04-03 20:50:15
【问题描述】:

我正在尝试覆盖 FOSElasticaExtension https://github.com/FriendsOfSymfony/FOSElasticaBundle 的部分内容,以通过包继承处理配置和参数的方式略有不同(这里有些描述 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

【问题讨论】:

    标签: symfony


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多