-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle][Command] print registered event-listeners + event-subscribers #8234
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,143 @@ | ||
<?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\Component\Console\Helper\TableHelper; | ||
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\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
/** | ||
* A console command for retrieving information about events | ||
* | ||
* @author Florian Semm <floriansemm@googlemail.com> | ||
*/ | ||
class EventDispatcherDebugCommand extends ContainerAwareCommand | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('event_dispatcher:debug') | ||
->addArgument('event', InputArgument::OPTIONAL, 'Show listeners for a event', '') | ||
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. for an event 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. you can leave the '' last argument as it is defaulting to null also |
||
->setDescription('Displays current event-listeners and event-subscribers for an application'); | ||
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.
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. provide a help as all the other commands in the same folder |
||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
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. add docblock |
||
{ | ||
$containerBuilder = $this->getContainerBuilder(); | ||
|
||
$listeners = $this->getListeners($containerBuilder); | ||
$listeners = $this->getEventSubscriber($containerBuilder, $listeners); | ||
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. the name is plural here as indicated above, however i see that it return a misleading $listener variable i think it would be nicer if we can split the two groups but in the same table, bet it will be fun to do a table divisor 👶 👍 |
||
|
||
$event = $input->getArgument('event'); | ||
if ($event != '') { | ||
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. Should be: if ($input->hasArgument('event')) {
$event = $input->getArgument('event'); 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. I tried it out and it seem to be that the "event" argument is always set but empty 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. It's probably because you set default value of argument to 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. I have removed the default option 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. i don't see that in the code, i made a note |
||
if (!isset($listeners[$event])) { | ||
throw new \InvalidArgumentException(sprintf('The event %s does not exist', $event)); | ||
} | ||
|
||
$listeners = $listeners[$event]; | ||
|
||
$output->writeln(sprintf('<info>[event_dispatcher]</info> Information for event <info>%s</info>', $event)); | ||
} else { | ||
ksort($listeners); | ||
$listeners = array_reduce($listeners, function($result, $elements){ | ||
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. leave a space between |
||
foreach ($elements as $element) { | ||
$result[] = $element; | ||
} | ||
|
||
return $result; | ||
}); | ||
|
||
$output->writeln('<info>[event_dispatcher]</info> Listeners'); | ||
} | ||
|
||
$table = $this->getHelperSet()->get('table'); | ||
$table | ||
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. use Symfony\Component\Console\Helper\TableHelper;
// (...)
$table->setLayout(TableHelper::LAYOUT_BORDERLESS); |
||
->setHeaders(array('Class', 'Event')) | ||
->setLayout(TableHelper::LAYOUT_BORDERLESS) | ||
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. Why not using default layout? 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. The table should look like the "container:debug" command 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. But it does not, does it? I opened #8292 to fix that. |
||
->setRows($listeners) | ||
->render($output); | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $containerBuilder | ||
* @return array | ||
*/ | ||
private function getListeners(ContainerBuilder $containerBuilder) | ||
{ | ||
$definitions = $containerBuilder->findTaggedServiceIds('kernel.event_listener'); | ||
|
||
$listeners = array(); | ||
foreach ($definitions as $id => $definition) { | ||
$event = $definition[0]['event']; | ||
|
||
$class = $containerBuilder->getDefinition($id)->getClass(); | ||
|
||
$listeners[$event][] = array($class, $event); | ||
} | ||
|
||
return $listeners; | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $containerBuilder | ||
* @return array | ||
*/ | ||
private function getEventSubscriber(ContainerBuilder $containerBuilder, array $listeners) | ||
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. i think this should be getEventSubscribers no? |
||
{ | ||
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface'; | ||
foreach ($containerBuilder->findTaggedServiceIds('kernel.event_subscriber') as $id => $attributes) { | ||
$class = $containerBuilder->getDefinition($id)->getClass(); | ||
|
||
$refClass = new \ReflectionClass($class); | ||
if (!$refClass->implementsInterface($interface)) { | ||
continue; | ||
} | ||
|
||
$subscribedEvents = $class::getSubscribedEvents(); | ||
$events = array_keys($subscribedEvents); | ||
|
||
foreach ($events as $event) { | ||
$listeners[$event][] = array($class, $event); | ||
} | ||
} | ||
|
||
return $listeners; | ||
} | ||
|
||
/** | ||
* Loads the ContainerBuilder from the cache. | ||
* | ||
* @return ContainerBuilder | ||
*/ | ||
protected function getContainerBuilder() | ||
{ | ||
if (!$this->getApplication()->getKernel()->isDebug()) { | ||
throw new \LogicException(sprintf('Debug information about the EventDispatcher is only available in debug mode.')); | ||
} | ||
|
||
if (!is_file($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) { | ||
throw new \LogicException(sprintf('Debug information about the EventDispatcher could not be found. Please clear the cache and try again.')); | ||
} | ||
|
||
$container = new ContainerBuilder(); | ||
|
||
$loader = new XmlFileLoader($container, new FileLocator()); | ||
$loader->load($cachedFile); | ||
|
||
return $container; | ||
} | ||
} | ||
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. run php-cs-fixer |
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.
please add