View source
<?php
namespace Drupal\devel\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Consolidation\SiteProcess\Util\Escape;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Utility\Token;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
use Drush\Exec\ExecTrait;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\Utils\StringUtils;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Output\Output;
class DevelCommands extends DrushCommands implements SiteAliasManagerAwareInterface {
use SiteAliasManagerAwareTrait;
use ExecTrait;
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);
$process = $this
->processManager()
->drush($this
->siteAliasManager()
->getSelf(), 'pm:uninstall', [
$modules_str,
]);
$process
->mustRun();
$process = $this
->processManager()
->drush($this
->siteAliasManager()
->getSelf(), 'pm:enable', [
$modules_str,
]);
$process
->mustRun();
}
public function hook($hook, $implementation) {
include_once './core/includes/install.inc';
drupal_load_updates();
$info = $this
->codeLocate($implementation . "_{$hook}");
$exec = self::getEditor();
$cmd = sprintf($exec, Escape::shellArg($info['file']));
$process = $this
->processManager()
->shell($cmd);
$process
->setTty(TRUE);
$process
->mustRun();
}
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'));
}
}
}
public function event($event, $implementation) {
$info = $this
->codeLocate($implementation);
$exec = self::getEditor();
$cmd = sprintf($exec, Escape::shellArg($info['file']));
$process = $this
->processManager()
->shell($cmd);
$process
->setTty(TRUE);
$process
->mustRun();
}
public function interactEvent(Input $input, Output $output) {
$dispatcher = $this
->getEventDispatcher();
$event = $input
->getArgument('event');
if (!$event) {
$events = [
'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();
}
public 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 [
'file' => $reflect
->getFileName(),
'startline' => $reflect
->getStartLine(),
'endline' => $reflect
->getEndLine(),
];
}
public function services($prefix = NULL, array $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;
}
}