<p dir="auto"><strong>Symfony version(s) affected</strong>: 4.2.5</p>
<p dir="auto"><strong>Description</strong><br>
If some listeners are removed from <code class="notranslate">ContainerAwareEventManager</code> before being initialized they can not be added back if any event was dispatched in the meantime. This happens because <a href="https://github.com/symfony/symfony/blob/2243bf5bc11e3908f975f92551bd869a61211999/src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php#L63"><code class="notranslate">dispatchEvent</code> marks specific groups of events as initialized</a> BUT <code class="notranslate">addEventListener</code> checks <a href="https://github.com/symfony/symfony/blob/2243bf5bc11e3908f975f92551bd869a61211999/src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php#L102">whether anything was intialized</a></p>
<p dir="auto"><strong>Possible Solution</strong><br>
Maybe if a listeners is a string the container should be always checked whether it has a service defined under that name? That should allow adding lazy loaded listeners after construction. Other option might be to initialize listeners in <code class="notranslate">getListeners</code> but I don't know how it is used internally by Symfony.</p>
<p dir="auto"><strong>Additional context</strong><br>
In our app we're disabling and re-enabling Doctrine events mostly to disable FosElastica events for batch operations. This is no longer possible.</p>
<p dir="auto">in src/Symfony/Bundle/FrameworkBundle/Console/Application.php</p>
<p dir="auto">use Symfony\Component\HttpKernel\Bundle\Bundle; instead of Symfony\Component\HttpKernel\Bundle;</p>