EventDispatcherDebugCommand.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. /**
  19. * A console command for retrieving information about event dispatcher.
  20. *
  21. * @author Matthieu Auger <mail@matthieuauger.com>
  22. */
  23. class EventDispatcherDebugCommand extends ContainerAwareCommand
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function configure()
  29. {
  30. $this
  31. ->setName('debug:event-dispatcher')
  32. ->setDefinition(array(
  33. new InputArgument('event', InputArgument::OPTIONAL, 'An event name'),
  34. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  35. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
  36. ))
  37. ->setDescription('Displays configured listeners for an application')
  38. ->setHelp(<<<'EOF'
  39. The <info>%command.name%</info> command displays all configured listeners:
  40. <info>php %command.full_name%</info>
  41. To get specific listeners for an event, specify its name:
  42. <info>php %command.full_name% kernel.request</info>
  43. EOF
  44. )
  45. ;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. *
  50. * @throws \LogicException
  51. */
  52. protected function execute(InputInterface $input, OutputInterface $output)
  53. {
  54. $io = new SymfonyStyle($input, $output);
  55. $dispatcher = $this->getEventDispatcher();
  56. $options = array();
  57. if ($event = $input->getArgument('event')) {
  58. if (!$dispatcher->hasListeners($event)) {
  59. $io->warning(sprintf('The event "%s" does not have any registered listeners.', $event));
  60. return;
  61. }
  62. $options = array('event' => $event);
  63. }
  64. $helper = new DescriptorHelper();
  65. $options['format'] = $input->getOption('format');
  66. $options['raw_text'] = $input->getOption('raw');
  67. $options['output'] = $io;
  68. $helper->describe($io, $dispatcher, $options);
  69. }
  70. /**
  71. * Loads the Event Dispatcher from the container.
  72. *
  73. * @return EventDispatcherInterface
  74. */
  75. protected function getEventDispatcher()
  76. {
  77. return $this->getContainer()->get('event_dispatcher');
  78. }
  79. }