10000 [Framework] Add completion to `debug:event-dispatcher` by GromNaN · Pull Request #44321 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Framework] Add completion to debug:event-dispatcher #44321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;

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

< 10000 /span>
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('event')) {
$dispatcherServiceName = $input->getOption('dispatcher');
if ($this->dispatchers->has($dispatcherServiceName)) {
$dispatcher = $this->dispatchers->get($dispatcherServiceName);
$suggestions->suggestValues(array_keys($dispatcher->getListeners()));
}

return;
}

if ($input->mustSuggestOptionValuesFor('dispatcher')) {
if ($this->dispatchers instanceof ServiceProviderInterface) {
$suggestions->suggestValues(array_keys($this->dispatchers->getProvidedServices()));
}

return;
}

if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues((new DescriptorHelper())->getFormats());
}
}

private function searchForEvent(EventDispatcherInterface $dispatcher, string $needle): array
{
$output = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\EventDispatcherDebugCommand;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\EventDispatcher\EventDispatcher;

class EventDispatcherDebugCommandTest extends TestCase
{
/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$tester = $this->createCommandCompletionTester();

$suggestions = $tester->complete($input);

$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions()
{
yield 'event' => [[''], ['Symfony\Component\Mailer\Event\MessageEvent', 'console.command']];
yield 'event for other dispatcher' => [['--dispatcher', 'other_event_dispatcher', ''], ['other_event', 'App\OtherEvent']];
yield 'dispatcher' => [['--dispatcher='], ['event_dispatcher', 'other_event_dispatcher']];
yield 'format' => [['--format='], ['txt', 'xml', 'json', 'md']];
}

private function createCommandCompletionTester(): CommandCompletionTester
{
$dispatcher = new EventDispatcher();
$otherDispatcher = new EventDispatcher();

$dispatcher->addListener('event', 'Listener');
$otherDispatcher->addListener('other_event', 'OtherListener');
Copy link
Member Author
@GromNaN GromNaN Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those 5 lines are useless. They should be removed.


$dispatchers = new ServiceLocator([
'event_dispatcher' => function () {
$dispatcher = new EventDispatcher();
$dispatcher->addListener('Symfony\Component\Mailer\Event\MessageEvent', 'var_dump');
$dispatcher->addListener('console.command', 'var_dump');

return $dispatcher;
},
'other_event_dispatcher' => function () {
$dispatcher = new EventDispatcher();
$dispatcher->addListener('other_event', 'var_dump');
$dispatcher->addListener('App\OtherEvent', 'var_dump');

return $dispatcher;
},
]);
$command = new EventDispatcherDebugCommand($dispatchers);

return new CommandCompletionTester($command);
}
}
0