Skip to content

Commit 22f7151

Browse files
committed
[FrameworkBundle] container:debug : list services if no service match exacly the name argument
| Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11303 | License | MIT | Doc PR | If we launch the command "container:debug log" and there is no service nammed log, it will print something like this : ``` [0] logger [1] monolog.handler.console [2] monolog.handler.debug ``` After the service has been chosen, usual container:debug informations are displayed.
1 parent 8b54211 commit 22f7151

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
2121
use Symfony\Component\Config\FileLocator;
22+
use Symfony\Component\Console\Question\ChoiceQuestion;
2223

2324
/**
2425
* A console command for retrieving information about services.
@@ -106,6 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
106107
$options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
107108
} elseif ($name = $input->getArgument('name')) {
108109
$object = $this->getContainerBuilder();
110+
$name = $this->findProperServiceName($input, $output, $object, $name);
109111
$options = array('id' => $name);
110112
} else {
111113
$object = $this->getContainerBuilder();
@@ -116,6 +118,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
116118
$options['format'] = $input->getOption('format');
117119
$options['raw_text'] = $input->getOption('raw');
118120
$helper->describe($output, $object, $options);
121+
122+
if (!$input->getArgument('name') && $input->isInteractive()) {
123+
$output->writeln('To search for a service, re-run this command with a search term. <comment>container:debug log</comment>');
124+
}
119125
}
120126

121127
/**
@@ -168,4 +174,30 @@ protected function getContainerBuilder()
168174

169175
return $container;
170176
}
177+
178+
private function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)
179+
{
180+
if ($builder->has($name) || !$input->isInteractive()) {
181+
return $name;
182+
}
183+
184+
$question = new ChoiceQuestion('Choose a number for more information on the service', $this->findServiceIdsContaining($builder, $name));
185+
$question->setErrorMessage('Service %s is invalid.');
186+
187+
return $this->getHelper('question')->ask($input, $output, $question);
188+
}
189+
190+
private function findServiceIdsContaining(ContainerBuilder $builder, $name)
191+
{
192+
$serviceIds = $builder->getServiceIds();
193+
$foundServiceIds = array();
194+
foreach ($serviceIds as $serviceId) {
195+
if (false === strpos($serviceId, $name)) {
196+
continue;
197+
}
198+
$foundServiceIds[] = $serviceId;
199+
}
200+
201+
return $foundServiceIds;
202+
}
171203
}

0 commit comments

Comments
 (0)