Skip to content

Commit 16d889b

Browse files
TimoBakxderrabus
authored andcommitted
[FrameworkBundle] Added option to specify the event dispatcher in debug:event-dispatcher
1 parent 80056e7 commit 16d889b

File tree

8 files changed

+48
-10
lines changed

8 files changed

+48
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* Added support for configuring PHP error level to log levels
8+
* Added the `dispatcher` option to `debug:event-dispatcher`
9+
* Added the `event_dispatcher.dispatcher` tag
810

911
5.2.0
1012
-----

Command/EventDispatcherDebugCommand.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Psr\Container\ContainerInterface;
1415
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
1516
use Symfony\Component\Console\Command\Command;
1617
use Symfony\Component\Console\Input\InputArgument;
@@ -29,14 +30,16 @@
2930
*/
3031
class EventDispatcherDebugCommand extends Command
3132
{
33+
private const DEFAULT_DISPATCHER = 'event_dispatcher';
34+
3235
protected static $defaultName = 'debug:event-dispatcher';
33-
private $dispatcher;
36+
private $dispatchers;
3437

35-
public function __construct(EventDispatcherInterface $dispatcher)
38+
public function __construct(ContainerInterface $dispatchers)
3639
{
3740
parent::__construct();
3841

39-
$this->dispatcher = $dispatcher;
42+
$this->dispatchers = $dispatchers;
4043
}
4144

4245
/**
@@ -47,6 +50,7 @@ protected function configure()
4750
$this
4851
->setDefinition([
4952
new InputArgument('event', InputArgument::OPTIONAL, 'An event name or a part of the event name'),
53+
new InputOption('dispatcher', null, InputOption::VALUE_REQUIRED, 'To view events of a specific event dispatcher', self::DEFAULT_DISPATCHER),
5054
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
5155
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
5256
])
@@ -74,12 +78,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7478
$io = new SymfonyStyle($input, $output);
7579

7680
$options = [];
81+
$dispatcherServiceName = $input->getOption('dispatcher');
82+
if (!$this->dispatchers->has($dispatcherServiceName)) {
83+
$io->getErrorStyle()->error(sprintf('Event dispatcher "%s" is not available.', $dispatcherServiceName));
84+
85+
return 1;
86+
}
87+
88+
$dispatcher = $this->dispatchers->get($dispatcherServiceName);
89+
7790
if ($event = $input->getArgument('event')) {
78-
if ($this->dispatcher->hasListeners($event)) {
91+
if ($dispatcher->hasListeners($event)) {
7992
$options = ['event' => $event];
8093
} else {
8194
// if there is no direct match, try find partial matches
82-
$events = $this->searchForEvent($this->dispatcher, $event);
95+
$events = $this->searchForEvent($dispatcher, $event);
8396
if (0 === \count($events)) {
8497
$io->getErrorStyle()->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
8598

@@ -93,10 +106,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
93106
}
94107

95108
$helper = new DescriptorHelper();
109+
110+
if (self::DEFAULT_DISPATCHER !== $dispatcherServiceName) {
111+
$options['dispatcher_service_name'] = $dispatcherServiceName;
112+
}
113+
96114
$options['format'] = $input->getOption('format');
97115
$options['raw_text'] = $input->getOption('raw');
98116
$options['output'] = $io;
99-
$helper->describe($io, $this->dispatcher, $options);
117+
$helper->describe($io, $dispatcher, $options);
100118

101119
return 0;
102120
}

Console/Descriptor/MarkdownDescriptor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,14 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
287287
protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = [])
288288
{
289289
$event = \array_key_exists('event', $options) ? $options['event'] : null;
290+
$dispatcherServiceName = $options['dispatcher_service_name'] ?? null;
290291

291292
$title = 'Registered listeners';
293+
294+
if (null !== $dispatcherServiceName) {
295+
$title .= sprintf(' of event dispatcher "%s"', $dispatcherServiceName);
296+
}
297+
292298
if (null !== $event) {
293299
$title .= sprintf(' for event `%s` ordered by descending priority', $event);
294300
$registeredListeners = $eventDispatcher->getListeners($event);

Console/Descriptor/TextDescriptor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,19 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
474474
protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = [])
475475
{
476476
$event = \array_key_exists('event', $options) ? $options['event'] : null;
477+
$dispatcherServiceName = $options['dispatcher_service_name'] ?? null;
478+
479+
$title = 'Registered Listeners';
480+
481+
if (null !== $dispatcherServiceName) {
482+
$title .= sprintf(' of Event Dispatcher "%s"', $dispatcherServiceName);
483+
}
477484

478485
if (null !== $event) {
479-
$title = sprintf('Registered Listeners for "%s" Event', $event);
486+
$title .= sprintf(' for "%s" Event', $event);
480487
$registeredListeners = $eventDispatcher->getListeners($event);
481488
} else {
482-
$title = 'Registered Listeners Grouped by Event';
489+
$title .= ' Grouped by Event';
483490
// Try to see if "events" exists
484491
$registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events'], array_map(function ($event) use ($eventDispatcher) { return $eventDispatcher->getListeners($event); }, $options['events'])) : $eventDispatcher->getListeners();
485492
}

DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class UnusedTagsPass implements CompilerPassInterface
4343
'controller.argument_value_resolver',
4444
'controller.service_arguments',
4545
'data_collector',
46+
'event_dispatcher.dispatcher',
4647
'form.type',
4748
'form.type_extension',
4849
'form.type_guesser',
@@ -72,9 +73,9 @@ class UnusedTagsPass implements CompilerPassInterface
7273
'routing.expression_language_provider',
7374
'routing.loader',
7475
'routing.route_loader',
76+
'security.authenticator.login_linker',
7577
'security.expression_language_provider',
7678
'security.remember_me_aware',
77-
'security.authenticator.login_linker',
7879
'security.voter',
7980
'serializer.encoder',
8081
'serializer.normalizer',

DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
use Symfony\Contracts\Cache\CacheInterface;
161161
use Symfony\Contracts\Cache\CallbackInterface;
162162
use Symfony\Contracts\Cache\TagAwareCacheInterface;
163+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
163164
use Symfony\Contracts\HttpClient\HttpClientInterface;
164165
use Symfony\Contracts\Service\ResetInterface;
165166
use Symfony\Contracts\Service\ServiceSubscriberInterface;
@@ -479,6 +480,8 @@ public function load(array $configs, ContainerBuilder $container)
479480
->addTag('kernel.cache_clearer');
480481
$container->registerForAutoconfiguration(CacheWarmerInterface::class)
481482
->addTag('kernel.cache_warmer');
483+
$container->registerForAutoconfiguration(EventDispatcherInterface::class)
484+
->addTag('event_dispatcher.dispatcher');
482485
$container->registerForAutoconfiguration(EventSubscriberInterface::class)
483486
->addTag('kernel.event_subscriber');
484487
$container->registerForAutoconfiguration(LocaleAwareInterface::class)

Resources/config/console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129

130130
->set('console.command.event_dispatcher_debug', EventDispatcherDebugCommand::class)
131131
->args([
132-
service('event_dispatcher'),
132+
tagged_locator('event_dispatcher.dispatcher'),
133133
])
134134
->tag('console.command', ['command' => 'debug:event-dispatcher'])
135135

Resources/config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
6565
->set('event_dispatcher', EventDispatcher::class)
6666
->public()
6767
->tag('container.hot_path')
68+
->tag('event_dispatcher.dispatcher')
6869
->alias(EventDispatcherInterfaceComponentAlias::class, 'event_dispatcher')
6970
->alias(EventDispatcherInterface::class, 'event_dispatcher')
7071

0 commit comments

Comments
 (0)