class DevelEventCommand

Same name in this branch
  1. 5.x src/Drush/Commands/DevelEventCommand.php \Drupal\devel\Drush\Commands\DevelEventCommand

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

Attributes

#[AsCommand(name: self::NAME, description: 'List implementations of a given event and optionally edit one.', aliases: [ 'fne', 'fn-event', 'event', ], usages: [ 'devel:event kernel.terminate', 'devel:event kernel.terminate --editor="code --wait"', ])]

Hierarchy

Expanded class hierarchy of DevelEventCommand

2 files declare their use of DevelEventCommand
DevelCommandsKernelTest.php in tests/src/Kernel/DevelCommandsKernelTest.php
DevelEventCommand.php in src/Drush/Commands/DevelEventCommand.php

File

src/Command/DevelEventCommand.php, line 21

Namespace

Drupal\devel\Command
View source
class DevelEventCommand {
  use CodeLocationTrait;
  use StringTranslationTrait;
  public const NAME = 'devel:event';
  public function __construct(protected readonly EventDispatcherInterface $eventDispatcher) {
  }
  
  /**
   * Prompts for an event, and for an implementation, when not given.
   */
  public function askForImplementation(InputInterface $input, OutputInterface $output) : void {
    $io = new SymfonyStyle($input, $output);
    if (!$input->getArgument('event')) {
      // @todo Expand this list.
      $events = [
        'kernel.controller',
        'kernel.exception',
        'kernel.request',
        'kernel.response',
        'kernel.terminate',
        'kernel.view',
      ];
      $input->setArgument('event', $io->choice((string) $this->t('Enter the event you wish to explore.'), $events));
    }
    if (!$input->getArgument('implementation') && $implementations = $this->getImplementations($input->getArgument('event'))) {
      $choice = $io->choice((string) $this->t('Select the implementation you wish to view.'), array_keys($implementations));
      $input->setArgument('implementation', $choice);
    }
  }
  
  /**
   * Lists the listeners of an event, or opens one of them in an editor.
   */
  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;
  }
  
  /**
   * Finds the listeners of an event.
   *
   * @return array<string, array{name: string, file: string, line: int}>
   *   The listeners, keyed by function or Class::method name.
   */
  protected function getImplementations(string $event) : array {
    $implementations = [];
    foreach ($this->eventDispatcher
      ->getListeners($event) as $listener) {
      $location = $this->locate($listener);
      $implementations[$location['name']] ??= $location;
    }
    return $implementations;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
CodeLocationTrait::edit protected function Opens a file in the user&#039;s editor.
CodeLocationTrait::locate protected function Describes where a callable is defined.
DevelEventCommand::askForImplementation public function Prompts for an event, and for an implementation, when not given.
DevelEventCommand::getImplementations protected function Finds the listeners of an event.
DevelEventCommand::NAME public constant
DevelEventCommand::__construct public function
DevelEventCommand::__invoke public function Lists the listeners of an event, 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.