Skip to content

Commit

Permalink
Merge pull request #12 from ezsystems/ezp-25598_infocollector_registry
Browse files Browse the repository at this point in the history
Fix EZP-25598: eZSupportTools infocollector registry
  • Loading branch information
glye committed Apr 7, 2016
2 parents 1bf51d3 + c131db8 commit 8101746
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 57 deletions.
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ branches:
- master

before_script:
- composer selfupdate
- composer install --prefer-dist
# re-enable when / if coverage is disabled for faster test runs
#- phpenv config-rm xdebug.ini
- travis_retry composer selfupdate
# Avoid memory issues on composer install
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- travis_retry composer install --prefer-dist --no-interaction

script: phpunit --coverage-text
script: php bin/phpunit --coverage-text

notifications:
email: false
65 changes: 60 additions & 5 deletions Command/SystemInfoDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@
*/
namespace EzSystems\EzSupportToolsBundle\Command;

use EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector;
use EzSystems\EzSupportToolsBundle\SystemInfo\SystemInfoCollectorRegistry;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SystemInfoDumpCommand extends ContainerAwareCommand
{
/**
* System info collector registry.
*
* @var \EzSystems\EzSupportToolsBundle\SystemInfo\SystemInfoCollectorRegistry
*/
private $registry;

public function __construct(SystemInfoCollectorRegistry $registry)
{
$this->registry = $registry;

parent::__construct();
}

/**
* Define command and input options.
*/
Expand All @@ -23,10 +40,22 @@ protected function configure()
$this
->setName('ez-support-tools:dump-info')
->setDescription('Collects system information and dumps it.')
->setHelp(<<<'EOD'
By default it dumps information from all available information collectors.
You can specify one or more collectors as arguments, e.g. 'php database hardware'.
To get a list if available collectors, use '--list-info-collectors'
EOD
)
->addOption(
'list-info-collectors',
null,
InputOption::VALUE_NONE,
'List all available information collectors, and exit.'
)
->addArgument(
'info-collector',
InputArgument::REQUIRED,
'Which information collector should be used?'
'info-collectors',
InputArgument::IS_ARRAY,
'Which information collector(s) should be used? (separate multiple names with spaces)'
)
;
}
Expand All @@ -39,11 +68,37 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$infoCollector = $this->getContainer()->get($input->getArgument('info-collector'));
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);
}
}
}

/**
* Output info collected by the given collector.
*
* @param $infoCollector SystemInfoCollector
* @param $output OutputInterface
*/
private function outputInfo(SystemInfoCollector $infoCollector, OutputInterface $output)
{
$infoValue = $infoCollector->collect();

$outputArray = [];
// attributes() is deprecated, and getProperties() is protected. Smeg it, this is very temporary anyway.
// attributes() is deprecated, and getProperties() is protected. TODO add a toArray() or similar.
foreach ($infoValue->attributes() as $property) {
$outputArray[$property] = $infoValue->$property;
}
Expand Down
40 changes: 40 additions & 0 deletions DependencyInjection/Compiler/SystemInfoCollectorPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* File containing the SystemInfoCollectorPass 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 SystemInfoCollectorPass implements CompilerPassInterface
{
/**
* Registers the SystemInfoCollector into the system info collector registry.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if (!$container->has('support_tools.system_info.collector_registry')) {
return;
}

$infoCollectorsTagged = $container->findTaggedServiceIds('support_tools.system_info.collector');

$infoCollectors = [];
foreach ($infoCollectorsTagged as $id => $tags) {
foreach ($tags as $attributes) {
$infoCollectors[$attributes['identifier']] = new Reference($id);
}
}

$infoCollectorRegistryDef = $container->findDefinition('support_tools.system_info.collector_registry');
$infoCollectorRegistryDef->setArguments([$infoCollectors]);
}
}
2 changes: 2 additions & 0 deletions EzSystemsEzSupportToolsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace EzSystems\EzSupportToolsBundle;

use EzSystems\EzSupportToolsBundle\DependencyInjection\Compiler\SystemInfoCollectorPass;
use EzSystems\EzSupportToolsBundle\DependencyInjection\Compiler\ViewBuilderPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -17,6 +18,7 @@ class EzSystemsEzSupportToolsBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new SystemInfoCollectorPass());
$container->addCompilerPass(new ViewBuilderPass());
}
}
68 changes: 49 additions & 19 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,69 @@ imports:
- { resource: view.yml }

parameters:
support_tools.info_collectors.composer.lock_file.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector
support_tools.info_collectors.database.doctrine.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\DoctrineDatabaseSystemInfoCollector
support_tools.info_collectors.hardware.ezc.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\EzcHardwareSystemInfoCollector
support_tools.info_collectors.php.ezc.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\EzcPhpSystemInfoCollector
support_tools.info_collectors.symfony.kernel.config.class: EzSystems\EzSupportToolsBundle\SystemInfo\Collector\ConfigurationSymfonyKernelSystemInfoCollector
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.ezc.factory.class: EzSystems\EzSupportToolsBundle\SystemInfo\EzcSystemInfoFactory
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

services:
support_tools.info_collector.system_info.ezc:
support_tools.command.dump_info:
class: %support_tools.command.dump_info.class%
arguments:
- @support_tools.system_info.collector_registry
tags:
- { name: console.command }

support_tools.system_info.collector_registry:
class: %support_tools.system_info.collector_registry.class%

support_tools.system_info.ezc:
class: ezcSystemInfo
factory: [EzSystems\EzSupportToolsBundle\SystemInfo\EzcSystemInfoFactory, buildEzcSystemInfo]
factory: [@support_tools.system_info.ezc.factory, buildEzcSystemInfo]

support_tools.system_info.ezc.factory:
class: %support_tools.system_info.ezc.factory.class%
lazy: true


# SystemInfoCollectors

support_tools.info_collectors.composer.lock_file:
class: %support_tools.info_collectors.composer.lock_file.class%
support_tools.system_info.collector.composer.lock_file:
class: %support_tools.system_info.collector.composer.lock_file.class%
arguments:
- "%kernel.root_dir%/../composer.lock"
tags:
- { name: "support_tools.system_info.collector", identifier: "composer" }

support_tools.info_collectors.database.doctrine:
class: %support_tools.info_collectors.database.doctrine.class%
support_tools.system_info.collector.database.doctrine:
class: %support_tools.system_info.collector.database.doctrine.class%
arguments:
- @database_connection
tags:
- { name: "support_tools.system_info.collector", identifier: "database" }

support_tools.info_collectors.hardware.ezc:
class: %support_tools.info_collectors.hardware.ezc.class%
support_tools.system_info.collector.hardware.ezc:
class: %support_tools.system_info.collector.hardware.ezc.class%
arguments:
- @support_tools.info_collector.system_info.ezc
- @support_tools.system_info.ezc
tags:
- { name: "support_tools.system_info.collector", identifier: "hardware" }

support_tools.info_collectors.php.ezc:
class: %support_tools.info_collectors.php.ezc.class%
support_tools.system_info.collector.php.ezc:
class: %support_tools.system_info.collector.php.ezc.class%
arguments:
- @support_tools.info_collector.system_info.ezc
- @support_tools.system_info.ezc
tags:
- { name: "support_tools.system_info.collector", identifier: "php" }

support_tools.info_collectors.symfony.kernel.config:
class: %support_tools.info_collectors.symfony.kernel.config.class%
support_tools.system_info.collector.symfony.kernel.config:
class: %support_tools.system_info.collector.symfony.kernel.config.class%
arguments:
- @kernel
- %kernel.bundles%
tags:
- { name: "support_tools.system_info.collector", identifier: "symfony_kernel" }
5 changes: 1 addition & 4 deletions Resources/config/view.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ services:
class: EzSystems\EzSupportToolsBundle\View\SystemInfoViewBuilder
arguments:
- @ezpublish.view.configurator
# temporary until we add a service tag (EZP-25598)
-
hardware: @support_tools.info_collectors.hardware.ezc
php: @support_tools.info_collectors.php.ezc
- @support_tools.system_info.collector_registry

support_tools.view.controller:
class: EzSystems\EzSupportToolsBundle\Controller\SystemInfoController
Expand Down
57 changes: 57 additions & 0 deletions SystemInfo/Registry/IdentifierBased.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* File containing the IdentifierBased 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\Registry;

use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use EzSystems\EzSupportToolsBundle\SystemInfo\SystemInfoCollectorRegistry;

/**
* A registry of SystemInfoCollectors that uses an identifier string to identify the collector.
*/
class IdentifierBased implements SystemInfoCollectorRegistry
{
/** @var \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector[] */
private $registry = [];

/**
* @param \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector[] $items Hash of SystemInfoCollectors, with identifier string as key.
*/
public function __construct(array $items = [])
{
$this->registry = $items;
}

/**
* Returns the SystemInfoCollector matching the argument.
*
* @param string $identifier An identifier string.
*
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException If no SystemInfoCollector exists with this identifier
*
* @return \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector The SystemInfoCollector given by the identifier.
*/
public function getItem($identifier)
{
if (isset($this->registry[$identifier])) {
return $this->registry[$identifier];
}

throw new NotFoundException("A SystemInfo collector could not be found.", $identifier);
}

/**
* Returns the identifiers of all registered SystemInfoCollectors.
*
* @return string[] Array of identifier strings.
*/
public function getIdentifiers()
{
return array_keys($this->registry);
}
}
38 changes: 38 additions & 0 deletions SystemInfo/SystemInfoCollectorRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* File containing the SystemInfoCollectorRegistry 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;

/**
* A registry of SystemInfoCollectors.
*/
interface SystemInfoCollectorRegistry
{
/**
* @param \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector[] $items Hash of SystemInfoCollectors, with identifier string as key.
*/
public function __construct(array $items = []);

/**
* Returns the SystemInfoCollector matching the argument.
*
* @param string $identifier An identifier string.
*
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException If no SystemInfoCollector exists with this identifier
*
* @return \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\SystemInfoCollector The SystemInfoCollector given by the identifier.
*/
public function getItem($identifier);

/**
* Returns the identifiers of all registered SystemInfoCollectors.
*
* @return string[] Array of identifier strings.
*/
public function getIdentifiers();
}
Loading

0 comments on commit 8101746

Please sign in to comment.