src/EventSubscriber/BeforeControllerSubscriber.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class BeforeControllerSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @param ControllerEvent $event
  10.      */
  11.     public function onKernelController(ControllerEvent $event): void
  12.     {
  13.         $controller $event->getController();
  14.         /*
  15.          * $controller passed can be either a class or a Closure.
  16.          * This is not usual in Symfony but it may happen.
  17.          * If it is a class, it comes in array format
  18.          */
  19.         if (! \is_array($controller)) {
  20.             return;
  21.         }
  22.         if ($controller[0] instanceof BeforeControllerInterface) {
  23.             $controller[0]->before($event);
  24.         }
  25.     }
  26.     /**
  27.      * Returns an array of event names this subscriber wants to listen to.
  28.      *
  29.      * The array keys are event names and the value can be:
  30.      *
  31.      *  * The method name to call (priority defaults to 0)
  32.      *  * An array composed of the method name to call and the priority
  33.      *  * An array of arrays composed of the method names to call and respective
  34.      *    priorities, or 0 if unset
  35.      *
  36.      * For instance:
  37.      *
  38.      *  * ['eventName' => 'methodName']
  39.      *  * ['eventName' => ['methodName', $priority]]
  40.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  41.      *
  42.      * @return array|string[] The event names to listen to
  43.      */
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             KernelEvents::CONTROLLER => 'onKernelController',
  48.         ];
  49.     }
  50. }