dongweicha6077 2015-11-17 16:15
浏览 50
已采纳

在我的CompilerPass中配置第三方服务

I'm using Symfony 2.7 and I want to configure some service by adding configurator to it. I follow instructions on http://symfony.com/doc/current/components/dependency_injection/configurators.html, but I want to add my configurator to that service in my CompilerPass. I wrote the following code:

$container->getDefinition('exercise_html_purifier.config.default')
    ->setConfigurator([
        $container->getDefinition('application.exercise_html_purifier.config_configurator')->getClass(),
        'configure'
    ]);

where application.exercise_html_purifier.config_configurator is id of my configurator service. This code works as expected, but of course it is also triggers php's warning:

DEPRECATED - Non-static method Application\Service\ExerciseHTMLPurifier\Configurator\ConfigConfigurator::configure() should not be called statically, assuming $this from incompatible context

because the configure method is not static in my case. I can't figure out, how to tell symfony to set non-static configurator callback. I tried to set it like this:

$container->getDefinition('exercise_html_purifier.config.default')
    ->setConfigurator([
        $container->get('application.exercise_html_purifier.config_configurator'),
        'configure'
    ]);

but got this error:

ContextErrorException in XmlDumper.php line 201: Warning: DOMElement::setAttribute() expects parameter 2 to be string, object given

I even tried to use the following syntax:

$container->getDefinition('exercise_html_purifier.config.default')
    ->setConfigurator([
        '@application.exercise_html_purifier.config_configurator',
        'configure'
    ]);

but also got error

FatalErrorException in appDevDebugProjectContainer.php line 2992: Parse Error: syntax error, unexpected '@', expecting identifier (T_STRING)

What I'm doing wrong? Thanks.

  • 写回答

1条回答 默认 最新

  • doujiang7258 2015-11-17 17:39
    关注

    OK, I've read the source of \Symfony\Component\DependencyInjection\Dumper\XmlDumper::addService() and found that one of the solutions is to pass Definition instead of instance:

    $container->getDefinition('exercise_html_purifier.config.default')
        ->setConfigurator([
            $container->getDefinition('application.exercise_html_purifier.config_configurator'),
            'configure'
        ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?