Same filename in this branch
- 5.x src/Drush/Commands/DevelEventCommand.php
Namespace
Drupal\devel\Command
File
-
src/Command/DevelEventCommand.php
View source
<?php
declare (strict_types=1);
namespace Drupal\devel\Command;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Interact;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class DevelEventCommand {
use CodeLocationTrait;
use StringTranslationTrait;
public const NAME = 'devel:event';
public function __construct(protected readonly EventDispatcherInterface $eventDispatcher) {
}
public function askForImplementation(InputInterface $input, OutputInterface $output) : void {
$io = new SymfonyStyle($input, $output);
if (!$input->getArgument('event')) {
$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);
}
}
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.');
}
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;
}
protected function getImplementations(string $event) : array {
$implementations = [];
foreach ($this->eventDispatcher
->getListeners($event) as $listener) {
$location = $this->locate($listener);
$implementations[$location['name']] ??= $location;
}
return $implementations;
}
}
Classes
| Title |
Deprecated |
Summary |
| DevelEventCommand |
|
Lists the listeners of an event and opens one in an editor. |