-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] [Command] Event Dispatcher Debug - Display registered listeners #10388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
/** | ||
* A console command for retrieving information about event dispatcher | ||
* | ||
* @author Matthieu Auger <mail@matthieuauger.com> | ||
*/ | ||
class EventDispatcherDebugCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('debug:event-dispatcher') | ||
->setDefinition(array( | ||
new InputArgument('event', InputArgument::OPTIONAL, 'An event name (foo)'), | ||
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output description in other formats', 'txt'), | ||
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'), | ||
)) | ||
->setDescription('Displays configured listeners for an application') | ||
->setHelp(<<<EOF | ||
The <info>%command.name%</info> command displays all configured listeners: | ||
|
||
<info>php %command.full_name%</info> | ||
|
||
To get specific listeners for an event, specify its name: | ||
|
||
<info>php %command.full_name% kernel.request</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws \LogicException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if ($event = $input->getArgument('event')) { | ||
$options = array('event' => $event); | ||
} else { | ||
$options = array(); | ||
} | ||
|
||
$dispatcher = $this->getEventDispatcher(); | ||
|
||
$helper = new DescriptorHelper(); | ||
$options['format'] = $input->getOption('format'); | ||
$options['raw_text'] = $input->getOption('raw'); | ||
$helper->describe($output, $dispatcher, $options); | ||
} | ||
|
||
/** | ||
* Loads the Event Dispatcher from the container | ||
* | ||
* @return EventDispatcherInterface | ||
*/ | ||
protected function getEventDispatcher() | ||
{ | ||
return $this->getContainer()->get('event_dispatcher'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
|
@@ -134,6 +135,22 @@ protected function describeContainerAlias(Alias $alias, array $options = array() | |
$this->writeData($this->getContainerAliasData($alias), $options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = array()) | ||
{ | ||
$this->writeData($this->getEventDispatcherListenersData($eventDispatcher, array_key_exists('event', $options) ? $options['event'] : null), $options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function describeCallable($callable, array $options = array()) | ||
{ | ||
$this->writeData($this->getCallableData($callable, $options), $options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -222,4 +239,96 @@ private function getContainerAliasData(Alias $alias) | |
'public' => $alias->isPublic(), | ||
); | ||
} | ||
|
||
/** | ||
* @param EventDispatcherInterface $eventDispatcher | ||
* @param string|null $event | ||
* | ||
* @return array | ||
*/ | ||
private function getEventDispatcherListenersData(EventDispatcherInterface $eventDispatcher, $event = null) | ||
{ | ||
$data = array(); | ||
|
||
$registeredListeners = $eventDispatcher->getListeners($event); | ||
if (null !== $event) { | ||
foreach ($registeredListeners as $listener) { | ||
$data[] = $this->getCallableData($listener); | ||
} | ||
} else { | ||
ksort($registeredListeners); | ||
|
||
foreach ($registeredListeners as $eventListened => $eventListeners) { | ||
foreach ($eventListeners as $eventListener) { | ||
$data[$eventListened][] = $this->getCallableData($eventListener); | ||
} | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param callable $callable | ||
* @param array $options | ||
* | ||
* @return array | ||
*/ | ||
private function getCallableData($callable, array $options = array()) | ||
{ | ||
$data = array(); | ||
|
||
if (is_array($callable)) { | ||
$data['type'] = 'function'; | ||
|
||
if (is_object($callable[0])) { | ||
$data['name'] = $callable[1]; | ||
$data['class'] = get_class($callable[0]); | ||
} else { | ||
if (0 !== strpos($callable[1], 'parent::')) { | ||
$data['name'] = $callable[1]; | ||
$data['class'] = $callable[0]; | ||
$data['static'] = true; | ||
} else { | ||
$data['name'] = substr($callable[1], 8); | ||
$data['class'] = $callable[0]; | ||
$data['static'] = true; | ||
$data['parent'] = true; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if (is_string($callable)) { | ||
$data['type'] = 'function'; | ||
|
||
if (false === strpos($callable, '::')) { | ||
$data['name'] = $callable; | ||
} else { | ||
$callableParts = explode('::', $callable); | ||
|
||
$data['name'] = $callableParts[1]; | ||
$data['class'] = $callableParts[0]; | ||
$data['static'] = true; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
if ($callable instanceof \Closure) { | ||
$data['type'] = 'closure'; | ||
|
||
return $data; | ||
} | ||
|
||
if (method_exists($callable, '__invoke')) { | ||
$data['type'] = 'object'; | ||
$data['name'] = get_class($callable); | ||
|
||
return $data; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if you will not reach any of those There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right, I should return $data, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finally opted to raise an exception as if somedays it happens, we should know it instead of silently ignore it |
||
|
||
throw new \InvalidArgumentException('Callable is not describable.'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove
(foo)
. It looks a bit like a default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback, I copied that from the debug:container command (https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php#L46). I should be able to remove it by tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, if it's there I'd keep it here too to be consistent.