Skip to content

Commit 86842ac

Browse files
committed
feature #39276 [FrameworkBundle] Added option to specify the event dispatcher in debug:event-dispatcher (TimoBakx)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [FrameworkBundle] Added option to specify the event dispatcher in debug:event-dispatcher | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | symfony/symfony-docs#14650 | Tags | #SymfonyHackday ### Description: In the recent security improvements, new event dispatchers were added (one for each firewall). These were not visible in the `debug:event-dispatcher` command. This PR adds an option to define a event dispatcher to show the events (and listeners) for that specific event dispatcher. Without any options, the default dispatcher is shown (identical to previous working) In order to be able to fetch specific event dispatchers, I tagged all event dispatchers with a new `kernel.event-dispatcher` tag. ### Usage: `bin/console debug:event-dispatcher --dispatcher=[servicename] [other options] [event]` ### Subtasks: - [x] Add `dispatcher` option - [x] Add `kernel.event_dispatcher` tag to all event dispatchers - [x] Add autoconfigure tag for services implementing `EventDispatcherInterface` - [x] Update Descriptor classes to specify the chosen dispatcher - [x] Update changelog - [x] Add documentation Commits ------- 62398b5 [FrameworkBundle] Added option to specify the event dispatcher in debug:event-dispatcher
2 parents ffab85c + 62398b5 commit 86842ac

File tree

9 files changed

+50
-11
lines changed

9 files changed

+50
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/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
-----

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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);

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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',

src/Symfony/Bundle/FrameworkBundle/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)

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
358358

359359
// Register Firewall-specific event dispatcher
360360
$firewallEventDispatcherId = 'security.event_dispatcher.'.$id;
361-
$container->register($firewallEventDispatcherId, EventDispatcher::class);
361+
$container->register($firewallEventDispatcherId, EventDispatcher::class)
362+
->addTag('event_dispatcher.dispatcher');
362363

363364
// Register listeners
364365
$listeners = [];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy