View source
<?php
namespace Drupal\devel\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Utility\Token;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
use Drush\Utils\StringUtils;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Output\Output;
class DevelCommands extends DrushCommands {
protected $token;
protected $container;
protected $eventDispatcher;
protected $moduleHandler;
public function __construct(Token $token, $container, $eventDispatcher, $moduleHandler) {
parent::__construct();
$this->token = $token;
$this->container = $container;
$this->eventDispatcher = $eventDispatcher;
$this->moduleHandler = $moduleHandler;
}
public function getModuleHandler() {
return $this->moduleHandler;
}
public function getEventDispatcher() {
return $this->eventDispatcher;
}
public function getContainer() {
return $this->container;
}
public function getToken() {
return $this->token;
}
public function reinstall($modules) {
$modules = StringUtils::csvToArray($modules);
$modules_str = implode(',', $modules);
drush_invoke_process('@self', 'pm:uninstall', [
$modules_str,
], []);
drush_invoke_process('@self', 'pm:enable', [
$modules_str,
], []);
}
function hook($hook, $implementation) {
include_once './core/includes/install.inc';
drupal_load_updates();
$info = $this
->codeLocate($implementation . "_{$hook}");
$exec = drush_get_editor();
drush_shell_exec_interactive($exec, $info['file']);
}
public function hookInteract(Input $input, Output $output) {
if (!$input
->getArgument('implementation')) {
if ($hook_implementations = $this
->getModuleHandler()
->getImplementations($input
->getArgument('hook'))) {
if (!($choice = $this
->io()
->choice('Enter the number of the hook implementation you wish to view.', array_combine($hook_implementations, $hook_implementations)))) {
throw new UserAbortException();
}
$input
->setArgument('implementation', $choice);
}
else {
throw new \Exception(dt('No implementations'));
}
}
}
function event($event, $implementation) {
$info = $this
->codeLocate($implementation);
$exec = drush_get_editor();
drush_shell_exec_interactive($exec, $info['file']);
}
public function interactEvent(Input $input, Output $output) {
$dispatcher = $this
->getEventDispatcher();
if (!$input
->getArgument('event')) {
$events = array(
'kernel.controller',
'kernel.exception',
'kernel.request',
'kernel.response',
'kernel.terminate',
'kernel.view',
);
$events = array_combine($events, $events);
if (!($event = $this
->io()
->choice('Enter the event you wish to explore.', $events))) {
throw new UserAbortException();
}
$input
->setArgument('event', $event);
}
if ($implementations = $dispatcher
->getListeners($event)) {
foreach ($implementations as $implementation) {
$callable = get_class($implementation[0]) . '::' . $implementation[1];
$choices[$callable] = $callable;
}
if (!($choice = $this
->io()
->choice('Enter the number of the implementation you wish to view.', $choices))) {
throw new UserAbortException();
}
$input
->setArgument('implementation', $choice);
}
else {
throw new \Exception(dt('No implementations.'));
}
}
public function token($options = [
'format' => 'table',
]) {
$all = $this
->getToken()
->getInfo();
foreach ($all['tokens'] as $group => $tokens) {
foreach ($tokens as $key => $token) {
$rows[] = [
'group' => $group,
'token' => $key,
'name' => $token['name'],
];
}
}
return new RowsOfFields($rows);
}
public function uuid() {
$uuid = new Php();
return $uuid
->generate();
}
function codeLocate($function_name) {
include_once './core/includes/install.inc';
drupal_load_updates();
if (strpos($function_name, '::') === FALSE) {
if (!function_exists($function_name)) {
throw new \Exception(dt('Function not found'));
}
$reflect = new \ReflectionFunction($function_name);
}
else {
list($class, $method) = explode('::', $function_name);
if (!method_exists($class, $method)) {
throw new \Exception(dt('Method not found'));
}
$reflect = new \ReflectionMethod($class, $method);
}
return array(
'file' => $reflect
->getFileName(),
'startline' => $reflect
->getStartLine(),
'endline' => $reflect
->getEndLine(),
);
}
public function services($prefix = NULL, $options = [
'format' => 'yaml',
]) {
$container = $this
->getContainer();
$services = $container
->getServiceIds();
if (isset($prefix)) {
$services = preg_grep("/{$prefix}/", $services);
}
if (empty($services)) {
throw new \Exception(dt('No container services found.'));
}
sort($services);
return $services;
}
}