vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/SessionHelper.php line 14

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 Symfony\Bundle\FrameworkBundle\Templating\Helper;
  11. @trigger_error('The '.SessionHelper::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.'E_USER_DEPRECATED);
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Templating\Helper\Helper;
  14. /**
  15.  * SessionHelper provides read-only access to the session attributes.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  20.  */
  21. class SessionHelper extends Helper
  22. {
  23.     protected $session;
  24.     protected $requestStack;
  25.     public function __construct(RequestStack $requestStack)
  26.     {
  27.         $this->requestStack $requestStack;
  28.     }
  29.     /**
  30.      * Returns an attribute.
  31.      *
  32.      * @param string $name    The attribute name
  33.      * @param mixed  $default The default value
  34.      *
  35.      * @return mixed
  36.      */
  37.     public function get($name$default null)
  38.     {
  39.         return $this->getSession()->get($name$default);
  40.     }
  41.     public function getFlash($name, array $default = [])
  42.     {
  43.         return $this->getSession()->getFlashBag()->get($name$default);
  44.     }
  45.     public function getFlashes()
  46.     {
  47.         return $this->getSession()->getFlashBag()->all();
  48.     }
  49.     public function hasFlash($name)
  50.     {
  51.         return $this->getSession()->getFlashBag()->has($name);
  52.     }
  53.     private function getSession()
  54.     {
  55.         if (null === $this->session) {
  56.             if (!$this->requestStack->getMasterRequest()) {
  57.                 throw new \LogicException('A Request must be available.');
  58.             }
  59.             $this->session $this->requestStack->getMasterRequest()->getSession();
  60.         }
  61.         return $this->session;
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getName()
  67.     {
  68.         return 'session';
  69.     }
  70. }