EventDispatcherDebugCommand.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. /**
  20. * A console command for retrieving information about event dispatcher.
  21. *
  22. * @author Matthieu Auger <mail@matthieuauger.com>
  23. *
  24. * @final
  25. */
  26. class EventDispatcherDebugCommand extends Command
  27. {
  28. protected static $defaultName = 'debug:event-dispatcher';
  29. private $dispatcher;
  30. public function __construct(EventDispatcherInterface $dispatcher)
  31. {
  32. parent::__construct();
  33. $this->dispatcher = $dispatcher;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function configure()
  39. {
  40. $this
  41. ->setDefinition([
  42. new InputArgument('event', InputArgument::OPTIONAL, 'An event name'),
  43. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  44. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
  45. ])
  46. ->setDescription('Display configured listeners for an application')
  47. ->setHelp(<<<'EOF'
  48. The <info>%command.name%</info> command displays all configured listeners:
  49. <info>php %command.full_name%</info>
  50. To get specific listeners for an event, specify its name:
  51. <info>php %command.full_name% kernel.request</info>
  52. EOF
  53. )
  54. ;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @throws \LogicException
  60. */
  61. protected function execute(InputInterface $input, OutputInterface $output): int
  62. {
  63. $io = new SymfonyStyle($input, $output);
  64. $options = [];
  65. if ($event = $input->getArgument('event')) {
  66. if (!$this->dispatcher->hasListeners($event)) {
  67. $io->getErrorStyle()->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
  68. return 0;
  69. }
  70. $options = ['event' => $event];
  71. }
  72. $helper = new DescriptorHelper();
  73. $options['format'] = $input->getOption('format');
  74. $options['raw_text'] = $input->getOption('raw');
  75. $options['output'] = $io;
  76. $helper->describe($io, $this->dispatcher, $options);
  77. return 0;
  78. }
  79. }