[UTIL] Provide static access to current request and utilities in Common

This commit is contained in:
Hugo Sales 2021-05-12 15:45:00 +00:00
parent 9198797aea
commit 86400ce815
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 30 additions and 4 deletions

View File

@ -56,6 +56,8 @@ use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@ -87,6 +89,7 @@ class GNUsocial implements EventSubscriberInterface
protected SanitizerInterface $sanitizer;
protected ContainerBagInterface $config;
protected \Twig\Environment $twig;
protected Request $request;
/**
* Symfony dependency injection gives us access to these services
@ -105,7 +108,8 @@ class GNUsocial implements EventSubscriberInterface
HttpClientInterface $cl,
SanitizerInterface $san,
ContainerBagInterface $conf,
\Twig\Environment $twig)
\Twig\Environment $twig,
RequestStack $request_stack)
{
$this->logger = $logger;
$this->translator = $trans;
@ -122,6 +126,7 @@ class GNUsocial implements EventSubscriberInterface
$this->saniter = $san;
$this->config = $conf;
$this->twig = $twig;
$this->request = $request_stack->getCurrentRequest();
$this->initialize();
}
@ -135,6 +140,7 @@ class GNUsocial implements EventSubscriberInterface
{
if (!$this->initialized) {
Common::setupConfig($this->config);
Common::setRequest($this->request);
Log::setLogger($this->logger);
Event::setDispatcher($this->event_dispatcher);
I18n::setTranslator($this->translator);
@ -169,11 +175,11 @@ class GNUsocial implements EventSubscriberInterface
public function onKernelRequest(RequestEvent $event,
string $event_name): RequestEvent
{
$request = $event->getRequest();
$this->request = $event->getRequest();
// Save the target path, so we can redirect back after logging in
if (!(!$event->isMasterRequest() || $request->isXmlHttpRequest() || in_array($request->attributes->get('_route'), ['login', 'register', 'logout']))) {
$this->saveTargetPath($this->session, 'main', $request->getBaseUrl());
if (!(!$event->isMasterRequest() || $this->request->isXmlHttpRequest() || Common::isRoute(['login', 'register', 'logout']))) {
$this->saveTargetPath($this->session, 'main', $this->request->getBaseUrl());
}
$this->initialize();
@ -188,6 +194,7 @@ class GNUsocial implements EventSubscriberInterface
* @param EventDispatcherInterface $event_dispatcher
*
* @return ConsoleCommandEvent
* @codeCoverageIgnore
*/
public function onCommand(ConsoleCommandEvent $event,
string $event_name): ConsoleCommandEvent
@ -199,6 +206,8 @@ class GNUsocial implements EventSubscriberInterface
/**
* Tell Symfony which events we want to listen to, which Symfony detects and autowires
* due to this implementing the `EventSubscriberInterface`
*
* @codeCoverageIgnore
*/
public static function getSubscribedEvents()
{

View File

@ -39,6 +39,7 @@ use App\Entity\LocalUser;
use App\Util\Exception\NoLoggedInUser;
use Functional as F;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Yaml;
@ -52,6 +53,22 @@ abstract class Common
self::$defaults = $config->get('gnusocial_defaults');
}
private static ?Request $request = null;
public static function setRequest(Request $req)
{
self::$request = $req;
}
public static function route()
{
return self::$request->attributes->get('_route');
}
public static function isRoute(string | array $routes)
{
return in_array(self::route(), is_array($routes) ? $routes : [$routes]);
}
/**
* Access sysadmin's configuration preferences for GNU social
*/