Skip to content

Commit 0ebd8e8

Browse files
committed
feat: add completion for DebugAutowiring search argument
1 parent 85bf403 commit 0ebd8e8

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1618
use Symfony\Component\Console\Input\InputArgument;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Input\InputOption;
1921
use Symfony\Component\Console\Output\OutputInterface;
2022
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\DependencyInjection\ContainerBuilder;
2124
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
2225

2326
/**
@@ -77,11 +80,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7780
$errorIo = $io->getErrorStyle();
7881

7982
$builder = $this->getContainerBuilder($this->getApplication()->getKernel());
80-
$serviceIds = $builder->getServiceIds();
81-
$serviceIds = array_filter($serviceIds, [$this, 'filterToServiceTypes']);
83+
$serviceIds = $this->getServices($builder);
8284

8385
if ($search = $input->getArgument('search')) {
84-
$searchNormalized = preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', '', $search);
86+
$searchNormalized = $this->getSearchNormalized($search);
87+
8588
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($searchNormalized) {
8689
return false !== stripos(str_replace('\\', '', $serviceId), $searchNormalized) && !str_starts_with($serviceId, '.');
8790
});
@@ -162,4 +165,33 @@ private function getFileLink(string $class): string
162165

163166
return (string) $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine());
164167
}
168+
169+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
170+
{
171+
$builder = $this->getContainerBuilder($this->getApplication()->getKernel());
172+
$serviceIds = $this->getServices($builder);
173+
174+
$searchNormalized = $this->getSearchNormalized($input->getArgument('search'));
175+
176+
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($searchNormalized) {
177+
$serviceId = str_replace('\\', '', $serviceId);
178+
$serviceId = str_replace('.', '', $serviceId);
179+
180+
return false !== stripos($serviceId, $searchNormalized) && !str_starts_with($serviceId, '.');
181+
});
182+
183+
$suggestions->suggestValues($serviceIds);
184+
}
185+
186+
private function getServices(ContainerBuilder $builder): array
187+
{
188+
$serviceIds = $builder->getServiceIds();
189+
190+
return array_filter($serviceIds, [$this, 'filterToServiceTypes']);
191+
}
192+
193+
private function getSearchNormalized(string $search): string
194+
{
195+
return preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', '', $search);
196+
}
165197
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

14+
use Symfony\Bundle\FrameworkBundle\Command\DebugAutowiringCommand;
1415
use Symfony\Bundle\FrameworkBundle\Console\Application;
1516
use Symfony\Component\Console\Tester\ApplicationTester;
17+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1618

1719
/**
1820
* @group functional
@@ -109,4 +111,29 @@ public function testNotConfusedByClassAliases()
109111
$tester->run(['command' => 'debug:autowiring', 'search' => 'ClassAlias']);
110112
$this->assertStringContainsString('Symfony\Bundle\FrameworkBundle\Tests\Fixtures\ClassAliasExampleClass', $tester->getDisplay());
111113
}
114+
115+
/**
116+
* @dataProvider provideCompletionSuggestions
117+
*/
118+
public function testCompletingName(array $input, array $expectedSuggestions)
119+
{
120+
$kernel = static::bootKernel(['test_case' => 'BundlePaths']);
121+
$command = (new Application($kernel))->add(new DebugAutowiringCommand());
122+
123+
$tester = new CommandCompletionTester($command);
124+
125+
$suggestions = $tester->complete($input);
126+
127+
$this->assertSame($expectedSuggestions, $suggestions);
128+
}
129+
130+
public function provideCompletionSuggestions(): \Generator
131+
{
132+
yield 'search' => [
133+
['twig'],
134+
[
135+
'Twig\Environment'
136+
],
137+
];
138+
}
112139
}

0 commit comments

Comments
 (0)