Symfony EventDispatcher 服务标签不在 Symfony 应用程序中
Symfony EventDispatcher Service Tags Not Inside Symfony App
在 Symfony 应用程序中使用 Symfony 2.8 事件分配器和容器组件不
来自我的 bootstrap/kernel 文件:
$this->container = new ContainerBuilder(new ParameterBag());
$this->getContainer()->addCompilerPass(new RegisterListenersPass());
$this->getContainer()->register('event_dispatcher', EventDispatcher::class);
$this->loadServiceConfig(); // See below for reference
/** @var EventDispatcher $ed */
$ed = $this->getContainer()->get('event_dispatcher');
// The next line works
$ed->addSubscriber(new SampleSubscriber());
...
private function loadServiceConfig()
{
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__);
$loader->load('config/services.yml');
}
来自config/services.yml
:
services:
sample_subscriber:
class: Sample\Event\SampleSubscriber
public: true
tags:
- { name: kernel.event_subscriber }
手动订阅有效,但我希望根据 yaml 文件中的标签自动附加事件
我创建了一个独立的 repo,展示了如何设置容器、事件调度程序和 RegisterListenersPass:
https://github.com/simshaun/so-45003754
Symfony 2.8 需要 ContainerAwareEventDispatcher
才能使 RegisterListenersPass
正常运行。
这是 setup/wiring 的内容:
<?php
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
require __DIR__.'/vendor/autoload.php';
class App
{
protected $container;
public function __construct()
{
$builder = new ContainerBuilder(new ParameterBag());
$builder->addCompilerPass(new RegisterListenersPass());
$definition = new Definition(ContainerAwareEventDispatcher::class, [new Reference('service_container')]);
$builder->setDefinition('event_dispatcher', $definition);
$loader = new YamlFileLoader($builder, new FileLocator(__DIR__));
$loader->load('services.yml');
$builder->compile();
$this->container = $builder;
}
public function getEventDispatcher(): EventDispatcherInterface
{
return $this->container->get('event_dispatcher');
}
}
$app = new App();
$ed = $app->getEventDispatcher();
$ed->dispatch('my.event');
在 Symfony 应用程序中使用 Symfony 2.8 事件分配器和容器组件不
来自我的 bootstrap/kernel 文件:
$this->container = new ContainerBuilder(new ParameterBag());
$this->getContainer()->addCompilerPass(new RegisterListenersPass());
$this->getContainer()->register('event_dispatcher', EventDispatcher::class);
$this->loadServiceConfig(); // See below for reference
/** @var EventDispatcher $ed */
$ed = $this->getContainer()->get('event_dispatcher');
// The next line works
$ed->addSubscriber(new SampleSubscriber());
...
private function loadServiceConfig()
{
$loader = new YamlFileLoader($this->container, new FileLocator(__DIR__);
$loader->load('config/services.yml');
}
来自config/services.yml
:
services:
sample_subscriber:
class: Sample\Event\SampleSubscriber
public: true
tags:
- { name: kernel.event_subscriber }
手动订阅有效,但我希望根据 yaml 文件中的标签自动附加事件
我创建了一个独立的 repo,展示了如何设置容器、事件调度程序和 RegisterListenersPass:
https://github.com/simshaun/so-45003754
Symfony 2.8 需要 ContainerAwareEventDispatcher
才能使 RegisterListenersPass
正常运行。
这是 setup/wiring 的内容:
<?php
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
require __DIR__.'/vendor/autoload.php';
class App
{
protected $container;
public function __construct()
{
$builder = new ContainerBuilder(new ParameterBag());
$builder->addCompilerPass(new RegisterListenersPass());
$definition = new Definition(ContainerAwareEventDispatcher::class, [new Reference('service_container')]);
$builder->setDefinition('event_dispatcher', $definition);
$loader = new YamlFileLoader($builder, new FileLocator(__DIR__));
$loader->load('services.yml');
$builder->compile();
$this->container = $builder;
}
public function getEventDispatcher(): EventDispatcherInterface
{
return $this->container->get('event_dispatcher');
}
}
$app = new App();
$ed = $app->getEventDispatcher();
$ed->dispatch('my.event');