[CORE][COMMAND] Register internal structures on command event

This commit is contained in:
Hugo Sales 2020-03-20 22:48:27 +00:00 committed by Hugo Sales
parent 6df658a987
commit b50909a335
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 29 additions and 8 deletions

View File

@ -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',
];
}
}

View File

@ -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());
}