Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
21 / 21 |
| Event | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
10 | |
100.00% |
21 / 21 |
| setDispatcher | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| addHandler | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
|||
| handle | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| hasHandler | |
100.00% |
1 / 1 |
4 | |
100.00% |
8 / 8 |
|||
| getHandlers | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 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 | * GNU social's event handler wrapper around Symfony's, |
| 22 | * keeping our old interface, which is more convenient and just as powerful |
| 23 | * |
| 24 | * @package GNUsocial |
| 25 | * @category Event |
| 26 | * |
| 27 | * @author Hugo Sales <hugo@hsal.es> |
| 28 | * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org |
| 29 | * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later |
| 30 | */ |
| 31 | |
| 32 | namespace App\Core; |
| 33 | |
| 34 | use ReflectionFunction; |
| 35 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 36 | use Symfony\Component\EventDispatcher\GenericEvent; |
| 37 | |
| 38 | abstract class Event |
| 39 | { |
| 40 | /** |
| 41 | * Constants to be returned from event handlers, for increased clarity |
| 42 | * |
| 43 | * bool stop - Stop other handlers from processing the event |
| 44 | * bool next - Allow the other handlers to process the event |
| 45 | */ |
| 46 | public const stop = false; |
| 47 | public const next = true; |
| 48 | |
| 49 | private static EventDispatcherInterface $dispatcher; |
| 50 | |
| 51 | public static function setDispatcher(EventDispatcherInterface $dis): void |
| 52 | { |
| 53 | self::$dispatcher = $dis; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Add an event handler |
| 58 | * |
| 59 | * To run some code at a particular point in GNU social processing. |
| 60 | * Named events include receiving an XMPP message, adding a new notice, |
| 61 | * or showing part of an HTML page. |
| 62 | * |
| 63 | * The arguments to the handler vary by the event. Handlers can return |
| 64 | * two possible values: false means that the event has been replaced by |
| 65 | * the handler completely, and no default processing should be done. |
| 66 | * Non-false means successful handling, and that the default processing |
| 67 | * should succeed. (Note that this only makes sense for some events.) |
| 68 | * |
| 69 | * Handlers can also abort processing by throwing an exception; these will |
| 70 | * be caught by the closest code and displayed as errors. |
| 71 | * |
| 72 | * @param string $name Name of the event |
| 73 | * @param callable $handler Code to run |
| 74 | * @param int $priority Higher runs first |
| 75 | * @param string $ns |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public static function addHandler(string $name, |
| 80 | callable $handler, |
| 81 | int $priority = 0, |
| 82 | string $ns = 'GNUsocial.'): void |
| 83 | { |
| 84 | self::$dispatcher->addListener( |
| 85 | $ns . $name, |
| 86 | function ($event, $event_name, $dispatcher) use ($handler, $name) { |
| 87 | // Old style of events (preferred) |
| 88 | if ($event instanceof GenericEvent) { |
| 89 | if (call_user_func_array($handler, $event->getArguments()) === self::stop) { |
| 90 | $event->stopPropagation(); |
| 91 | } |
| 92 | return $event; |
| 93 | } |
| 94 | // @codeCoverageIgnoreStart |
| 95 | // Symfony style of events |
| 96 | Log::warning("Event::addHandler for {$name} doesn't Conform to GNU social guidelines. Use of this style of event is discouraged."); |
| 97 | call_user_func($handler, $event, $event_name, $dispatcher); |
| 98 | return null; |
| 99 | // @codeCoverageIgnoreEnd |
| 100 | }, |
| 101 | $priority |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Handle an event |
| 107 | * |
| 108 | * Events are any point in the code that we want to expose for admins |
| 109 | * or third-party developers to use. |
| 110 | * |
| 111 | * We pass in an array of arguments (including references, for stuff |
| 112 | * that can be changed), and each assigned handler gets run with those |
| 113 | * arguments. Exceptions can be thrown to indicate an error. |
| 114 | * |
| 115 | * @param string $name Name of the event that's happening |
| 116 | * @param array $args Arguments for handlers |
| 117 | * @param string $ns Namspace for the event |
| 118 | * |
| 119 | * @return bool flag saying whether to continue processing, based |
| 120 | * on results of handlers. |
| 121 | */ |
| 122 | public static function handle(string $name, array $args = [], string $ns = 'GNUsocial.'): bool |
| 123 | { |
| 124 | return !(self::$dispatcher->dispatch(new GenericEvent($name, $args), $ns . $name)->isPropagationStopped()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Check to see if an event handler exists |
| 129 | * |
| 130 | * Look to see if there's any handler for a given event, or narrow |
| 131 | * by providing the name of a specific plugin class. |
| 132 | * |
| 133 | * @param string $name Name of the event to look for |
| 134 | * @param string $plugin Optional name of the plugin class to look for |
| 135 | * |
| 136 | * @return bool flag saying whether such a handler exists |
| 137 | */ |
| 138 | public static function hasHandler(string $name, ?string $plugin = null, string $ns = 'GNUsocial.'): bool |
| 139 | { |
| 140 | $listeners = self::$dispatcher->getListeners($ns . $name); |
| 141 | if (isset($plugin)) { |
| 142 | foreach ($listeners as $event_handler) { |
| 143 | $class = (new ReflectionFunction((new ReflectionFunction($event_handler))->getStaticVariables()['handler']))->getClosureScopeClass()->getName(); |
| 144 | if ($class === $plugin) { |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | } else { |
| 149 | return !empty($listeners); |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the array of handlers for $name |
| 156 | * |
| 157 | * @param string $name Name of event |
| 158 | * |
| 159 | * @return array |
| 160 | * @return array |
| 161 | */ |
| 162 | public static function getHandlers(string $name, string $ns = 'GNUsocial.'): array |
| 163 | { |
| 164 | return self::$dispatcher->getListeners($ns . $name); |
| 165 | } |
| 166 | } |