Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
ListEventsCommand
n/a
0 / 0
n/a
0 / 0
6
n/a
0 / 0
 __construct
n/a
0 / 0
1
n/a
0 / 0
 configure
n/a
0 / 0
1
n/a
0 / 0
 execute
n/a
0 / 0
4
n/a
0 / 0
1<?php
2
3// {{{ License
4// This file is part of GNU social - https://www.gnu.org/software/social
5//
6// GNU social is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Affero General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU social is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU Affero General Public License for more details.
15//
16// You should have received a copy of the GNU Affero General Public License
17// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
18// }}}
19
20/**
21 * Command to search for event by pattern
22 *
23 * @package  GNUsocial
24 * @category Command
25 *
26 * @author    Hugo Sales <hugo@hsal.es>
27 * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
28 * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
29 */
30
31namespace App\Command;
32
33use Functional as F;
34use ReflectionFunction;
35use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
36use Symfony\Component\Console\Command\Command;
37use Symfony\Component\Console\Input\InputArgument;
38use Symfony\Component\Console\Input\InputInterface;
39use Symfony\Component\Console\Output\OutputInterface;
40use Symfony\Component\Console\Style\SymfonyStyle;
41use Symfony\Component\EventDispatcher\EventDispatcherInterface;
42
43/**
44 * Get a list of event registered in GNU social
45 *
46 * Testing unfeasable, since it outputs stuff
47 *
48 * @codeCoverageIgnore
49 */
50class ListEventsCommand extends Command
51{
52    protected static $defaultName = 'app:events';
53    private EventDispatcherInterface $dispatcher;
54
55    public function __construct(EventDispatcherInterface $dispatcher)
56    {
57        parent::__construct();
58        $this->dispatcher = $dispatcher;
59    }
60
61    protected function configure()
62    {
63        $this->setDefinition([new InputArgument('pattern', InputArgument::OPTIONAL, 'An event pattern to look for')])
64             ->setDescription('Search for an event')
65             ->setHelp(<<<'EOF'
66The <info>%command.name%</info> command displays GNU social event listeners:
67
68  <info>php %command.full_name%</info>
69
70To get specific listeners for an event, specify its name:
71
72  <info>php %command.full_name% kernel.request</info>
73EOF
74             );
75    }
76
77    /**
78     * Execute the command, outputing a list of GNU social events
79     */
80    protected function execute(InputInterface $input, OutputInterface $output): int
81    {
82        $patterm   = $input->getArgument('pattern') ?? 'GNUsocial.*';
83        $listeners = $this->dispatcher->getListeners();
84        $listeners = F\select($listeners,
85                              function ($_, $key, $__) use ($patterm) {
86                                  return preg_match('/' . $patterm . '/', $key);
87                              });
88
89        ksort($listeners);
90        foreach ($listeners as $event => $listener) {
91            $event = explode('.', $event)[1];
92            $output->writeln("<info>Event '<options=bold>{$event}</options=bold>':</info>");
93            foreach ($listener as $c) {
94                $r = new ReflectionFunction($c);
95                $m = $r->getStaticVariables()['handler'];
96                $output->writeln("\t" . get_class($m[0]) . '::' . $m[1]);
97            }
98            $output->writeln('');
99        }
100
101        if (!$input->hasArgument('pattern')) {
102            $io                = new SymfonyStyle($input, $output);
103            $options           = [];
104            $helper            = new DescriptorHelper();
105            $options['output'] = $io;
106            $helper->describe($io, null, $options);
107        }
108        return 0;
109    }
110}