Skip to content

Commit

Permalink
feature #43838 feat: add completion for DebugAutowiring search argume…
Browse files Browse the repository at this point in the history
…nt (eclairia, Adrien Jourdier)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

feat: add completion for DebugAutowiring search argument

| Q             | A
| ------------- | ---
| Branch?       | 5.4
 Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #43594
| License       | MIT
| Doc PR        | -

Adding Bash completion debug:autowiring command.

Commits
-------

ef8c518 feat: add completion for DebugAutowiring search argument
  • Loading branch information
fabpot committed Nov 4, 2021
2 parents 873e8ab + ef8c518 commit 7acf15e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '.');
});
Expand Down Expand Up @@ -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']));
}
}
}
Expand Up @@ -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
Expand Down Expand Up @@ -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']];
}
}

0 comments on commit 7acf15e

Please sign in to comment.