Skip to content

Commit

Permalink
Allow registering listeners/subscribers on a specific dispatcher service
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Mar 27, 2020
1 parent e0de6cc commit 8156e7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/EventDispatcher/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* The `LegacyEventDispatcherProxy` class has been deprecated.
* Added an optional `dispatcher` attribute to the listener and subscriber tags in `RegisterListenerPass`.

5.0.0
-----
Expand Down
Expand Up @@ -61,7 +61,7 @@ public function process(ContainerBuilder $container)
} else {
$aliases = [];
}
$definition = $container->findDefinition($this->dispatcherService);
$globalDispatcherDefinition = $container->findDefinition($this->dispatcherService);

foreach ($container->findTaggedServiceIds($this->listenerTag, true) as $id => $events) {
foreach ($events as $event) {
Expand Down Expand Up @@ -90,7 +90,12 @@ public function process(ContainerBuilder $container)
}
}

$definition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]);
$dispatcherDefinition = $globalDispatcherDefinition;
if (isset($event['dispatcher'])) {
$dispatcherDefinition = $container->getDefinition($event['dispatcher']);
}

$dispatcherDefinition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]);

if (isset($this->hotPathEvents[$event['event']])) {
$container->getDefinition($id)->addTag($this->hotPathTagName);
Expand All @@ -100,7 +105,7 @@ public function process(ContainerBuilder $container)

$extractingDispatcher = new ExtractingEventDispatcher();

foreach ($container->findTaggedServiceIds($this->subscriberTag, true) as $id => $attributes) {
foreach ($container->findTaggedServiceIds($this->subscriberTag, true) as $id => $tags) {
$def = $container->getDefinition($id);

// We must assume that the class value has been correctly filled, even if the service is created by a factory
Expand All @@ -114,12 +119,20 @@ public function process(ContainerBuilder $container)
}
$class = $r->name;

$dispatcherDefinition = $globalDispatcherDefinition;
foreach ($tags as $attributes) {
if (isset($attributes['dispatcher'])) {
$dispatcherDefinition = $container->getDefinition($event['dispatcher']);
break;
}
}

ExtractingEventDispatcher::$aliases = $aliases;
ExtractingEventDispatcher::$subscriber = $class;
$extractingDispatcher->addSubscriber($extractingDispatcher);
foreach ($extractingDispatcher->listeners as $args) {
$args[1] = [new ServiceClosureArgument(new Reference($id)), $args[1]];
$definition->addMethodCall('addListener', $args);
$dispatcherDefinition->addMethodCall('addListener', $args);

if (isset($this->hotPathEvents[$args[0]])) {
$container->getDefinition($id)->addTag($this->hotPathTagName);
Expand Down

0 comments on commit 8156e7e

Please sign in to comment.