Skip to content

Commit

Permalink
[site:debug] Relocate command. (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Dec 12, 2016
1 parent 7a61278 commit ec23bda
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
5 changes: 5 additions & 0 deletions services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ services:
arguments: ['@console.chain_discovery']
tags:
- { name: drupal.command }
console.site_debug:
class: Drupal\Console\Command\Site\DebugCommand
arguments: ['@console.configuration_manager']
tags:
- { name: drupal.command }
# DrupalConsoleCore Generators
console.init_generator:
class: Drupal\Console\Generator\InitGenerator
Expand Down
150 changes: 150 additions & 0 deletions src/Command/Site/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Site\DebugCommand.
*/

namespace Drupal\Console\Command\Site;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Utils\ConfigurationManager;
use Drupal\Console\Style\DrupalStyle;

/**
* Class SiteDebugCommand
* @package Drupal\Console\Command\Site
*/
class DebugCommand extends Command
{
use CommandTrait;

/**
* @var ConfigurationManager
*/
protected $configurationManager;

/**
* DebugCommand constructor.
* @param ConfigurationManager $configurationManager
*/
public function __construct(
ConfigurationManager $configurationManager
) {
$this->configurationManager = $configurationManager;
parent::__construct();
}

/**
* @{@inheritdoc}
*/
public function configure()
{
$this
->setName('site:debug')
->setDescription($this->trans('commands.site.debug.description'))
->addArgument(
'target',
InputArgument::OPTIONAL,
$this->trans('commands.site.debug.options.target'),
null
)
->setHelp($this->trans('commands.site.debug.help'));
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$sitesDirectory = $this->configurationManager->getSitesDirectory();

if (!is_dir($sitesDirectory)) {
$io->error(
sprintf(
$this->trans('commands.site.debug.messages.directory-not-found'),
$sitesDirectory
)
);

return 1;
}

// --target argument
$target = $input->getArgument('target');
if ($target) {
$io->write(
$this->siteDetail($target)
);

return 0;
}

$tableHeader =[
$this->trans('commands.site.debug.messages.site'),
$this->trans('commands.site.debug.messages.host'),
$this->trans('commands.site.debug.messages.root')
];

$tableRows = $this->siteList($sitesDirectory);

$io->table($tableHeader, $tableRows);
return 0;
}

/**
* @param string $target
*
* @return string
*/
private function siteDetail($target)
{
if ($targetConfig = $this->configurationManager->readTarget($target)) {
$dumper = new Dumper();

return $dumper->dump($targetConfig, 2);
}

return null;
}

/**
* @param string $sitesDirectory
* @return array
*/
private function siteList($sitesDirectory)
{
$finder = new Finder();
$finder->in($sitesDirectory);
$finder->name("*.yml");

$tableRows = [];
foreach ($finder as $site) {
$siteName = $site->getBasename('.yml');
$environments = $this->configurationManager
->readSite($site->getRealPath());

if (!$environments || !is_array($environments)) {
continue;
}

foreach ($environments as $environment => $config) {
$tableRows[] = [
$siteName . '.' . $environment,
array_key_exists('host', $config) ? $config['host'] : 'local',
array_key_exists('root', $config) ? $config['root'] : ''
];
}
}

return $tableRows;
}
}

0 comments on commit ec23bda

Please sign in to comment.