在Symfony2中使用标记服务的Bug

时间:2015-10-20 20:34:41

标签: php symfony dependency-injection

我有以下服务(仅限):

# app/config/services.yml
services:
    app.google_service_gmail.watcher_manager:
        class: AppBundle\Provider\Google\Service\Gmail\Watch\WatcherManager

    app.google_service_gmail.mail_watcher:
        class: AppBundle\Provider\Google\Service\Gmail\Watch\MailWatcher
        tags:
            - { name: app.watcher }

我有WatcherManager课程:

namespace AppBundle\Provider\Google\Service\Gmail\Watch;

class WatcherManager
{

    /**
     * @var WatcherInterface[]
     */
    private $watchers = [];

    /**
     * Add a Watcher
     *
     * @param WatcherInterface $watcher
     *
     * @return WatcherManager
     */
    public function addWatcher(WatcherInterface $watcher)
    {
        $this->watchers[] = $watcher;

        return $this;
    }
}

以下编译器传递类:

namespace AppBundle\DependencyInjection\CompilerPass;

use ...

class WatcherCompilerPass implements CompilerPassInterface
{

    /**
     * @inheritdoc
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->has('app.google_service_gmail.watcher_manager')) {
            return;
        }

        $definition = $container->findDefinition(
            'app.google_service_gmail.watcher_manager'
        );

        $taggedServices = $container->findTaggedServiceIds(
            'app.watcher'
        );

        foreach ($taggedServices as $id => $tags) {
            $definition->addMethodCall('addWatcher', [new Reference($id)]);
        }
    }
}

注册编译器传递:

namespace AppBundle;

use ...

class AppBundle extends Bundle
{

    /**
     * @inheritdoc
     */
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

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

获取执行任何控制台命令的以下异常:

  

[Symfony的\元器件\ DependencyInjection \异常\的RuntimeException]
   如果参数是对象或资源,则无法转储服务容器。

跨浏览器运行应用程序:

  

XmlDumper.php第355行中的RuntimeException:   如果参数是对象或资源,则无法转储服务容器。

in XmlDumper.php line 355
at XmlDumper::phpToXml(object(Reference)) in XmlDumper.php line 304
at XmlDumper->convertParameters(array(object(Reference)), 'argument', object(DOMElement)) in XmlDumper.php line 98
at XmlDumper->addMethodCalls(array(array('addWatcher', array(object(Reference)))), object(DOMElement)) in XmlDumper.php line 181
at XmlDumper->addService(object(Definition), 'app.google_service_gmail.watcher_manager', object(DOMElement)) in XmlDumper.php line 248
at XmlDumper->addServices(object(DOMElement)) in XmlDumper.php line 56
at XmlDumper->dump() in ContainerBuilderDebugDumpPass.php line 34
at ContainerBuilderDebugDumpPass->process(object(ContainerBuilder)) in Compiler.php line 117
at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 614
at ContainerBuilder->compile() in bootstrap.php.cache line 2633
at Kernel->initializeContainer() in bootstrap.php.cache line 2411
at Kernel->boot() in bootstrap.php.cache line 2442
at Kernel->handle(object(Request)) in app_dev.php line 29

我查看了symfony文档:

并且代码完全相同。

如果我禁用此行代码:

$definition->addMethodCall('addWatcher', [new Reference($id)]);

一切正常。 任何想法?

0 个答案:

没有答案