function DevelEventCommand::__invoke

Lists the listeners of an event, or opens one of them in an editor.

File

src/Command/DevelEventCommand.php, line 70

Class

DevelEventCommand
Lists the listeners of an event and opens one in an editor.

Namespace

Drupal\devel\Command

Code

public function __invoke(InputInterface $input, OutputInterface $output, #[Argument('The name of the event to explore.')] string $event, #[Argument('The implementation to edit, as 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($event);
  if ($implementations === []) {
    throw new \InvalidArgumentException('No implementations.');
  }
  // Without an implementation to edit, list them all.
  if ($implementation === NULL || $implementation === '') {
    foreach ($implementations as $location) {
      $output->writeln($location['file'] . ':' . $location['line']);
    }
    return Command::SUCCESS;
  }
  $location = $implementations[ltrim($implementation, '\\')] ?? NULL;
  if ($location === NULL) {
    throw new \InvalidArgumentException(sprintf('%s does not listen to %s. Implementations: %s', $implementation, $event, implode(', ', array_keys($implementations))));
  }
  $this->edit($input, $output, $location, $editor);
  return Command::SUCCESS;
}