Skip to content

Commit

Permalink
EZP-26549: Output pretty JSON in dump-info command (#23)
Browse files Browse the repository at this point in the history
EZP-26549: Output pretty JSON in dump-info command
  • Loading branch information
João Inácio authored Nov 23, 2016
1 parent c8283cc commit 0eb6620
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 40 deletions.
86 changes: 47 additions & 39 deletions Command/SystemInfoDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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,
Expand All @@ -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));
}
}
40 changes: 40 additions & 0 deletions DependencyInjection/Compiler/OutputFormatPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* File containing the OutputFormatPass class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class OutputFormatPass implements CompilerPassInterface
{
/**
* Registers the OutputFormat tagged services into the output format registry.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if (!$container->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]);
}
}
2 changes: 2 additions & 0 deletions EzSystemsEzSupportToolsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,7 @@ public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new SystemInfoCollectorPass());
$container->addCompilerPass(new OutputFormatPass());
$container->addCompilerPass(new ViewBuilderPass());
}
}
14 changes: 13 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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" }
20 changes: 20 additions & 0 deletions SystemInfo/OutputFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* File containing the OutputFormat interface.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo;

interface OutputFormat
{
/**
* Format an array of collected information data, and return it as string
*
* @param array $collectedInfo
* @return string
*/
public function format(array $collectedInfo);
}
22 changes: 22 additions & 0 deletions SystemInfo/OutputFormat/JsonOutputFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* File containing the JsonOutputFormat class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormat;

use EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormat as SystemInfoOutputFormat;

/**
* Implements the JSON output format.
*/
class JsonOutputFormat implements SystemInfoOutputFormat
{
public function format(array $collectedInfo)
{
return json_encode( $collectedInfo, JSON_PRETTY_PRINT);
}
}
56 changes: 56 additions & 0 deletions SystemInfo/OutputFormatRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* File containing the OutputFormatRegistry class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo;

use eZ\Publish\Core\Base\Exceptions\NotFoundException;

/**
* A registry of OutputFormats.
*/
class OutputFormatRegistry
{
/** @var \EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormat[] */
private $registry = [];

/**
* @param \EzSystems\EzSupportToolsBundle\SystemInfo\OutputFormat[] $items Hash of OutputFormats, with identifier string as key.
*/
public function __construct(array $items = [])
{
$this->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);
}
}

0 comments on commit 0eb6620

Please sign in to comment.