定义:addMethodCall有效,但从不调用方法

时间:2016-05-09 18:01:35

标签: symfony service tags

我想在我的服务上使用自定义标记,因此我按照文档中的说明进行操作:http://symfony.com/doc/2.8/components/dependency_injection/tags.html

我有一个RulesHydrator类:

<?php

namespace TestBundle\Thruway;

class RulesHydrator
{
    private $container;
    private $manualChecks = [];

    public function __construct($container)
    {
        $this->container = $container;
    }

    public function addManualCheck($service, $rule, $method)
    {
        echo 'addManualCheck invoked!'.PHP_EOL;
        exit;
        $this->manualChecks[$rule] = $service;
    }
}

这是编译器传递:

<?php

namespace TestBundle\Thruway;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;

class ThruwayCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if (!$container->has('thruway.rules_hydrator')) {
            return;
        }

        $definition = $container->findDefinition('thruway.rules_hydrator');

        foreach ($container->findTaggedServiceIds('thruway.manual_check') as $id => $tags) {
            foreach ($tags as $attributes) {
                $definition->addMethodCall('addManualCheck', [new Reference($id), $attributes['rule'], $attributes['method']]);
            }
        }
    }
}

这是我的包的类:

<?php

namespace TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use TestBundle\Thruway\ThruwayCompilerPass;

class TestBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new ThruwayCompilerPass());
    }
}

我的services.yml文件如下所示:

services:
    thruway.rules_hydrator:
        class: TestBundle\Thruway\RulesHydrator

    thruway.customer_checker:
        class: TestBundle\Thruway\MyChecker
        tags:
            - { name: thruway.manual_check, rule: some.rule1, method: someMethod1 }
            - { name: thruway.manual_check, rule: some.rule2, method: someMethod2 }

调用进程方法,对定义对象上的 addMethodCall 的不同调用正常工作(属性“调用”定义正确填写)。问题是我的方法 addManualCheck 的调用永远不会发生。知道为什么吗?

1 个答案:

答案 0 :(得分:0)

案例可能是您未实例化该服务。默认情况下,我记得服务是延迟加载的,直到你实际从容器中获取一个服务,或者它被注入到其他服务中,它将不会被初始化。

您可以在app / cache / prod中查看“TestBundle \ Thruway \ MyChecker”中的appProdProjectContainer.php并回复它的使用方式吗?

还可以通过从容器中获取thruway.customer_checker来尝试快速检查。

像这样的快速命令可以帮助

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class TestCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('service:test')->setDescription('Test service functionalities');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $service = $this->getContainer()->get('thruway.customer_checker');
    }
}