8000 Complete event name & dispatcher in EventDispatcherDebugCommand · symfony/symfony@97160dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 97160dd

Browse files
stephenkhooGromNaN
authored andcommitted
Complete event name & dispatcher in EventDispatcherDebugCommand
1 parent f43a1ac commit 97160dd

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Completion\CompletionInput;
18+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1719
use Symfony\Component\Console\Input\InputArgument;
1820
use Symfony\Component\Console\Input\InputInterface;
1921
use Symfony\Component\Console\Input\InputOption;
2022
use Symfony\Component\Console\Output\OutputInterface;
2123
use Symfony\Component\Console\Style\SymfonyStyle;
2224
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25+
use Symfony\Contracts\Service\ServiceProviderInterface;
2326

2427
/**
2528
* A console command for retrieving information about event dispatcher.
@@ -120,6 +123,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int
120123
return 0;
121124
}
122125

126+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
127+
{
128+
if ($input->mustSuggestArgumentValuesFor('event')) {
129+
$dispatcherServiceName = $input->getOption('dispatcher');
130+
if ($this->dispatchers->has($dispatcherServiceName)) {
131+
$dispatcher = $this->dispatchers->get($dispatcherServiceName);
132+
$suggestions->suggestValues(array_keys($dispatcher->getListeners()));
133+
}
134+
135+
return;
136+
}
137+
138+
if ($input->mustSuggestOptionValuesFor('dispatcher')) {
139+
if ($this->dispatchers instanceof ServiceProviderInterface) {
140+
$suggestions->suggestValues(array_keys($this->dispatchers->getProvidedServices()));
141+
}
142+
143+
return;
144+
}
145+
146+
if ($input->mustSuggestOptionValuesFor('format')) {
147+
$suggestions->suggestValues((new DescriptorHelper())->getFormats());
148+
}
149+
}
150+
123151
private function searchForEvent(EventDispatcherInterface $dispatcher, string $needle): array
124152
{
125153
$output = [];
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\Command\EventDispatcherDebugCommand;
16+
use Symfony\Component\Console\Tester\CommandCompletionTester;
17+
use Symfony\Component\DependencyInjection\ServiceLocator;
18+
use Symfony\Component\EventDispatcher\EventDispatcher;
19+
20+
class EventDispatcherDebugCommandTest extends TestCase
21+
{
22+
/**
23+
* @dataProvider provideCompletionSuggestions
24+
*/
25+
public function testComplete(array $input, array $expectedSuggestions)
26+
{
27+
$tester = $this->createCommandCompletionTester();
28+
29+
$suggestions = $tester->complete($input);
30+
31+
$this->assertSame($expectedSuggestions, $suggestions);
32+
}
33+
34+
public function provideCompletionSuggestions()
35+
{
36+
yield 'event' => [[''], ['Symfony\Component\Mailer\Event\MessageEvent', 'console.command']];
37+
yield 'event for other dispatcher' => [['--dispatcher', 'other_event_dispatcher', ''], ['other_event', 'App\OtherEvent']];
38+
yield 'dispatcher' => [['--dispatcher='], ['event_dispatcher', 'other_event_dispatcher']];
39+
yield 'format' => [['--format='], ['txt', 'xml', 'json', 'md']];
40+
}
41+
42+
private function createCommandCompletionTester(): CommandCompletionTester
43+
{
44+
$dispatcher = new EventDispatcher();
45+
$otherDispatcher = new EventDispatcher();
46+
47+
$dispatcher->addListener('event', 'Listener');
48+
$otherDispatcher->addListener('other_event', 'OtherListener');
49+
50+
$dispatchers = new ServiceLocator([
51+
'event_dispatcher' => function () {
52+
$dispatcher = new EventDispatcher();
53+
$dispatcher->addListener('Symfony\Component\Mailer\Event\MessageEvent', 'var_dump');
54+
$dispatcher->addListener('console.command', 'var_dump');
55+
56+
return $dispatcher;
57+
},
58+
'other_event_dispatcher' => function () {
59+
$dispatcher = new EventDispatcher();
60+
$dispatcher->addListener('other_event', 'var_dump');
61+
$dispatcher->addListener('App\OtherEvent', 'var_dump');
62+
63+
return $dispatcher;
64+
},
65+
]);
66+
$command = new EventDispatcherDebugCommand($dispatchers);
67+
68+
return new CommandCompletionTester($command);
69+
}
70+
}

0 commit comments

Comments
 (0)
0