Skip to content

Commit 57d107c

Browse files
vshevelevfabpot
authored andcommitted
[FrameworkBundle] Update ContainerDebugCommand to add parial search for tags
1 parent ea3bc64 commit 57d107c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Command/ContainerDebugCommand.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
145145
} elseif ($input->getOption('tags')) {
146146
$options = ['group_by' => 'tags'];
147147
} elseif ($tag = $input->getOption('tag')) {
148+
$tag = $this->findProperTagName($input, $errorIo, $object, $tag);
148149
$options = ['tag' => $tag];
149150
} elseif ($name = $input->getArgument('name')) {
150151
$name = $this->findProperServiceName($input, $errorIo, $object, $name, $input->getOption('show-hidden'));
@@ -287,6 +288,24 @@ private function findProperServiceName(InputInterface $input, SymfonyStyle $io,
287288
return $io->choice('Select one of the following services to display its information', $matchingServices);
288289
}
289290

291+
private function findProperTagName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $builder, string $tagName): string
292+
{
293+
if (\in_array($tagName, $builder->findTags(), true) || !$input->isInteractive()) {
294+
return $tagName;
295+
}
296+
297+
$matchingTags = $this->findTagsContaining($builder, $tagName);
298+
if (!$matchingTags) {
299+
throw new InvalidArgumentException(sprintf('No tags found that match "%s".', $tagName));
300+
}
301+
302+
if (1 === \count($matchingTags)) {
303+
return $matchingTags[0];
304+
}
305+
306+
return $io->choice('Select one of the following tags to display its information', $matchingTags);
307+
}
308+
290309
private function findServiceIdsContaining(ContainerBuilder $builder, string $name, bool $showHidden): array
291310
{
292311
$serviceIds = $builder->getServiceIds();
@@ -306,6 +325,19 @@ private function findServiceIdsContaining(ContainerBuilder $builder, string $nam
306325
return $foundServiceIds ?: $foundServiceIdsIgnoringBackslashes;
307326
}
308327

328+
private function findTagsContaining(ContainerBuilder $builder, string $tagName): array
329+
{
330+
$tags = $builder->findTags();
331+
$foundTags = [];
332+
foreach ($tags as $tag) {
333+
if (str_contains($tag, $tagName)) {
334+
$foundTags[] = $tag;
335+
}
336+
}
337+
338+
return $foundTags;
339+
}
340+
309341
/**
310342
* @internal
311343
*/

Tests/Functional/ContainerDebugCommandTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ public function testIgnoreBackslashWhenFindingService(string $validServiceId)
100100
$this->assertStringNotContainsString('No services found', $tester->getDisplay());
101101
}
102102

103+
public function testTagsPartialSearch()
104+
{
105+
static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']);
106+
107+
$application = new Application(static::$kernel);
108+
$application->setAutoExit(false);
109+
110+
$tester = new ApplicationTester($application);
111+
$tester->setInputs(['0']);
112+
$tester->run(['command' => 'debug:container', '--tag' => 'kernel.']);
113+
114+
$this->assertStringContainsString('Select one of the following tags to display its information', $tester->getDisplay());
115+
$this->assertStringContainsString('[0] kernel.event_subscriber', $tester->getDisplay());
116+
$this->assertStringContainsString('[1] kernel.locale_aware', $tester->getDisplay());
117+
$this->assertStringContainsString('[2] kernel.cache_warmer', $tester->getDisplay());
118+
$this->assertStringContainsString('[3] kernel.fragment_renderer', $tester->getDisplay());
119+
$this->assertStringContainsString('[4] kernel.reset', $tester->getDisplay());
120+
$this->assertStringContainsString('[5] kernel.cache_clearer', $tester->getDisplay());
121+
$this->assertStringContainsString('Symfony Container Services Tagged with "kernel.event_subscriber" Tag', $tester->getDisplay());
122+
}
123+
103124
public function testDescribeEnvVars()
104125
{
105126
putenv('REAL=value');

0 commit comments

Comments
 (0)