vendor/troopers/alertify-bundle/Troopers/AlertifyBundle/EventListener/AlertifyListener.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Troopers\AlertifyBundle\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Troopers\AlertifyBundle\Exception\IncompatibleStatusCodeException;
  20. use Troopers\AlertifyBundle\Handler\AlertifySessionHandler;
  21. use Twig\Environment;
  22. /**
  23.  * AlertifyListener append the alertify views to the response.
  24.  *
  25.  * @author Leny Bernard <leny@troopers.email>
  26.  */
  27. class AlertifyListener implements EventSubscriberInterface
  28. {
  29.     protected $alertifySessionHandler;
  30.     /**
  31.      * @var Session
  32.      */
  33.     private $session;
  34.     private $twig;
  35.     /**
  36.      * AlertifyListener constructor.
  37.      *
  38.      * @param Session                $session
  39.      * @param AlertifySessionHandler $alertifySessionHandler
  40.      */
  41.     public function __construct(Environment $twigSession $sessionAlertifySessionHandler $alertifySessionHandler)
  42.     {
  43.         $this->twig $twig;
  44.         $this->session $session;
  45.         $this->alertifySessionHandler $alertifySessionHandler;
  46.     }
  47.     /**
  48.      * @param ResponseEvent|FilterResponseEvent $event
  49.      */
  50.     public function onKernelResponse($event)
  51.     {
  52.         $response $event->getResponse();
  53.         $request $event->getRequest();
  54.         $this->injectAlertify($response$request);
  55.     }
  56.     /**
  57.      * Injects the alertify view into the given Response just before </body> tag.
  58.      *
  59.      * @param Response $response
  60.      * @param Request  $request
  61.      *
  62.      * @throws IncompatibleStatusCodeException
  63.      */
  64.     protected function injectAlertify(Response $responseRequest $request)
  65.     {
  66.         $content $response->getContent();
  67.         $endBodyPos strripos($content'</body>');
  68.         $hasBody false !== $endBodyPos;
  69.         $hasMetaRefresh false !== strripos($content'http-equiv="refresh"');
  70.         $forceInject $response->headers->get('X-Inject-Alertify'false);
  71.         $isRedirectResponse $response instanceof RedirectResponse;
  72.         $hasPreviousSession $request->hasPreviousSession();
  73.         if ($hasBody && $hasPreviousSession && !$hasMetaRefresh && !$isRedirectResponse || $forceInject) {
  74.             if ($response->getStatusCode() === 204) {
  75.                 throw new IncompatibleStatusCodeException();
  76.             }
  77.             $alertify $this->alertifySessionHandler->handle($this->session$this->twig);
  78.             if ($endBodyPos) {
  79.                 $content substr($content0$endBodyPos).$alertify.substr($content$endBodyPos);
  80.             } else {
  81.                 $content .= $alertify;
  82.             }
  83.             $response->setContent($content);
  84.         }
  85.     }
  86.     public static function getSubscribedEvents()
  87.     {
  88.         return [
  89.             KernelEvents::RESPONSE => ['onKernelResponse'],
  90.         ];
  91.     }
  92. }