Skip to content

Commit

Permalink
make RegisterKernelListenersPass reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandresalome authored and fabpot committed Apr 25, 2013
1 parent e9b6c7c commit 45f1a16
Showing 1 changed file with 34 additions and 4 deletions.
Expand Up @@ -14,17 +14,47 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* Compiler pass to register tagged services for an event dispatcher.
*/
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)
{
if (!$container->hasDefinition('event_dispatcher')) {
if (!$container->hasDefinition($this->dispatcherService)) {
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) {
$priority = isset($event['priority']) ? $event['priority'] : 0;

Expand All @@ -44,7 +74,7 @@ public function process(ContainerBuilder $container)
}
}

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
$class = $container->getDefinition($id)->getClass();

Expand Down

0 comments on commit 45f1a16

Please sign in to comment.