. // }}} /** * Command to search for event by pattern * * @package GNUsocial * @category Command * * @author Hugo Sales * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ namespace App\Command; use Functional as F; use ReflectionFunction; use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Get a list of event registered in GNU social * * Testing unfeasable, since it outputs stuff * * @codeCoverageIgnore */ class ListEventsCommand extends Command { protected static $defaultName = 'app:events'; private EventDispatcherInterface $dispatcher; public function __construct(EventDispatcherInterface $dispatcher) { parent::__construct(); $this->dispatcher = $dispatcher; } protected function configure() { $this->setDefinition([new InputArgument('pattern', InputArgument::OPTIONAL, 'An event pattern to look for')]) ->setDescription('Search for an event') ->setHelp(<<<'EOF' The %command.name% command displays GNU social event listeners: php %command.full_name% To get specific listeners for an event, specify its name: php %command.full_name% kernel.request EOF ); } /** * Execute the command, outputing a list of GNU social events */ protected function execute(InputInterface $input, OutputInterface $output): int { $patterm = $input->getArgument('pattern') ?? 'GNUsocial.*'; $listeners = $this->dispatcher->getListeners(); $listeners = F\select($listeners, function ($_, $key, $__) use ($patterm) { return preg_match('/' . $patterm . '/', $key); }); ksort($listeners); foreach ($listeners as $event => $listener) { $event = explode('.', $event)[1]; $output->writeln("Event '{$event}':"); foreach ($listener as $c) { $r = new ReflectionFunction($c); $m = $r->getStaticVariables()['handler']; $output->writeln("\t" . get_class($m[0]) . '::' . $m[1]); } $output->writeln(''); } if (!$input->hasArgument('pattern')) { $io = new SymfonyStyle($input, $output); $options = []; $helper = new DescriptorHelper(); $options['output'] = $io; $helper->describe($io, null, $options); } return 0; } }