Skip to content

[FrameworkBundle] container:debug : list services if no service match exacly the name argument #11339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') && $input->isInteractive()) {
$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,31 @@ protected function getContainerBuilder()

return $container;
}

private function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)
{
if ($builder->has($name) || !$input->isInteractive()) {
return $name;
}

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

return $this->getHelper('question')->ask($input, $output, $question);
}

private function findServiceIdsContaining(ContainerBuilder $builder, $name)
{
$serviceIds = $builder->getServiceIds();
$foundServiceIds = array();
$name = strtolower($name);
foreach ($serviceIds as $serviceId) {
if (false === strpos($serviceId, $name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that service ids are always lowercase internally in Symfony, you should either use stripos, or even better lowercase $name (which can be done outside the loop)

continue;
}
$foundServiceIds[] = $serviceId;
}

return $foundServiceIds;
}
}