diff --git a/src/Util/GNUsocial.php b/src/Util/GNUsocial.php index 7dbac8f8e5..b90b45beb9 100644 --- a/src/Util/GNUsocial.php +++ b/src/Util/GNUsocial.php @@ -55,6 +55,8 @@ namespace App\Util; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\Console\Event\ConsoleCommandEvent; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -75,20 +77,35 @@ class GNUsocial implements EventSubscriberInterface $this->translator = $translator; } - public function onKernelRequest(RequestEvent $event, - string $event_name, - $event_dispatcher): RequestEvent + public function register(EventDispatcherInterface $event_dispatcher): void { Log::setLogger($this->logger); GSEvent::setDispatcher($event_dispatcher); I18n::setTranslator($this->translator); ExtensionManager::loadExtensions(); + } + public function onKernelRequest(RequestEvent $event, + string $event_name, + EventDispatcherInterface $event_dispatcher): RequestEvent + { + $this->register($event_dispatcher); + return $event; + } + + public function onCommand(ConsoleCommandEvent $event, + string $event_name, + EventDispatcherInterface $event_dispatcher): ConsoleCommandEvent + { + $this->register($event_dispatcher); return $event; } public static function getSubscribedEvents() { - return [KernelEvents::REQUEST => 'onKernelRequest']; + return [ + KernelEvents::REQUEST => 'onKernelRequest', + 'console.command' => 'onCommand', + ]; } } diff --git a/src/Util/GSEvent.php b/src/Util/GSEvent.php index eb4ded8706..76b018904b 100644 --- a/src/Util/GSEvent.php +++ b/src/Util/GSEvent.php @@ -61,10 +61,13 @@ abstract class GSEvent * * @return void */ - public static function addHandler(string $name, callable $handler, int $priority = 0): void + public static function addHandler(string $name, + callable $handler, + int $priority = 0, + string $ns = 'GNUsocial/'): void { self::$dispatcher->addListener( - $name, + $ns . $name, function ($event, $event_name, $dispatcher) use ($handler) { // Old style of events (preferred) if ($event instanceof GenericEvent) { @@ -94,14 +97,15 @@ abstract class GSEvent * * @param string $name Name of the event that's happening * @param array $args Arguments for handlers + * @param string $ns Namspace for the event * * @return bool flag saying whether to continue processing, based * on results of handlers. */ - public static function handle(string $name, array $args = []): bool + public static function handle(string $name, array $args = [], string $ns = 'GNUsocial/'): bool { return !(self::$dispatcher->dispatch( - new GenericEvent($name, $args), + new GenericEvent($ns . $name, $args), $name)->isPropagationStopped()); }