2 Commits f8107c86c5 ... 818a31a690

Author SHA1 Message Date
  Hugo Sales 818a31a690 [UTIL] Provide static access to current request and utilities in Common 2 years ago
  Hugo Sales 9b862d6a26 [CORE] Throw more meaningfull error when method doesn't exist in Security and Entity 2 years ago
4 changed files with 36 additions and 6 deletions
  1. 1 1
      src/Core/Entity.php
  2. 13 4
      src/Core/GNUsocial.php
  3. 5 1
      src/Core/Security.php
  4. 17 0
      src/Util/Common.php

+ 1 - 1
src/Core/Entity.php

@@ -40,7 +40,7 @@ abstract class Entity
             $private_property_accessor = $private_property_accessor->bindTo($this, get_called_class());
             return $private_property_accessor($prop);
         }
-        throw new \Exception('Non existent method ' . get_called_class() . "::{$name} called with arguments: " . print_r($arguments, true));
+        throw new \BadMethodCallException('Non existent method ' . get_called_class() . "::{$name} called with arguments: " . print_r($arguments, true));
     }
 
     /**

+ 13 - 4
src/Core/GNUsocial.php

@@ -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()
     {

+ 5 - 1
src/Core/Security.php

@@ -49,7 +49,11 @@ abstract class Security
         if (method_exists(self::$security, $name)) {
             return self::$security->{$name}(...$args);
         } else {
-            return self::$sanitizer->{$name}(...$args);
+            if (method_exists(self::$sanitizer, $name)) {
+                return self::$sanitizer->{$name}(...$args);
+            } else {
+                throw new \BadMethodCallException("Method Security::{$name} doesn't exist");
+            }
         }
     }
 }

+ 17 - 0
src/Util/Common.php

@@ -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
      */