[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Event Package 4 * 5 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Event\Command; 10 11 use Joomla\Console\Command\AbstractCommand; 12 use Joomla\Event\DispatcherAwareInterface; 13 use Joomla\Event\DispatcherAwareTrait; 14 use Joomla\Event\DispatcherInterface; 15 use Symfony\Component\Console\Input\InputArgument; 16 use Symfony\Component\Console\Input\InputInterface; 17 use Symfony\Component\Console\Output\OutputInterface; 18 use Symfony\Component\Console\Style\SymfonyStyle; 19 20 /** 21 * Command listing information about the application's event dispatcher. 22 * 23 * @since 2.0.0 24 */ 25 class DebugEventDispatcherCommand extends AbstractCommand implements DispatcherAwareInterface 26 { 27 use DispatcherAwareTrait; 28 29 /** 30 * The default command name 31 * 32 * @var string 33 * @since 2.0.0 34 */ 35 protected static $defaultName = 'debug:event-dispatcher'; 36 37 /** 38 * Instantiate the command. 39 * 40 * @param DispatcherInterface $dispatcher The application event dispatcher. 41 * 42 * @since 2.0.0 43 */ 44 public function __construct(DispatcherInterface $dispatcher) 45 { 46 $this->setDispatcher($dispatcher); 47 48 parent::__construct(); 49 } 50 51 /** 52 * Configure the command. 53 * 54 * @return void 55 * 56 * @since 2.0.0 57 */ 58 protected function configure(): void 59 { 60 $this->setDescription("Displays information about the application's event dispatcher"); 61 $this->addArgument('event', InputArgument::OPTIONAL, 'Show the listeners for a specific event'); 62 $this->setHelp(<<<'EOF' 63 The <info>%command.name%</info> command lists all of the registered event handlers in an application's event dispatcher: 64 65 <info>php %command.full_name%</info> 66 67 To get specific listeners for an event, specify its name: 68 69 <info>php %command.full_name% application.before_execute</info> 70 EOF 71 ); 72 } 73 74 /** 75 * Internal function to execute the command. 76 * 77 * @param InputInterface $input The input to inject into the command. 78 * @param OutputInterface $output The output to inject into the command. 79 * 80 * @return integer The command exit code 81 * 82 * @since 2.0.0 83 */ 84 protected function doExecute(InputInterface $input, OutputInterface $output): int 85 { 86 $io = new SymfonyStyle($input, $output); 87 88 if ($event = $input->getArgument('event')) 89 { 90 $listeners = $this->dispatcher->getListeners($event); 91 92 if (empty($listeners)) 93 { 94 $io->warning(sprintf('The event "%s" does not have any registered listeners.', $event)); 95 96 return 0; 97 } 98 99 $io->title(sprintf('%s Registered Listeners for "%s" Event', $this->getApplication()->getName(), $event)); 100 101 $this->renderEventListenerTable($listeners, $io); 102 103 return 0; 104 } 105 106 $listeners = $this->dispatcher->getListeners(); 107 108 if (empty($listeners)) 109 { 110 $io->comment('There are no listeners registered to the event dispatcher.'); 111 112 return 0; 113 } 114 115 $io->title(sprintf('%s Registered Listeners Grouped By Event', $this->getApplication()->getName())); 116 117 ksort($listeners); 118 119 foreach ($listeners as $subscribedEvent => $eventListeners) 120 { 121 $io->section(sprintf('"%s" event', $subscribedEvent)); 122 123 $this->renderEventListenerTable($eventListeners, $io); 124 } 125 126 return 0; 127 } 128 129 /** 130 * Formats a callable resource to be displayed in the console output 131 * 132 * @param callable $callable A callable resource to format 133 * 134 * @return string 135 * 136 * @since 2.0.0 137 * @throws \ReflectionException 138 * @note This method is based on \Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor::formatCallable() 139 */ 140 private function formatCallable($callable): string 141 { 142 if (\is_array($callable)) 143 { 144 if (\is_object($callable[0])) 145 { 146 return sprintf('%s::%s()', \get_class($callable[0]), $callable[1]); 147 } 148 149 return sprintf('%s::%s()', $callable[0], $callable[1]); 150 } 151 152 if (\is_string($callable)) 153 { 154 return sprintf('%s()', $callable); 155 } 156 157 if ($callable instanceof \Closure) 158 { 159 $r = new \ReflectionFunction($callable); 160 161 if (strpos($r->name, '{closure}') !== false) 162 { 163 return 'Closure()'; 164 } 165 166 if (null !== $class = $r->getClosureScopeClass()) 167 { 168 return sprintf('%s::%s()', $class->name, $r->name); 169 } 170 171 return $r->name . '()'; 172 } 173 174 if (method_exists($callable, '__invoke')) 175 { 176 return sprintf('%s::__invoke()', \get_class($callable)); 177 } 178 179 throw new \InvalidArgumentException('Callable is not describable.'); 180 } 181 182 /** 183 * Renders the table of listeners for an event 184 * 185 * @param array $eventListeners The listeners for an event 186 * @param SymfonyStyle $io The I/O helper 187 * 188 * @return void 189 * 190 * @since 2.0.0 191 */ 192 private function renderEventListenerTable(array $eventListeners, SymfonyStyle $io): void 193 { 194 $tableHeaders = ['Order', 'Callable']; 195 $tableRows = []; 196 197 foreach ($eventListeners as $order => $listener) 198 { 199 $tableRows[] = [ 200 sprintf('#%d', $order + 1), 201 $this->formatCallable($listener), 202 ]; 203 } 204 205 $io->table($tableHeaders, $tableRows); 206 } 207 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |