8000 feature #39323 Search for pattern on debug:event-dispatcher (Nyholm) · symfony/symfony@caeac51 · GitHub
[go: up one dir, main page]

Skip to content

Commit caeac51

Browse files
committed
feature #39323 Search for pattern on debug:event-dispatcher (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- Search for pattern on debug:event-dispatcher | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | When running `bin/console event-dispatcher` you may pass it an argument to show what event you are interested to see the listeners for. Example: ``` bin/console event-dispatcher kernel.request bin/console debug:event-dispatcher "Symfony\Component\Security\Http\Event\CheckPassportEvent" ``` Since more and more events are using FQCN now, this is a handful to write. Other questions that I face is, should I use single backslash `\` or double `\\`? This PR propose that you can use a pattern as second argument. If there is no exact match, it will search for similar event names. Say: ``` bin/console debug:event-dispatcher CheckPassportEvent bin/console debug:event-dispatcher checkpassport bin/console debug:event-dispatcher Security bin/console debug:event-dispatcher Security bin/console debug:event-dispatcher App ``` #SymfonyHackday Commits ------- ab31ee9 Search for pattern on debug:event-dispatcher
2 parents 37d823d + ab31ee9 commit caeac51

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function configure()
4646
{
4747
$this
4848
->setDefinition([
49-
new InputArgument('event', InputArgument::OPTIONAL, 'An event name'),
49+
new InputArgument('event', InputArgument::OPTIONAL, 'An event name or a part of the event name'),
5050
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
5151
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
5252
])
@@ -75,13 +75,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7575

7676
$options = [];
7777
if ($event = $input->getArgument('event')) {
78-
if (!$this->dispatcher->hasListeners($event)) {
79-
$io->getErrorStyle()->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
80-
81-
return 0;
78+
if ($this->dispatcher->hasListeners($event)) {
79+
$options = ['event' => $event];
80+
} else {
81+
// if there is no direct match, try find partial matches
82+
$events = $this->searchForEvent($this->dispatcher, $event);
83+
if (0 === \count($events)) {
84+
$io->getErrorStyle()->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
85+
86+
return 0;
87+
} elseif (1 === \count($events)) {
88+
$options = ['event' => $events[array_key_first($events)]];
89+
} else {
90+
$options = ['events' => $events];
91+
}
8292
}
83-
84-
$options = ['event' => $event];
8593
}
8694

8795
$helper = new DescriptorHelper();
@@ -92,4 +100,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
92100

93101
return 0;
94102
}
103+
104+
private function searchForEvent(EventDispatcherInterface $dispatcher, $needle): array
105+
{
106+
$output = [];
107+
$allEvents = array_keys($dispatcher->getListeners());
108+
foreach ($allEvents as $event) {
109+
if (str_contains($event, $needle)) {
110+
$output[] = $event;
111+
}
112+
}
113+
114+
return $output;
115+
}
95116
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected function describeContainerAlias(Alias $alias, array $options = [], Con
135135

136136
protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = [])
137137
{
138-
$this->writeData($this->getEventDispatcherListenersData($eventDispatcher, \array_key_exists('event', $options) ? $options['event'] : null), $options);
138+
$this->writeData($this->getEventDispatcherListenersData($eventDispatcher, $options), $options);
139139
}
140140

141141
protected function describeCallable($callable, array $options = [])
@@ -274,18 +274,19 @@ private function getContainerAliasData(Alias $alias): array
274274
];
275275
}
276276

277-
private function getEventDispatcherListenersData(EventDispatcherInterfac 57AE e $eventDispatcher, string $event = null): array
277+
private function getEventDispatcherListenersData(EventDispatcherInterface $eventDispatcher, array $options): array
278278
{
279279
$data = [];
280+
$event = \array_key_exists('event', $options) ? $options['event'] : null;
280281

281-
$registeredListeners = $eventDispatcher->getListeners($event);
282282
if (null !== $event) {
283-
foreach ($registeredListeners as $listener) {
283+
foreach ($eventDispatcher->getListeners($event) as $listener) {
284284
$l = $this->getCallableData($listener);
285285
$l['priority'] = $eventDispatcher->getListenerPriority($event, $listener);
286286
$data[] = $l;
287287
}
288288
} else {
289+
$registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events'], array_map(function ($event) use ($eventDispatcher) { return $eventDispatcher->getListeners($event); }, $options['events'])) : $eventDispatcher->getListeners();
289290
ksort($registeredListeners);
290291

291292
foreach ($registeredListeners as $eventListened => $eventListeners) {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,14 @@ protected function describeEventDispatcherListeners(EventDispatcherInterface $ev
291291
$title = 'Registered listeners';
292292
if (null !== $event) {
293293
$title .= sprintf(' for event `%s` ordered by descending priority', $event);
294+
$registeredListeners = $eventDispatcher->getListeners($event);
295+
} else {
296+
// Try to see if "events" exists
297+
$registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events'], array_map(function ($event) use ($eventDispatcher) { return $eventDispatcher->getListeners($event); }, $options['events'])) : $eventDispatcher->getListeners();
294298
}
295299

296300
$this->write(sprintf('# %s', $title)."\n");
297301

298-
$registeredListeners = $eventDispatcher->getListeners($event);
299302
if (null !== $event) {
300303
foreach ($registeredListeners as $order => $listener) {
301304
$this->write("\n".sprintf('## Listener %d', $order + 1)."\n");

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,14 @@ protected function describeEventDispatcherListeners(EventDispatcherInterface $ev
477477

478478
if (null !== $event) {
479479
$title = sprintf('Registered Listeners for "%s" Event', $event);
480+
$registeredListeners = $eventDispatcher->getListeners($event);
480481
} else {
481482
$title = 'Registered Listeners Grouped by Event';
483+
// Try to see if "events" exists
484+
$registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events'], array_map(function ($event) use ($eventDispatcher) { return $eventDispatcher->getListeners($event); }, $options['events'])) : $eventDispatcher->getListeners();
482485
}
483486

484487
$options['output']->title($title);
485-
486-
$registeredListeners = $eventDispatcher->getListeners($event);
487488
if (null !== $event) {
488489
$this->renderEventListenerTable($eventDispatcher, $event, $registeredListeners, $options['output']);
489490
} else {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function describeContainerAlias(Alias $alias, array $options = [], Con
8989

9090
protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = [])
9191
{
92-
$this->writeDocument($this->getEventDispatcherListenersDocument($eventDispatcher, \array_key_exists('event', $options) ? $options['event'] : null));
92+
$this->writeDocument($this->getEventDispatcherListenersDocument($eventDispatcher, $options));
9393
}
9494

9595
protected function describeCallable($callable, array $options = [])
@@ -454,15 +454,18 @@ private function getContainerParameterDocument($parameter, array $options = []):
454454
return $dom;
455455
}
456456

457-
private function getEventDispatcherListenersDocument(EventDispatcherInterface $eventDispatcher, string $event = null): \DOMDocument
457+
private function getEventDispatcherListenersDocument(EventDispatcherInterface $eventDispatcher, array $options): \DOMDocument
458458
{
459+
$event = \array_key_exists('event', $options) ? $options['event'] : null;
459460
$dom = new \DOMDocument('1.0', 'UTF-8');
460461
$dom->appendChild($eventDispatcherXML = $dom->createElement('event-dispatcher'));
461462

462-
$registeredListeners = $eventDispatcher->getListeners($event);
463463
if (null !== $event) {
464+
$registeredListeners = $eventDispatcher->getListeners($event);
464465
$this->appendEventListenerDocument($eventDispatcher, $event, $eventDispatcherXML, $registeredListeners);
465466
} else {
467+
// Try to see if "events" exists
468+
$registeredListeners = \array_key_exists('events', $options) ? array_combine($options['events'], array_map(function ($event) use ($eventDispatcher) { return $eventDispatcher->getListeners($event); }, $options['events'])) : $eventDispatcher->getListeners();
466469
ksort($registeredListeners);
467470

468471
foreach ($registeredListeners as $eventListened => $eventListeners) {

0 commit comments

Comments
 (0)
0