src/EventSubscriber/ActionSubscriber.php line 23

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\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use App\Controller\BaseController;
  8. use App\Service\AppService;
  9. class ActionSubscriber implements EventSubscriberInterface
  10. {
  11.     private $_controller;
  12.     private $_appService;
  13.     
  14.     public function __construct(AppService $appService)
  15.     {
  16.         $this->_appService $appService;
  17.     }
  18.     
  19.     public function onKernelController(ControllerEvent $event)
  20.     {
  21.         $controller $event->getController();
  22.         /*
  23.          * $controller passed can be either a class or a Closure.
  24.          * This is not usual in Symfony but it may happen.
  25.          * If it is a class, it comes in array format
  26.          */
  27.         if (!is_array($controller))
  28.             return;
  29.         
  30.         $this->_appService->initApp();
  31.         if ($controller[0] instanceof BaseController)
  32.         {
  33.             $this->_controller $controller[0];
  34.             $this->_controller->beforeAction();
  35.         }
  36.     }
  37.     public function onKernelResponse(ResponseEvent $event)
  38.     {
  39.         if ($this->_controller)
  40.             $this->_controller->afterAction();
  41.     }
  42.     
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return array(
  46.             KernelEvents::CONTROLLER => 'onKernelController',
  47.             KernelEvents::RESPONSE => 'onKernelResponse'
  48.         );
  49.     }
  50. }