首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定义: addMethodCall起作用,但从未调用过方法

定义: addMethodCall起作用,但从未调用过方法
EN

Stack Overflow用户
提问于 2016-05-09 18:01:35
回答 1查看 1.1K关注 0票数 1

我想在我的服务上使用自定义标记,所以我遵循了文档中的说明:injection/tags.html

我有一个RulesHydrator类:

代码语言:javascript
复制
<?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;
    }
}

这是编译器的通行证:

代码语言:javascript
复制
<?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']]);
            }
        }
    }
}

这是我的捆绑包的类:

代码语言:javascript
复制
<?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文件如下所示:

代码语言:javascript
复制
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 }

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

EN

回答 1

Stack Overflow用户

发布于 2016-05-10 05:51:32

这种情况可能是您没有实例化服务。根据我的记忆,默认情况下,服务是延迟加载的,在您从容器中实际获取一个服务之前,或者它被注入到另一个服务中,它将不会被初始化。

您能在app/cache/prod中查看"TestBundle\Thruway\MyChecker“中的appProdProjectContainer.php并回发它的使用方式吗?

也尝试通过从容器中获取thruway.customer_checker来进行快速检查。

这样的快速命令可能会有所帮助。

代码语言:javascript
复制
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');
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37122455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档