make RegisterKernelListenersPass reusable
This commit is contained in:
parent
e9b6c7c2a4
commit
45f1a16b93
@ -14,17 +14,47 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
|||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiler pass to register tagged services for an event dispatcher.
|
||||||
|
*/
|
||||||
class RegisterKernelListenersPass implements CompilerPassInterface
|
class RegisterKernelListenersPass implements CompilerPassInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $dispatcherService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $listenerTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $subscriberTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $dispatcherService Service name of the event dispatcher in processed container
|
||||||
|
* @param string $listenerTag Tag name used for listener
|
||||||
|
* @param string $listenerTag Tag name used for subscribers
|
||||||
|
*/
|
||||||
|
public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
|
||||||
|
{
|
||||||
|
$this->dispatcherService = $dispatcherService;
|
||||||
|
$this->listenerTag = $listenerTag;
|
||||||
|
$this->subscriberTag = $subscriberTag;
|
||||||
|
}
|
||||||
|
|
||||||
public function process(ContainerBuilder $container)
|
public function process(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
if (!$container->hasDefinition('event_dispatcher')) {
|
if (!$container->hasDefinition($this->dispatcherService)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$definition = $container->getDefinition('event_dispatcher');
|
$definition = $container->getDefinition($this->dispatcherService);
|
||||||
|
|
||||||
foreach ($container->findTaggedServiceIds('kernel.event_listener') as $id => $events) {
|
foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
|
||||||
foreach ($events as $event) {
|
foreach ($events as $event) {
|
||||||
$priority = isset($event['priority']) ? $event['priority'] : 0;
|
$priority = isset($event['priority']) ? $event['priority'] : 0;
|
||||||
|
|
||||||
@ -44,7 +74,7 @@ class RegisterKernelListenersPass implements CompilerPassInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($container->findTaggedServiceIds('kernel.event_subscriber') as $id => $attributes) {
|
foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
|
||||||
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
||||||
$class = $container->getDefinition($id)->getClass();
|
$class = $container->getDefinition($id)->getClass();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user