From ef8c518478b941e06ef5d6cf5137117b2fe7122e Mon Sep 17 00:00:00 2001 From: Adrien Jourdier Date: Sat, 30 Oct 2021 13:15:19 +0200 Subject: [PATCH] feat: add completion for DebugAutowiring search argument --- .../Command/DebugAutowiringCommand.php | 14 ++++++++++- .../Functional/DebugAutowiringCommandTest.php | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php index 7d0c5f0092513..e1e3c95341de3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php @@ -12,6 +12,8 @@ namespace Symfony\Bundle\FrameworkBundle\Command; use Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -81,7 +83,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $serviceIds = array_filter($serviceIds, [$this, 'filterToServiceTypes']); if ($search = $input->getArgument('search')) { - $searchNormalized = preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', '', $search); + $searchNormalized = preg_replace('/[^a-zA-Z0-9\x7f-\xff $]++/', '', $search); + $serviceIds = array_filter($serviceIds, function ($serviceId) use ($searchNormalized) { return false !== stripos(str_replace('\\', '', $serviceId), $searchNormalized) && !str_starts_with($serviceId, '.'); }); @@ -162,4 +165,13 @@ private function getFileLink(string $class): string return (string) $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine()); } + + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestArgumentValuesFor('search')) { + $builder = $this->getContainerBuilder($this->getApplication()->getKernel()); + + $suggestions->suggestValues(array_filter($builder->getServiceIds(), [$this, 'filterToServiceTypes'])); + } + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php index a0ade821d5165..c3110cc71dcbb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php @@ -11,8 +11,10 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; +use Symfony\Bundle\FrameworkBundle\Command\DebugAutowiringCommand; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\ApplicationTester; +use Symfony\Component\Console\Tester\CommandCompletionTester; /** * @group functional @@ -109,4 +111,26 @@ public function testNotConfusedByClassAliases() $tester->run(['command' => 'debug:autowiring', 'search' => 'ClassAlias']); $this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ClassAliasExampleClass', $tester->getDisplay()); } + + /** + * @dataProvider provideCompletionSuggestions + */ + public function testComplete(array $input, array $expectedSuggestions) + { + $kernel = static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']); + $command = (new Application($kernel))->add(new DebugAutowiringCommand()); + + $tester = new CommandCompletionTester($command); + + $suggestions = $tester->complete($input); + + foreach ($expectedSuggestions as $expectedSuggestion) { + $this->assertContains($expectedSuggestion, $suggestions); + } + } + + public function provideCompletionSuggestions(): \Generator + { + yield 'search' => [[''], ['SessionHandlerInterface', 'Psr\\Log\\LoggerInterface', 'Psr\\Container\\ContainerInterface $parameterBag']]; + } }