vendor/coreshop/core-shop/src/CoreShop/Bundle/CoreBundle/Storage/CookieStorage.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * CoreShop
  5.  *
  6.  * This source file is available under two different licenses:
  7.  *  - GNU General Public License version 3 (GPLv3)
  8.  *  - CoreShop Commercial License (CCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.org)
  13.  * @license    https://www.coreshop.org/license     GPLv3 and CCL
  14.  *
  15.  */
  16. namespace CoreShop\Bundle\CoreBundle\Storage;
  17. use CoreShop\Component\Resource\Storage\StorageInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\Cookie;
  20. use Symfony\Component\HttpFoundation\ParameterBag;
  21. use Symfony\Component\HttpKernel\Event\RequestEvent;
  22. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. final class CookieStorage implements StorageInterfaceEventSubscriberInterface
  25. {
  26.     private ParameterBag $requestCookies;
  27.     private ParameterBag $responseCookies;
  28.     public function __construct(
  29.         ) {
  30.         $this->requestCookies = new ParameterBag();
  31.         $this->responseCookies = new ParameterBag();
  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(RequestEvent $event): void
  41.     {
  42.         if (!$event->isMainRequest()) {
  43.             return;
  44.         }
  45.         $this->requestCookies = new ParameterBag($event->getRequest()->cookies->all());
  46.         $this->responseCookies = new ParameterBag();
  47.     }
  48.     public function onKernelResponse(ResponseEvent $event): void
  49.     {
  50.         if (!$event->isMainRequest()) {
  51.             return;
  52.         }
  53.         $response $event->getResponse();
  54.         foreach ($this->responseCookies as $name => $value) {
  55.             $response->headers->setCookie(new Cookie($name, (string) $value));
  56.         }
  57.         $this->requestCookies = new ParameterBag();
  58.         $this->responseCookies = new ParameterBag();
  59.     }
  60.     public function has(string $name): bool
  61.     {
  62.         return !in_array($this->get($name), [''null], true);
  63.     }
  64.     public function get(string $namemixed $default null): mixed
  65.     {
  66.         return $this->responseCookies->get($name$this->requestCookies->get($name$default));
  67.     }
  68.     public function set(string $namemixed $value): void
  69.     {
  70.         $this->responseCookies->set($name$value);
  71.     }
  72.     public function remove(string $name): void
  73.     {
  74.         $this->set($namenull);
  75.     }
  76.     public function all(): array
  77.     {
  78.         return array_merge($this->responseCookies->all(), $this->requestCookies->all());
  79.     }
  80. }