Skip to content

Commit

Permalink
[debug:theme:keys] Displays all theme keys for DX. (#3361)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuaelFr authored and jmolivas committed Sep 5, 2017
1 parent 8561b13 commit 10ea42a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ services:
arguments: ['@config.factory', '@theme_handler']
tags:
- { name: drupal.command }
console.theme_keys_debug:
class: Drupal\Console\Command\Debug\ThemeKeysCommand
arguments: ['@theme.registry']
tags:
- { name: drupal.command }
console.router_debug:
class: Drupal\Console\Command\Debug\RouterCommand
arguments: ['@router.route_provider']
Expand Down
113 changes: 113 additions & 0 deletions src/Command/Debug/ThemeKeysCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Theme\DebugKeysCommand.
*/

namespace Drupal\Console\Command\Debug;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Yaml\Yaml;
use Drupal\Console\Core\Command\Shared\CommandTrait;
use Drupal\Core\Theme\Registry;
use Drupal\Console\Core\Style\DrupalStyle;

class ThemeKeysCommand extends Command
{
use CommandTrait;

/**
* @var Registry
*/
protected $themeRegistry;

/**
* DebugCommand constructor.
*
* @param \Drupal\Core\Theme\Registry $themeRegistry
* The theme registry service.
*/
public function __construct(Registry $themeRegistry) {
$this->themeRegistry = $themeRegistry;
parent::__construct();
}

protected function configure()
{
$this
->setName('debug:theme:keys')
->setDescription($this->trans('commands.debug.theme.keys.description'))
->setHelp($this->trans('commands.debug.theme.keys.help'))
->addArgument(
'key',
InputArgument::OPTIONAL,
$this->trans('commands.debug.theme.keys.arguments.key')
)
->setAliases(['dtk']);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$key = $input->getArgument('key');

if (!$key) {
$this->themeKeysList($io);
}
else {
$this->themeKeysDetail($io, $key);
}
}

protected function themeKeysList(DrupalStyle $io)
{
$tableHeader = [
$this->trans('commands.debug.theme.keys.table-headers.key'),
$this->trans('commands.debug.theme.keys.table-headers.provider-type'),
$this->trans('commands.debug.theme.keys.table-headers.provider'),
];

$tableRows = [];
$keys = $this->themeRegistry->get();
foreach ($keys as $key => $definition) {
$tableRows[] = [
$key,
$this->trans('commands.debug.theme.keys.provider-types.' . strtr($definition['type'], '_', '-')),
basename($definition['theme path']),
];
}
array_multisort($tableRows, array_column($tableRows, 0));

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

protected function themeKeysDetail(DrupalStyle $io, $key)
{
$tableHeader = [
$this->trans('commands.debug.theme.keys.table-headers.key'),
$this->trans('commands.debug.theme.keys.table-headers.value')
];

$keys = $this->themeRegistry->get();
$definition = $keys[$key];

$tableRows = [];
foreach ($definition as $key => $value) {
if (is_object($value) && method_exists($value, '__toString')) {
$value = (string) $value;
} elseif (is_array($value) || is_object($value)) {
$value = Yaml::dump($value);
} elseif (is_bool($value)) {
$value = ($value) ? 'TRUE' : 'FALSE';
}
$tableRows[$key] = [$key, $value];
}
ksort($tableRows);
$io->table($tableHeader, array_values($tableRows));
}
}

0 comments on commit 10ea42a

Please sign in to comment.