Skip to content

Commit

Permalink
[FrameworkBundle] container:debug : list services if no service match…
Browse files Browse the repository at this point in the history
… exacly the name argument

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony#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.
  • Loading branch information
agallou committed Jul 6, 2014
1 parent 8b54211 commit 5ad8c7a
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Question\ChoiceQuestion;

/**
* A console command for retrieving information about services.
Expand Down Expand Up @@ -106,6 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
} elseif ($name = $input->getArgument('name')) {
$object = $this->getContainerBuilder();
$name = $this->findProperServiceName($input, $output, $object, $name);
$options = array('id' => $name);
} else {
$object = $this->getContainerBuilder();
Expand All @@ -116,6 +118,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$options['format'] = $input->getOption('format');
$options['raw_text'] = $input->getOption('raw');
$helper->describe($output, $object, $options);

if (!$input->getArgument('name') && posix_isatty(STDOUT)) {
$output->writeln('To search for a service, re-run this command with a search term. <comment>container:debug log</comment>');
}
}

/**
Expand Down Expand Up @@ -168,4 +174,48 @@ protected function getContainerBuilder()

return $container;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param ContainerBuilder $builder
* @param string $name
*
* @return string
*/
protected function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)
{
if ($builder->has($name)) {
return $name;
}

$helper = $this->getApplication()->getHelperSet()->get('question');
$question = new ChoiceQuestion(
'Choose a number for more information on the service',
$this->findServiceidsLike($builder, $name)
);
$question->setErrorMessage('Service %s is invalid.');

return $helper->ask($input, $output, $question);
}

/**
* @param ContainerBuilder $builder
* @param string $name
*
* @return string
*/
protected function findServiceIdsLike(ContainerBuilder $builder, $name)
{
$serviceIds = $builder->getServiceIds();
$foundServiceIds = array();
foreach ($serviceIds as $serviceId) {
if (false === strpos($serviceId, $name)) {
continue;
}
$foundServiceIds[] = $serviceId;
}

return $foundServiceIds;
}
}

0 comments on commit 5ad8c7a

Please sign in to comment.