vendor/php-flasher/flasher-symfony/EventListener/SessionListener.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the PHPFlasher package.
  4.  * (c) Younes KHOUBZA <younes.khoubza@gmail.com>
  5.  */
  6. namespace Flasher\Symfony\EventListener;
  7. use Flasher\Prime\FlasherInterface;
  8. use Flasher\Prime\Response\Presenter\HtmlPresenter;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. final class SessionListener
  12. {
  13.     /**
  14.      * @var FlasherInterface
  15.      */
  16.     private $flasher;
  17.     /**
  18.      * @var array<string, string>
  19.      */
  20.     private $mapping;
  21.     /**
  22.      * @param array<string, string[]> $mapping
  23.      */
  24.     public function __construct(FlasherInterface $flasher, array $mapping = array())
  25.     {
  26.         $this->flasher $flasher;
  27.         $this->mapping $this->flatMapping($mapping);
  28.     }
  29.     /**
  30.      * @param ResponseEvent $event
  31.      *
  32.      * @return void
  33.      */
  34.     public function onKernelResponse($event)
  35.     {
  36.         $request $event->getRequest();
  37.         if (!$this->isMainRequest($event) || $request->isXmlHttpRequest() || !$request->hasSession()) {
  38.             return;
  39.         }
  40.         $response $event->getResponse();
  41.         $content $response->getContent() ?: '';
  42.         $insertPlaceHolder HtmlPresenter::FLASHER_FLASH_BAG_PLACE_HOLDER;
  43.         $insertPosition strripos($content$insertPlaceHolder);
  44.         if (false === $insertPosition) {
  45.             return;
  46.         }
  47.         $readyToRender false;
  48.         /** @var Session $session */
  49.         $session $request->getSession();
  50.         foreach ($session->getFlashBag()->all() as $type => $messages) {
  51.             if (!isset($this->mapping[$type])) {
  52.                 continue;
  53.             }
  54.             foreach ($messages as $message) {
  55.                 $this->flasher->addFlash($this->mapping[$type], $message);
  56.                 $readyToRender true;
  57.             }
  58.         }
  59.         if (false === $readyToRender) {
  60.             return;
  61.         }
  62.         $htmlResponse $this->flasher->render(array(), 'html', array('envelopes_only' => true));
  63.         if (empty($htmlResponse)) {
  64.             return;
  65.         }
  66.         $content substr($content0$insertPosition).$htmlResponse.substr($content$insertPosition + \strlen($insertPlaceHolder));
  67.         $response->setContent($content);
  68.     }
  69.     /**
  70.      * @param array<string, string[]> $mapping
  71.      *
  72.      * @return array<string, string>
  73.      */
  74.     private function flatMapping(array $mapping)
  75.     {
  76.         $flatMapping = array();
  77.         foreach ($mapping as $type => $aliases) {
  78.             foreach ($aliases as $alias) {
  79.                 $flatMapping[$alias] = $type;
  80.             }
  81.         }
  82.         return $flatMapping;
  83.     }
  84.     /**
  85.      * @param ResponseEvent $event
  86.      *
  87.      * @return bool
  88.      */
  89.     private function isMainRequest($event)
  90.     {
  91.         if (method_exists($event'isMainRequest')) {
  92.             return $event->isMainRequest();
  93.         }
  94.         if (method_exists($event'isMasterRequest')) { // @phpstan-ignore-line
  95.             return $event->isMasterRequest();
  96.         }
  97.         return === $event->getRequestType();
  98.     }
  99. }