8000 [Framework] Add completion to debug:container · symfony/symfony@01fe89e · GitHub
[go: up one dir, main page]

Skip to content

Commit 01fe89e

Browse files
committed
[Framework] Add completion to debug:container
1 parent 175e3f7 commit 01fe89e

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
1515
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Completion\CompletionInput;
17+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1618
use Symfony\Component\Console\Exception\InvalidArgumentException;
1719
use Symfony\Component\Console\Input\InputArgument;
1820
use Symfony\Component\Console\Input\InputInterface;
@@ -190,6 +192,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
190192
return 0;
191193
}
192194

195+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
196+
{
197+
if ($input->mustSuggestOptionValuesFor('format')) {
198+
$helper = new DescriptorHelper();
199+
$suggestions->suggestValues($helper->getFormats());
200+
201+
return;
202+
}
203+
204+
$kernel = $this->getApplication()->getKernel();
205+
$object = $this->getContainerBuilder($kernel);
206+
207+
if ($input->mustSuggestArgumentValuesFor('name')
208+
&& !$input->getOption('tag') && !$input->getOption('tags')
209+
&& !$input->getOption('parameter') && !$input->getOption('parameters')
210+
&& !$input->getOption('env-var') && !$input->getOption('env-vars')
211+
&& !$input->getOption('types') && !$input->getOption('deprecations')
212+
) {
213+
$suggestions->suggestValues($this->findServiceIdsContaining(
214+
$object,
215+
$input->getCompletionValue(),
216+
(bool) $input->getOption('show-hidden')
217+
));
218+
219+
return;
220+
}
221+
222+
if ($input->mustSuggestOptionValuesFor('tag')) {
223+
$suggestions->suggestValues($object->findTags());
224+
225+
return;
226+
}
227+
228+
if ($input->mustSuggestOptionValuesFor('parameter')) {
229+
$suggestions->suggestValues(array_keys($object->getParameterBag()->all()));
230+
}
231+
}
232+
193233
/**
194234
* Validates input arguments and options.
195235
*
@@ -245,7 +285,7 @@ private function findServiceIdsContaining(ContainerBuilder $builder, string $nam
245285
if (false !== stripos(str_replace('\\', '', $serviceId), $name)) {
246286
$foundServiceIdsIgnoringBackslashes[] = $serviceId;
247287
}
248-
if (false !== stripos($serviceId, $name)) {
288+
if ('' === $name || false !== stripos($serviceId, $name)) {
249289
$foundServiceIds[] = $serviceId;
250290
}
251291
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Console\Application;
1515
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BackslashClass;
1616
use Symfony\Component\Console\Tester\ApplicationTester;
17+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1718

1819
/**
1920
* @group functional
@@ -211,4 +212,68 @@ public function provideIgnoreBackslashWhenFindingService()
211212
['\\'.BackslashClass::class],
212213
];
213214
}
215+
216+
/**
217+
* @dataProvider provideCompletionSuggestions
218+
*/
219+
public function testComplete(array $input, array $expectedSuggestions, array $notExpectedSuggestions = [])
220+
{
221+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml', 'debug' => true]);
222+
223+
$application = new Application(static::$kernel);
224+
$tester = new CommandCompletionTester($application->find('debug:container'));
225+
$suggestions = $tester->complete($input);
226+
227+
foreach ($expectedSuggestions as $expectedSuggestion) {
228+
$this->assertContains($expectedSuggestion, $suggestions);
229+
}
230+
foreach ($notExpectedSuggestions as $notExpectedSuggestion) {
231+
$this->assertNotContains($notExpectedSuggestion, $suggestions);
232+
}
233+
}
234+
235+
public function provideCompletionSuggestions()
236+
{
237+
$serviceId = 'console.command.container_debug';
238+
$hiddenServiceId = '.console.command.container_debug.lazy';
239+
$interfaceServiceId = 'Symfony\Component\HttpKernel\HttpKernelInterface';
240+
241+
yield 'name' => [
242+
[''],
243+
[$serviceId, $interfaceServiceId],
244+
[$hiddenServiceId],
245+
];
246+
247+
yield 'name (with hidden)' => [
248+
['--show-hidden', ''],
249+
[$serviceId, $interfaceServiceId, $hiddenServiceId],
250+
];
251+
252+
yield 'name (with current value)' => [
253+
['--show-hidden', 'console'],
254+
[$serviceId, $hiddenServiceId],
255+
[$interfaceServiceId],
256+
];
257+
258+
yield 'name (no suggestion with --tags)' => [
259+
['--tags', ''],
260+
[],
261+
[$serviceId, $interfaceServiceId, $hiddenServiceId],
262+
];
263+
264+
yield 'option --tag' => [
265+
['--tag', ''],
266+
['console.command'],
267+
];
268+
269+
yield 'option --parameter' => [
270+
['--parameter', ''],
271+
['kernel.debug'],
272+
];
273+
274+
yield 'option --format' => [
275+
['--format', ''],
276+
['txt', 'xml', 'json', 'md'],
277+
];
278+
}
214279
}

0 commit comments

Comments
 (0)
0