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

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\RequestEvent;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  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.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => [['onKernelRequest'1024]],
  34.             KernelEvents::RESPONSE => [['onKernelResponse', -1024]],
  35.         ];
  36.     }
  37.     public function onKernelRequest(RequestEvent $event): void
  38.     {
  39.         if (!$event->isMasterRequest()) {
  40.             return;
  41.         }
  42.         $this->requestCookies = new ParameterBag($event->getRequest()->cookies->all());
  43.         $this->responseCookies = new ParameterBag();
  44.     }
  45.     public function onKernelResponse(ResponseEvent $event): void
  46.     {
  47.         if (!$event->isMasterRequest()) {
  48.             return;
  49.         }
  50.         $response $event->getResponse();
  51.         foreach ($this->responseCookies as $name => $value) {
  52.             $response->headers->setCookie(new Cookie((string) $name$value));
  53.         }
  54.         $this->requestCookies = new ParameterBag();
  55.         $this->responseCookies = new ParameterBag();
  56.     }
  57.     public function has(string $name): bool
  58.     {
  59.         return !in_array($this->get($name), [''null], true);
  60.     }
  61.     public function get(string $name$default null)
  62.     {
  63.         return $this->responseCookies->get($name$this->requestCookies->get($name$default));
  64.     }
  65.     public function set(string $name$value): void
  66.     {
  67.         $this->responseCookies->set($name$value);
  68.     }
  69.     public function remove(string $name): void
  70.     {
  71.         $this->set($namenull);
  72.     }
  73.     public function all(): array
  74.     {
  75.         return array_merge($this->responseCookies->all(), $this->requestCookies->all());
  76.     }
  77. }