Using Symfony 2.8 Event Dispatcher and Container components not inside a Symfony App
From my bootstrap/kernel file:
$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');
}
From config/services.yml:
services:
sample_subscriber:
class: Sample\Event\SampleSubscriber
public: true
tags:
- { name: kernel.event_subscriber }
The manual subscription works, but I would expect the event to automatically attach given the tags from the yaml file
I created a standalone repo showing how to set up the container, event dispatcher, and the RegisterListenersPass:
https://github.com/simshaun/so-45003754
Symfony 2.8 needs a
ContainerAwareEventDispatcherfor theRegisterListenersPassto function.Here's the meat of the setup/wiring: