function DevelHookCommand::__invoke

Lists the implementations of a hook, or opens one of them in an editor.

File

src/Command/DevelHookCommand.php, line 62

Class

DevelHookCommand
Lists the implementations of a hook and opens one in an editor.

Namespace

Drupal\devel\Command

Code

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;
}