class DevelHookCommand
Same name in this branch
- 5.x src/Drush/Commands/DevelHookCommand.php \Drupal\devel\Drush\Commands\DevelHookCommand
Lists the implementations of a hook and opens one in an editor.
Attributes
#[AsCommand(name: self::NAME, description: 'List implementations of a given hook and optionally edit one.', aliases: [
'fnh',
'fn-hook',
'hook',
'devel-hook',
], usages: [
'devel:hook cron',
'devel:hook cron system',
'devel:hook cron --editor="code --wait"',
])]
Hierarchy
- class \Drupal\devel\Command\DevelHookCommand uses \Drupal\devel\Command\CodeLocationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of DevelHookCommand
2 files declare their use of DevelHookCommand
- DevelHookCommand.php in src/
Drush/ Commands/ DevelHookCommand.php - DevelHookCommandTest.php in tests/
src/ Kernel/ DevelHookCommandTest.php
File
-
src/
Command/ DevelHookCommand.php, line 21
Namespace
Drupal\devel\CommandView source
class DevelHookCommand {
use CodeLocationTrait;
use StringTranslationTrait;
public const NAME = 'devel:hook';
public function __construct(protected readonly ModuleHandlerInterface $moduleHandler) {
}
/**
* Prompts for an implementation when none was given.
*/
public function askForImplementation(InputInterface $input, OutputInterface $output) : void {
$hook = $input->getArgument('hook');
if (!$hook || $input->getArgument('implementation')) {
return;
}
if ($implementations = $this->getImplementations($hook)) {
$io = new SymfonyStyle($input, $output);
$choice = $io->choice((string) $this->t('Select the hook implementation you wish to view.'), array_keys($implementations));
$input->setArgument('implementation', $choice);
}
}
/**
* Lists the implementations of a hook, or opens one of them in an editor.
*/
public function __invoke(InputInterface $input, OutputInterface $output, #[Argument('The name of the hook to explore.')] string $hook, #[Argument('The implementation to edit: a module name, or a function or Class::method name. Usually omitted.')] ?string $implementation = NULL, #[Option('A string of bash which launches your preferred text editor.')] string $editor = '${VISUAL-${EDITOR-vi}}') : int {
$implementations = $this->getImplementations($hook);
if ($implementations === []) {
throw new \InvalidArgumentException('No implementations.');
}
// Without an implementation to edit, list them all.
if ($implementation === NULL || $implementation === '') {
foreach ($implementations as $info) {
$output->writeln($info['file'] . ':' . $info['line']);
}
return Command::SUCCESS;
}
$info = $implementations[ltrim($implementation, '\\')] ?? array_find($implementations, static fn(array $info): bool => $info['module'] === $implementation);
if ($info === NULL) {
throw new \InvalidArgumentException(sprintf('%s does not implement hook_%s(). Implementations: %s', $implementation, $hook, implode(', ', array_keys($implementations))));
}
$this->edit($input, $output, $info, $editor);
return Command::SUCCESS;
}
/**
* Finds the implementations of a hook.
*
* @return array<string, array{module: string, name: string, file: string, line: int}>
* The implementations, keyed by function or Class::method name.
*/
protected function getImplementations(string $hook) : array {
$listeners = [];
// Despite its name, invokeAllWith() only passes each implementation to the
// callback. The hooks are collected here, never run.
$this->moduleHandler
->invokeAllWith($hook, static function (callable $listener, string $module) use (&$listeners) : void {
$listeners[] = [
$module,
$listener,
];
});
// Hooks in .install files, such as hook_install() and hook_update_N(), are
// not registered as implementations. Find those by function name.
include_once DRUPAL_ROOT . '/core/includes/install.inc';
drupal_load_updates();
foreach (array_keys($this->moduleHandler
->getModuleList()) as $module) {
if (function_exists($module . '_' . $hook)) {
$listeners[] = [
$module,
$module . '_' . $hook,
];
}
}
$implementations = [];
foreach ($listeners as [$module, $listener]) {
$location = $this->locate($listener);
$implementations[$location['name']] ??= [
'module' => $module,
] + $location;
}
return $implementations;
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|
| CodeLocationTrait::edit | protected | function | Opens a file in the user's editor. | |
| CodeLocationTrait::locate | protected | function | Describes where a callable is defined. | |
| DevelHookCommand::askForImplementation | public | function | Prompts for an implementation when none was given. | |
| DevelHookCommand::getImplementations | protected | function | Finds the implementations of a hook. | |
| DevelHookCommand::NAME | public | constant | ||
| DevelHookCommand::__construct | public | function | ||
| DevelHookCommand::__invoke | public | function | Lists the implementations of a hook, or opens one of them in an editor. | |
| StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 |
| StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |
| StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 |
| StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |