vendor/lnb/shopware6-lnb-rule-condition/src/Context/RequestScope.php line 11

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Lnb\Shopware6\LnbRuleCondition\Context;
  4. use Shopware\Core\Framework\Struct\Struct;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. class RequestScope extends Struct implements \Serializable
  8. {
  9.     protected ?Request $request null;
  10.     public function __construct(RequestStack $requestStack)
  11.     {
  12.         $this->request $requestStack->getCurrentRequest();
  13.     }
  14.     public function isStorefront(): bool
  15.     {
  16.         if (!$this->request) {
  17.             return false;
  18.         }
  19.         return str_starts_with($this->request->attributes->get('_route'''), 'frontend.');
  20.     }
  21.     public function serialize(): string
  22.     {
  23.         if (!$this->request) {
  24.             return '';
  25.         }
  26.         return serialize(
  27.             [
  28.                 'query' => $this->request->query->all(),
  29.                 'request' => $this->request->request->all(),
  30.                 'attributes' => $this->request->attributes->all(),
  31.                 'cookies' => $this->request->cookies->all(),
  32.                 'files' => $this->request->files->all(),
  33.                 'server' => $this->request->server->all(),
  34.                 'content' => $this->request->getContent(),
  35.             ]
  36.         );
  37.     }
  38.     public function unserialize($data): void
  39.     {
  40.         if ($data === '') {
  41.             return;
  42.         }
  43.         $unserialized unserialize($data);
  44.         if (!$unserialized ||
  45.             !array_key_exists('query'$unserialized) ||
  46.             !array_key_exists('request'$unserialized) ||
  47.             !array_key_exists('attributes'$unserialized) ||
  48.             !array_key_exists('cookies'$unserialized) ||
  49.             !array_key_exists('files'$unserialized) ||
  50.             !array_key_exists('server'$unserialized) ||
  51.             !array_key_exists('content'$unserialized)
  52.         ) {
  53.             return;
  54.         }
  55.         $this->request = new Request(
  56.             $unserialized['query'] ?? [],
  57.             $unserialized['request'] ?? [],
  58.             $unserialized['attributes'] ?? [],
  59.             $unserialized['cookies'] ?? [],
  60.             $unserialized['files'] ?? [],
  61.             $unserialized['server'] ?? [],
  62.             $unserialized['content'] ?? null
  63.         );
  64.     }
  65.     public function getRequest(): ?Request
  66.     {
  67.         return $this->request;
  68.     }
  69. }