vendor/sylius/resource-bundle/src/Bundle/Storage/CookieStorage.php line 59

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Storage;
  12. use Sylius\Component\Resource\Storage\StorageInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\ParameterBag;
  16. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  17. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. final class CookieStorage implements StorageInterfaceEventSubscriberInterface
  20. {
  21.     /** @var ParameterBag */
  22.     private $requestCookies;
  23.     /** @var ParameterBag */
  24.     private $responseCookies;
  25.     public function __construct()
  26.     {
  27.         $this->requestCookies = new ParameterBag();
  28.         $this->responseCookies = new ParameterBag();
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             KernelEvents::REQUEST => [['onKernelRequest'1024]],
  37.             KernelEvents::RESPONSE => [['onKernelResponse', -1024]],
  38.         ];
  39.     }
  40.     public function onKernelRequest(GetResponseEvent $event): void
  41.     {
  42.         if (!$event->isMasterRequest()) {
  43.             return;
  44.         }
  45.         $this->requestCookies = new ParameterBag($event->getRequest()->cookies->all());
  46.         $this->responseCookies = new ParameterBag();
  47.     }
  48.     public function onKernelResponse(FilterResponseEvent $event): void
  49.     {
  50.         if (!$event->isMasterRequest()) {
  51.             return;
  52.         }
  53.         $response $event->getResponse();
  54.         foreach ($this->responseCookies as $name => $value) {
  55.             $response->headers->setCookie(new Cookie((string) $name$value));
  56.         }
  57.         $this->requestCookies = new ParameterBag();
  58.         $this->responseCookies = new ParameterBag();
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function has(string $name): bool
  64.     {
  65.         return !in_array($this->get($name), [''null], true);
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function get(string $name$default null)
  71.     {
  72.         return $this->responseCookies->get($name$this->requestCookies->get($name$default));
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function set(string $name$value): void
  78.     {
  79.         $this->responseCookies->set($name$value);
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function remove(string $name): void
  85.     {
  86.         $this->set($namenull);
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function all(): array
  92.     {
  93.         return array_merge($this->responseCookies->all(), $this->requestCookies->all());
  94.     }
  95. }