vendor/php-flasher/flasher-symfony/Storage/SessionBag.php line 44

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\Storage;
  7. use Flasher\Prime\Storage\Bag\BagInterface;
  8. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Session as LegacySession;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. final class SessionBag implements BagInterface
  13. {
  14.     const ENVELOPES_NAMESPACE 'flasher::envelopes';
  15.     /**
  16.      * @var RequestStack|SessionInterface
  17.      */
  18.     private $session;
  19.     /**
  20.      * @var FallbackSession
  21.      */
  22.     private $fallbackSession;
  23.     /**
  24.      * @param RequestStack|SessionInterface $session
  25.      */
  26.     public function __construct($session)
  27.     {
  28.         $this->session $session;
  29.         $this->fallbackSession = new FallbackSession();
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function get()
  35.     {
  36.         return $this->session()->get(self::ENVELOPES_NAMESPACE, array()); // @phpstan-ignore-line
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function set(array $envelopes)
  42.     {
  43.         $this->session()->set(self::ENVELOPES_NAMESPACE$envelopes);
  44.     }
  45.     /**
  46.      * @return SessionInterface
  47.      */
  48.     private function session()
  49.     {
  50.         if ($this->session instanceof SessionInterface || $this->session instanceof LegacySession) { // @phpstan-ignore-line
  51.             return $this->session// @phpstan-ignore-line
  52.         }
  53.         try {
  54.             if (method_exists($this->session'getSession')) {
  55.                 $session $this->session->getSession();
  56.             } else {
  57.                 $session $this->session->getCurrentRequest()->getSession();
  58.             }
  59.             $isStateless $this->session->getCurrentRequest()->attributes->has('_stateless');
  60.             if (null !== $session && !$isStateless) {
  61.                 return $this->session $session;
  62.             }
  63.             return $this->fallbackSession;
  64.         } catch (SessionNotFoundException $e) {
  65.             return $this->fallbackSession;
  66.         }
  67.     }
  68. }