[EVENT] Fixup implementation, as imformed by tests

This commit is contained in:
Hugo Sales 2021-07-21 16:41:53 +00:00
parent d22711504c
commit f9a022745e
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 12 additions and 8 deletions

View File

@ -31,6 +31,7 @@
namespace App\Core; namespace App\Core;
use ReflectionFunction;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
@ -85,15 +86,17 @@ abstract class Event
function ($event, $event_name, $dispatcher) use ($handler) { function ($event, $event_name, $dispatcher) use ($handler) {
// Old style of events (preferred) // Old style of events (preferred)
if ($event instanceof GenericEvent) { if ($event instanceof GenericEvent) {
if (call_user_func_array($handler, $event->getArguments()) == self::stop) { if (call_user_func_array($handler, $event->getArguments()) === self::stop) {
$event->stopPropagation(); $event->stopPropagation();
} }
return $event; return $event;
} }
// @codeCoverageIgnoreStart
// Symfony style of events // Symfony style of events
Log::warning("Event::addHandler for {$name} doesn't Conform to GNU social guidelines. Use of this style of event is discouraged");
call_user_func($handler, $event, $event_name, $dispatcher); call_user_func($handler, $event, $event_name, $dispatcher);
return null; return null;
// @codeCoverageIgnoreEnd
}, },
$priority $priority
); );
@ -132,12 +135,13 @@ abstract class Event
* *
* @return bool flag saying whether such a handler exists * @return bool flag saying whether such a handler exists
*/ */
public static function hasHandler(string $name, ?string $plugin = null): bool public static function hasHandler(string $name, ?string $plugin = null, string $ns = 'GNUsocial.'): bool
{ {
$listeners = self::$dispatcher->getListeners($name); $listeners = self::$dispatcher->getListeners($ns . $name);
if (isset($plugin)) { if (isset($plugin)) {
foreach ($listeners as $handler) { foreach ($listeners as $event_handler) {
if (get_class($handler[0]) == $plugin) { $class = (new ReflectionFunction((new ReflectionFunction($event_handler))->getStaticVariables()['handler']))->getClosureScopeClass()->getName();
if ($class === $plugin) {
return true; return true;
} }
} }
@ -155,8 +159,8 @@ abstract class Event
* @return array * @return array
* @return array * @return array
*/ */
public static function getHandlers(string $name): array public static function getHandlers(string $name, string $ns = 'GNUsocial.'): array
{ {
return self::$dispatcher->getListeners($name); return self::$dispatcher->getListeners($ns . $name);
} }
} }