8000 [FrameworkBundle] [Console] added format parameter to ContainerDebugCommand by pulse00 · Pull Request #5740 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] [Console] added format parameter to ContainerDebugCommand #5740

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

Closed
wants to merge 3 commits into from
Closed
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
104 changes: 100 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected function configure()
->setDefinition(array(
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
8000 new InputOption('show-private', null, InputOption::VALUE_NONE, 'Use to show public *and* private services'),
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'The output format (plain|json)', 'plain'),
))
->setDescription('Displays current services for an application')
->setHelp(<<<EOF
Expand Down Expand Up @@ -78,13 +79,61 @@ protected function execute(InputInterface $input, OutputInterface $output)
asort($serviceIds);

if ($name) {
$this->outputService($output, $name);
$this->outputService($output, $name, $input->getOption('format'));
} else {
$this->outputServices($output, $serviceIds, $input->getOption('show-private'));
$this->outputServices($output, $serviceIds, $input->getOption('show-private'), $input->getOption('format'));
}
}

protected function outputServices(OutputInterface $output, $serviceIds, $showPrivate = false)
protected function outputServices(OutputInterface $output, $serviceIds, $showPrivate = false, $format = 'plain')
{
switch ($format) {
case 'plain':
$this->outputServicesPlain($output, $serviceIds, $showPrivate);
break;
case 'json':
$this->outputServicesJson($output, $serviceIds, $showPrivate);
break;
default:
throw new \InvalidArgumentException("The format " . $format ." is not available.");
}
}

protected function outputServicesJson(OutputInterface $output, $serviceIds, $showPrivate = false)
{
$services = array();

// loop through to filter private services
foreach ($serviceIds as $key => $serviceId) {
$definition = $this->resolveServiceDefinition($serviceId);

if ($definition instanceof Definition) {
// filter out private services unless shown explicitly
if (!$showPrivate && !$definition->isPublic()) {
unset($serviceIds[$key]);
continue;
}
}
}

foreach ($serviceIds as $serviceId) {
8000 $definition = $this->resolveServiceDefinition($serviceId);
if ($definition instanceof Definition) {
$services[] = array('id' => $serviceId, 'scope' => $definition->getScope(), 'class' => $definition->getClass());
} elseif ($definition instanceof Alias) {
$alias = $definition;
$services[] = array('id' => $serviceId, 'scope'=> 'n/a', 'aliasFor' => (string) $alias);
} else {
// we have no information (happens with "service_container")
$service = $definition;
$services[] = array('id' => $serviceId, 'scope' => '', 'class' => get_class($service));
}
}

$output->writeln(json_encode($services));
}

protected function outputServicesPlain(OutputInterface $output, $serviceIds, $showPrivate = false)
{
// set the label to specify public or public+private
if ($showPrivate) {
Expand Down Expand Up @@ -142,7 +191,54 @@ protected function outputServices(OutputInterface $output, $serviceIds, $showPri
/**
* Renders detailed service information about one service
*/
protected function outputService(OutputInterface $output, $serviceId)
protected function outputService(OutputInterface $output, $serviceId, $format = 'plain')
{
switch ($format) {
case 'plain':
$this->outputServicePlain($output, $serviceId);
break;
case 'json':
$this->outputServiceJson($output, $serviceId);
break;
default:
throw new \InvalidArgumentException("The format " . $format ." is not available.");
}
}

protected function outputServiceJson(OutputInterface $output, $serviceId)
{
$definition = $this->resolveServiceDefinition($serviceId);
$service = array();

if ($definition instanceof Definition) {

$public = $definition->isPublic() ? 'yes' : 'no';
$synthetic = $definition->isSynthetic() ? 'yes' : 'no';
$tags = $definition->getTags() ? array_keys($definition->getTags()) : array();
$file = $definition->getFile() ? $definition->getFile() : null;

$service['id'] = $serviceId;
$service['class'] = $definition->getClass();
$service['tags'] = $tags;
$service['scope'] = $definition->getScope();
$service['public'] = $public;
$service['synthetic'] = $synthetic;
$service['file'] = $file;

} elseif ($definition instanceof Alias) {
$alias = $definition;
$service['aliasFor'] = (string) $alias;
} else {
// edge case (but true for "service_container", all we have is the service itself
$s = $definition;
$service['id'] = $serviceId;
$service['class'] = get_class($s);
}

$output->writeln(json_encode($service));
}

protected function outputServicePlain(OutputInterface $output, $serviceId)
{
$definition = $this->resolveServiceDefinition($serviceId);

Expand Down
0