* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Bundle\FrameworkBundle\Command; use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * A console command for retrieving information about event dispatcher. * * @author Matthieu Auger * * @final */ class EventDispatcherDebugCommand extends Command { protected static $defaultName = 'debug:event-dispatcher'; private $dispatcher; public function __construct(EventDispatcherInterface $dispatcher) { parent::__construct(); $this->dispatcher = $dispatcher; } /** * {@inheritdoc} */ protected function configure() { $this ->setDefinition([ new InputArgument('event', InputArgument::OPTIONAL, 'An event name'), new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'), new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'), ]) ->setDescription('Display configured listeners for an application') ->setHelp(<<<'EOF' The %command.name% command displays all configured listeners: php %command.full_name% To get specific listeners for an event, specify its name: php %command.full_name% kernel.request EOF ) ; } /** * {@inheritdoc} * * @throws \LogicException */ protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $options = []; if ($event = $input->getArgument('event')) { if (!$this->dispatcher->hasListeners($event)) { $io->getErrorStyle()->warning(sprintf('The event "%s" does not have any registered listeners.', $event)); return 0; } $options = ['event' => $event]; } $helper = new DescriptorHelper(); $options['format'] = $input->getOption('format'); $options['raw_text'] = $input->getOption('raw'); $options['output'] = $io; $helper->describe($io, $this->dispatcher, $options); return 0; } }