From 0eb66208bda8904b477a67fc98ddc1a6e2242d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20In=C3=A1cio?= Date: Wed, 23 Nov 2016 13:30:00 +0000 Subject: [PATCH] EZP-26549: Output pretty JSON in dump-info command (#23) EZP-26549: Output pretty JSON in dump-info command --- Command/SystemInfoDumpCommand.php | 86 ++++++++++--------- .../Compiler/OutputFormatPass.php | 40 +++++++++ EzSystemsEzSupportToolsBundle.php | 2 + Resources/config/services.yml | 14 ++- SystemInfo/OutputFormat.php | 20 +++++ SystemInfo/OutputFormat/JsonOutputFormat.php | 22 +++++ SystemInfo/OutputFormatRegistry.php | 56 ++++++++++++ 7 files changed, 200 insertions(+), 40 deletions(-) create mode 100644 DependencyInjection/Compiler/OutputFormatPass.php create mode 100644 SystemInfo/OutputFormat.php create mode 100644 SystemInfo/OutputFormat/JsonOutputFormat.php create mode 100644 SystemInfo/OutputFormatRegistry.php diff --git a/Command/SystemInfoDumpCommand.php b/Command/SystemInfoDumpCommand.php index e0e1a484..c2b4fcdb 100644 --- a/Command/SystemInfoDumpCommand.php +++ b/Command/SystemInfoDumpCommand.php @@ -10,6 +10,7 @@ use EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector; use EzSystems\EzSupportToolsBundle\SystemInfo\SystemInfoCollectorRegistry; +use EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormatRegistry; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -23,11 +24,19 @@ class SystemInfoDumpCommand extends ContainerAwareCommand * * @var \EzSystems\EzSupportToolsBundle\SystemInfo\SystemInfoCollectorRegistry */ - private $registry; + private $systemInfoCollectorRegistry; - public function __construct(SystemInfoCollectorRegistry $registry) + /** + * Output format registry. + * + * @var \EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormatRegistry + */ + private $outputFormatRegistry; + + public function __construct(SystemInfoCollectorRegistry $systemInfoCollectorRegistry, OutputFormatRegistry $outputFormatRegistry) { - $this->registry = $registry; + $this->systemInfoCollectorRegistry = $systemInfoCollectorRegistry; + $this->outputFormatRegistry = $outputFormatRegistry; parent::__construct(); } @@ -52,6 +61,13 @@ protected function configure() InputOption::VALUE_NONE, 'List all available information collectors, and exit.' ) + ->addOption( + 'format', + 'f', + InputOption::VALUE_OPTIONAL, + 'Output format (currently only JSON)', + 'json' + ) ->addArgument( 'info-collectors', InputArgument::IS_ARRAY, @@ -66,43 +82,35 @@ protected function configure() * @param $input InputInterface * @param $output OutputInterface */ - protected function execute(InputInterface $input, OutputInterface $output) - { - if ($input->getOption('list-info-collectors')) { - $output->writeln('Available info collectors:', true); - foreach ($this->registry->getIdentifiers() as $identifier) { - $output->writeln(" $identifier", true); - } - } else if ($identifiers = $input->getArgument('info-collectors')) { - foreach ($identifiers as $identifier) { - $this->outputInfo( - $this->registry->getItem($identifier), - $output - ); - } - } else { - foreach ($this->registry->getIdentifiers() as $identifier) { - $this->outputInfo($this->registry->getItem($identifier), $output); - } - } - } + protected function execute(InputInterface $input, OutputInterface $output) + { + if ($input->getOption('list-info-collectors')) { + $output->writeln('Available info collectors:', true); + foreach ($this->systemInfoCollectorRegistry->getIdentifiers() as $identifier) { + $output->writeln(" $identifier", true); + } + return; + } - /** - * Output info collected by the given collector. - * - * @param $infoCollector SystemInfoCollector - * @param $output OutputInterface - */ - private function outputInfo(SystemInfoCollector $infoCollector, OutputInterface $output) - { - $infoValue = $infoCollector->collect(); + $outputFormatter = $this->outputFormatRegistry->getItem( + $input->getOption('format') + ); - $outputArray = []; - // attributes() is deprecated, and getProperties() is protected. TODO add a toArray() or similar. - foreach ($infoValue->attributes() as $property) { - $outputArray[$property] = $infoValue->$property; - } + if ($input->getArgument('info-collectors')) { + $identifiers = $input->getArgument('info-collectors'); + } else { + $identifiers = $this->systemInfoCollectorRegistry->getIdentifiers(); + } + + // Collect info for the given identifiers. + $collectedInfoArray = []; + foreach ($identifiers as $identifier) { + $collectedInfoArray[$identifier] = $this->systemInfoCollectorRegistry->getItem($identifier)->collect(); + } + + $output->writeln( + $outputFormatter->format($collectedInfoArray) + ); + } - $output->writeln(var_export($outputArray, true)); - } } diff --git a/DependencyInjection/Compiler/OutputFormatPass.php b/DependencyInjection/Compiler/OutputFormatPass.php new file mode 100644 index 00000000..f1aa081b --- /dev/null +++ b/DependencyInjection/Compiler/OutputFormatPass.php @@ -0,0 +1,40 @@ +has('support_tools.system_info.output_registry')) { + return; + } + + $outputFormattersTagged = $container->findTaggedServiceIds('support_tools.system_info.output_format'); + + $outputFormatters = []; + foreach ($outputFormattersTagged as $id => $tags) { + foreach ($tags as $attributes) { + $outputFormatters[$attributes['format']] = new Reference($id); + } + } + + $outputFormatRegistryDef = $container->findDefinition('support_tools.system_info.output_registry'); + $outputFormatRegistryDef->setArguments([$outputFormatters]); + } +} diff --git a/EzSystemsEzSupportToolsBundle.php b/EzSystemsEzSupportToolsBundle.php index b374a161..fc48f408 100644 --- a/EzSystemsEzSupportToolsBundle.php +++ b/EzSystemsEzSupportToolsBundle.php @@ -9,6 +9,7 @@ namespace EzSystems\EzSupportToolsBundle; use EzSystems\EzSupportToolsBundle\DependencyInjection\Compiler\SystemInfoCollectorPass; +use EzSystems\EzSupportToolsBundle\DependencyInjection\Compiler\OutputFormatPass; use EzSystems\EzSupportToolsBundle\DependencyInjection\Compiler\ViewBuilderPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -19,6 +20,7 @@ public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass(new SystemInfoCollectorPass()); + $container->addCompilerPass(new OutputFormatPass()); $container->addCompilerPass(new ViewBuilderPass()); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 2cc5ea80..2d3c99b4 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -4,29 +4,34 @@ imports: parameters: support_tools.command.dump_info.class: EzSystems\EzSupportToolsBundle\Command\SystemInfoDumpCommand support_tools.system_info.collector_registry.class: EzSystems\EzSupportToolsBundle\SystemInfo\Registry\IdentifierBased + support_tools.system_info.output_registry.class: EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormatRegistry support_tools.system_info.ezc.wrapper.class: EzSystems\EzSupportToolsBundle\SystemInfo\EzcSystemInfoWrapper support_tools.system_info.collector.composer.lock_file.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector support_tools.system_info.collector.database.doctrine.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\DoctrineDatabaseSystemInfoCollector support_tools.system_info.collector.hardware.ezc.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\EzcHardwareSystemInfoCollector support_tools.system_info.collector.php.ezc.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\EzcPhpSystemInfoCollector support_tools.system_info.collector.symfony.kernel.config.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\ConfigurationSymfonyKernelSystemInfoCollector + support_tools.system_info.output_format.json.class: EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormat\JsonOutputFormat services: support_tools.command.dump_info: class: "%support_tools.command.dump_info.class%" arguments: - "@support_tools.system_info.collector_registry" + - "@support_tools.system_info.output_registry" tags: - { name: console.command } support_tools.system_info.collector_registry: class: "%support_tools.system_info.collector_registry.class%" + support_tools.system_info.output_registry: + class: "%support_tools.system_info.output_registry.class%" + support_tools.system_info.ezc.wrapper: class: "%support_tools.system_info.ezc.wrapper.class%" lazy: true - # SystemInfoCollectors support_tools.system_info.collector.composer.lock_file: @@ -64,3 +69,10 @@ services: - "%kernel.bundles%" tags: - { name: "support_tools.system_info.collector", identifier: "symfony_kernel" } + + # SystemInfoOutputFormats + + support_tools.system_info.output_format.json: + class: "%support_tools.system_info.output_format.json.class%" + tags: + - { name: "support_tools.system_info.output_format", format: "json" } diff --git a/SystemInfo/OutputFormat.php b/SystemInfo/OutputFormat.php new file mode 100644 index 00000000..f5e2089f --- /dev/null +++ b/SystemInfo/OutputFormat.php @@ -0,0 +1,20 @@ +registry = $items; + } + + /** + * Returns the OutputFormat matching the argument. + * + * @param string $identifier An identifier string. + * + * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException If no OutputFormat exists with this identifier + * + * @return \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\OutputFormat The OutputFormat given by the identifier. + */ + public function getItem($identifier) + { + if (isset($this->registry[$identifier])) { + return $this->registry[$identifier]; + } + + throw new NotFoundException("A SystemInfo output format could not be found.", $identifier); + } + + /** + * Returns the identifiers of all registered OutputFormats. + * + * @return string[] Array of identifier strings. + */ + public function getIdentifiers() + { + return array_keys($this->registry); + } +}