Skip to content

Add completion for DebugConfig name and path arguments #43850

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 4, 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
59 changes: 54 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -94,11 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$extensionAlias = $extension->getAlias();
$container = $this->compileContainer();

$config = $container->resolveEnvPlaceholders(
$container->getParameterBag()->resolveValue(
$this->getConfigForExtension($extension, $container)
)
);
$config = $this->getConfig($extension, $container);

if (null === $path = $input->getArgument('path')) {
$io->title(
Expand Down Expand Up @@ -188,4 +186,55 @@ private function getConfigForExtension(ExtensionInterface $extension, ContainerB

return (new Processor())->processConfiguration($configuration, $configs);
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('name')) {
$suggestions->suggestValues($this->getAvailableBundles(!preg_match('/^[A-Z]/', $input->getCompletionValue())));

return;
}

if ($input->mustSuggestArgumentValuesFor('path') && null !== $name = $input->getArgument('name')) {
try {
$config = $this->getConfig($this->findExtension($name), $this->compileContainer());
$paths = array_keys(self::buildPathsCompletion($config));
$suggestions->suggestValues($paths);
} catch (LogicException $e) {
}
}
}

private function getAvailableBundles(bool $alias): array
{
$availableBundles = [];
foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
$availableBundles[] = $alias ? $bundle->getContainerExtension()->getAlias() : $bundle->getName();
}

return $availableBundles;
}

private function getConfig(ExtensionInterface $extension, ContainerBuilder $container)
{
return $container->resolveEnvPlaceholders(
$container->getParameterBag()->resolveValue(
$this->getConfigForExtension($extension, $container)
)
);
}

private static function buildPathsCompletion(array $paths, string $prefix = ''): array
{
$completionPaths = [];
foreach ($paths as $key => $values) {
if (\is_array($values)) {
$completionPaths = $completionPaths + self::buildPathsCompletion($values, $prefix.$key.'.');
} else {
$completionPaths[$prefix.$key] = null;
}
}

return $completionPaths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;

/**
Expand Down Expand Up @@ -111,6 +113,31 @@ public function testDumpThrowsExceptionWhenDefaultConfigFallbackIsImpossible()
$tester->execute(['name' => 'ExtensionWithoutConfigTestBundle']);
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$this->application->add(new ConfigDebugCommand());

$tester = new CommandCompletionTester($this->application->get('debug:config'));

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

foreach ($expectedSuggestions as $expectedSuggestion) {
$this->assertContains($expectedSuggestion, $suggestions);
}
}

public function provideCompletionSuggestions(): \Generator
{
yield 'name' => [[''], ['default_config_test', 'extension_without_config_test', 'framework', 'test']];

yield 'name (started CamelCase)' => [['Fra'], ['DefaultConfigTestBundle', 'ExtensionWithoutConfigTestBundle', 'FrameworkBundle', 'TestBundle']];

yield 'name with existing path' => [['framework', ''], ['secret', 'router.resource', 'router.utf8', 'router.enabled', 'validation.enabled', 'default_locale']];
}

private function createCommandTester(): CommandTester
{
$command = $this->application->find('debug:config');
Expand Down