From 34b60f75582d877cff55c83227b172b2a8204d9c Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Sun, 15 Jul 2012 22:43:34 +0200 Subject: [PATCH] [FrameworkBundle] more verbose output about service tags in container:debug command and display all tag attributes as columns in normal container:debug output --- .../Command/ContainerDebugCommand.php | 116 +++++++++++++----- 1 file changed, 86 insertions(+), 30 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php index 0a4c6233960a..08922a9ac545 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php @@ -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; @@ -83,13 +82,21 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->containerBuilder = $this->getContainerBuilder(); + $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; } - - $tag = $input->getOption('tag'); + 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(); @@ -122,6 +129,7 @@ protected function outputServices(OutputInterface $output, $serviceIds, $showPri // 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); @@ -135,51 +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, 'Service Id', 'Scope', 'Class Name')); + $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[] = ''.$tagName.''; + } + $output->writeln(vsprintf($format1, $this->buildArgumentsArray('Service Id', 'Scope', 'Class Name', $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('alias for %s', (string) $alias))); + $output->writeln(vsprintf($format, $this->buildArgumentsArray($serviceId, 'n/a', sprintf('alias for %s', (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))); - } - - if (null !== $showTagAttributes) { - $tags = $definition->getTag($showTagAttributes); - foreach($tags as $tag) { - $output->write(' '.$showTagAttributes.' tag attributes: '); - if (count($tag)) { - $arguments = array(); - foreach($tag as $key => $value) { - $arguments[] = $key; - $arguments[] = $value; - } - $output->writeln(vsprintf(implode(", ", array_fill(0, count($tag), '%s: %s')), $arguments)); - } else { - $output->writeln('none'); - } - } + $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 */ @@ -193,10 +238,21 @@ protected function outputService(OutputInterface $output, $serviceId) if ($definition instanceof Definition) { $output->writeln(sprintf('Service Id %s', $serviceId)); - $output->writeln(sprintf('Class %s', $definition->getClass())); - - $tags = $definition->getTags() ? implode(', ', array_keys($definition->getTags())) : '-'; - $output->writeln(sprintf('Tags %s', $tags)); + $output->writeln(sprintf('Class %s', $definition->getClass() ?: "-")); + + $tags = $definition->getTags(); + if (count($tags)) { + $output->writeln('Tags'); + foreach ($tags as $tagName => $tagData) { + foreach ($tagData as $singleTagData) { + $output->writeln(sprintf(' - %-30s (%s)', $tagName, implode(', ', array_map(function($key, $value) { + return sprintf('%s: %s', $key, $value); + }, array_keys($singleTagData), array_values($singleTagData))))); + } + } + } else { + $output->writeln('Tags -'); + } $output->writeln(sprintf('Scope %s', $definition->getScope()));