Skip to content
Merged
Show file tree
Hide file tree
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
166 changes: 152 additions & 14 deletions src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -44,6 +43,8 @@ protected function configure()
->setDefinition(array(
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Use to show public *and* private services'),
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Show all services with a specific tag'),
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application')
))
->setDescription('Displays current services for an application')
->setHelp(<<<EOF
Expand All @@ -59,6 +60,14 @@ protected function configure()
using the --show-private flag:

<info>php %command.full_name% --show-private</info>

Use the --tags option to display tagged <comment>public</comment> services grouped by tag:

<info>php %command.full_name% --tags</info>

Find all services with a specific tag by specifying the tag name with the --tag option:

<info>php %command.full_name% --tag=form.type</info>
EOF
)
;
Expand All @@ -72,32 +81,55 @@ protected function execute(InputInterface $input, OutputInterface $output)
$name = $input->getArgument('name');

$this->containerBuilder = $this->getContainerBuilder();
$serviceIds = $this->containerBuilder->getServiceIds();

$tag = $input->getOption('tag');
if ($input->getOption('tags')) {
if ($tag || $input->getArgument('name')) {
throw new \InvalidArgumentException('The --tags option cannot be combined with the --tag option or the service name argument.');
}

$this->outputTags($output, $input->getOption('show-private'));
return;
}

if (null !== $tag) {
if ($input->getArgument('name')) {
throw new \InvalidArgumentException('The --tag option cannot be combined with the service name argument.');
}

$serviceIds = array_keys($this->containerBuilder->findTaggedServiceIds($tag));
} else {
$serviceIds = $this->containerBuilder->getServiceIds();
}

// sort so that it reads like an index of services
asort($serviceIds);

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

protected function outputServices(OutputInterface $output, $serviceIds, $showPrivate = false)
protected function outputServices(OutputInterface $output, $serviceIds, $showPrivate = false, $showTagAttributes = null)
{
// set the label to specify public or public+private
if ($showPrivate) {
$label = '<comment>Public</comment> and <comment>private</comment> services';
} else {
$label = '<comment>Public</comment> services';
}
if ($showTagAttributes) {
$label .= ' with tag <info>'.$showTagAttributes.'</info>';
}

$output->writeln($this->getHelper('formatter')->formatSection('container', $label));

// loop through to get space needed and filter private services
$maxName = 4;
$maxScope = 6;
$maxTags = array();
foreach ($serviceIds as $key => $serviceId) {
$definition = $this->resolveServiceDefinition($serviceId);

Expand All @@ -111,34 +143,88 @@ protected function outputServices(OutputInterface $output, $serviceIds, $showPri
if (strlen($definition->getScope()) > $maxScope) {
$maxScope = strlen($definition->getScope());
}

if (null !== $showTagAttributes) {
$tags = $definition->getTag($showTagAttributes);
foreach($tags as $tag) {
foreach($tag as $key => $value) {
if (!isset($maxTags[$key])) {
$maxTags[$key] = strlen($key);
}
if (strlen($value) > $maxTags[$key]) {
$maxTags[$key] = strlen($value);
}
}
}
}
}

if (strlen($serviceId) > $maxName) {
$maxName = strlen($serviceId);
}
}
$format = '%-'.$maxName.'s %-'.$maxScope.'s %s';
$format = '%-'.$maxName.'s ';
$format .= implode("", array_map(function($length) { return "%-{$length}s "; }, $maxTags));
$format .= '%-'.$maxScope.'s %s';

// the title field needs extra space to make up for comment tags
$format1 = '%-'.($maxName + 19).'s %-'.($maxScope + 19).'s %s';
$output->writeln(sprintf($format1, '<comment>Service Id</comment>', '<comment>Scope</comment>', '<comment>Class Name</comment>'));
$format1 = '%-'.($maxName + 19).'s ';
$format1 .= implode("", array_map(function($length) { return '%-'.($length + 19).'s '; }, $maxTags));
$format1 .= '%-'.($maxScope + 19).'s %s';

$tags = array();
foreach($maxTags as $tagName => $length) {
$tags[] = '<comment>'.$tagName.'</comment>';
}
$output->writeln(vsprintf($format1, $this->buildArgumentsArray('<comment>Service Id</comment>', '<comment>Scope</comment>', '<comment>Class Name</comment>', $tags)));

foreach ($serviceIds as $serviceId) {
$definition = $this->resolveServiceDefinition($serviceId);

if ($definition instanceof Definition) {
$output->writeln(sprintf($format, $serviceId, $definition->getScope(), $definition->getClass()));
$lines = array();
if (null !== $showTagAttributes) {
foreach($definition->getTag($showTagAttributes) as $key => $tag) {
$tagValues = array();
foreach(array_keys($maxTags) as $tagName) {
$tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : "";
}
if (0 === $key) {
$lines[] = $this->buildArgumentsArray($serviceId, $definition->getScope(), $definition->getClass(), $tagValues);
} else {
$lines[] = $this->buildArgumentsArray(' "', '', '', $tagValues);
}
}
} else {
$lines[] = $this->buildArgumentsArray($serviceId, $definition->getScope(), $definition->getClass());
}

foreach($lines as $arguments) {
$output->writeln(vsprintf($format, $arguments));
}
} elseif ($definition instanceof Alias) {
$alias = $definition;
$output->writeln(sprintf($format, $serviceId, 'n/a', sprintf('<comment>alias for</comment> <info>%s</info>', (string) $alias)));
$output->writeln(vsprintf($format, $this->buildArgumentsArray($serviceId, 'n/a', sprintf('<comment>alias for</comment> <info>%s</info>', (string) $alias), count($maxTags) ? array_fill(0, count($maxTags), "") : array())));
} else {
// we have no information (happens with "service_container")
$service = $definition;
$output->writeln(sprintf($format, $serviceId, '', get_class($service)));
$output->writeln(vsprintf($format, $this->buildArgumentsArray($serviceId, '', get_class($service), count($maxTags) ? array_fill(0, count($maxTags), "") : array())));
}
}
}

protected function buildArgumentsArray($serviceId, $scope, $className, array $tagAttributes = array())
{
$arguments = array($serviceId);
foreach($tagAttributes as $tagAttribute) {
$arguments[] = $tagAttribute;
}
$arguments[] = $scope;
$arguments[] = $className;

return $arguments;
}

/**
* Renders detailed service information about one service
*/
Expand All @@ -152,10 +238,21 @@ protected function outputService(OutputInterface $output, $serviceId)

if ($definition instanceof Definition) {
$output->writeln(sprintf('<comment>Service Id</comment> %s', $serviceId));
$output->writeln(sprintf('<comment>Class</comment> %s', $definition->getClass()));

$tags = $definition->getTags() ? implode(', ', array_keys($definition->getTags())) : '-';
$output->writeln(sprintf('<comment>Tags</comment> %s', $tags));
$output->writeln(sprintf('<comment>Class</comment> %s', $definition->getClass() ?: "-"));

$tags = $definition->getTags();
if (count($tags)) {
$output->writeln('<comment>Tags</comment>');
foreach ($tags as $tagName => $tagData) {
foreach ($tagData as $singleTagData) {
$output->writeln(sprintf(' - %-30s (%s)', $tagName, implode(', ', array_map(function($key, $value) {
return sprintf('<info>%s</info>: %s', $key, $value);
}, array_keys($singleTagData), array_values($singleTagData)))));
}
}
} else {
$output->writeln('<comment>Tags</comment> -');
}

$output->writeln(sprintf('<comment>Scope</comment> %s', $definition->getScope()));

Expand Down Expand Up @@ -223,4 +320,45 @@ protected function resolveServiceDefinition($serviceId)
// the service has been injected in some special way, just return the service
return $this->containerBuilder->get($serviceId);
}

/**
* Renders list of tagged services grouped by tag
*
* @param OutputInterface $output
* @param bool $showPrivate
*/
protected function outputTags(OutputInterface $output, $showPrivate = false)
{
$tags = $this->containerBuilder->findTags();
asort($tags);

$label = 'Tagged services';
$output->writeln($this->getHelper('formatter')->formatSection('container', $label));

foreach ($tags as $tag) {
$serviceIds = $this->containerBuilder->findTaggedServiceIds($tag);

foreach ($serviceIds as $serviceId => $attributes) {
$definition = $this->resolveServiceDefinition($serviceId);
if ($definition instanceof Definition) {
if (!$showPrivate && !$definition->isPublic()) {
unset($serviceIds[$serviceId]);
continue;
}
}
}

if (count($serviceIds) === 0) {
continue;
}

$output->writeln($this->getHelper('formatter')->formatSection('tag', $tag));

foreach ($serviceIds as $serviceId => $attributes) {
$output->writeln($serviceId);
}

$output->writeln('');
}
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,21 @@ public function findTaggedServiceIds($name)
return $tags;
}

/**
* Returns all tags the defined services use.
*
* @return array An array of tags
*/
public function findTags()
{
$tags = array();
foreach ($this->getDefinitions() as $id => $definition) {
$tags = array_merge(array_keys($definition->getTags()), $tags);
}

return array_unique($tags);
}

/**
* Returns the Service Conditionals.
*
Expand Down