Skip to content

Commit d318e09

Browse files
committed
feature #11339 [FrameworkBundle] container:debug : list services if no service match exacly the name argument (agallou)
This PR was merged into the 2.6-dev branch. Discussion ---------- [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. Commits ------- 16201b6 [FrameworkBundle] container:debug : list services if no service match exacly the name argument
2 parents 0314d75 + 16201b6 commit d318e09

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

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

+33
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.
@@ -109,6 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
109110
$options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
110111
} elseif ($name = $input->getArgument('name')) {
111112
$object = $this->getContainerBuilder();
113+
$name = $this->findProperServiceName($input, $output, $object, $name);
112114
$options = array('id' => $name);
113115
} else {
114116
$object = $this->getContainerBuilder();
@@ -119,6 +121,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
119121
$options['format'] = $input->getOption('format');
120122
$options['raw_text'] = $input->getOption('raw');
121123
$helper->describe($output, $object, $options);
124+
125+
if (!$input->getArgument('name') && $input->isInteractive()) {
126+
$output->writeln('To search for a service, re-run this command with a search term. <comment>container:debug log</comment>');
127+
}
122128
}
123129

124130
/**
@@ -171,4 +177,31 @@ protected function getContainerBuilder()
171177

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

0 commit comments

Comments
 (0)