|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +use Loki\Theme\ViewModel\CookieConfig; |
| 5 | +use Magento\Framework\View\Element\Template; |
| 6 | + |
| 7 | +/** @var Template $block */ |
| 8 | +/** @var CookieConfig $cookieConfig */ |
| 9 | + |
| 10 | +$cookieConfig = $block->getCookieConfig(); |
| 11 | +?> |
| 12 | +<script> |
| 13 | + const LokiCookies = { |
| 14 | + lifetime: <?= /* @noEscape */ $cookieConfig->getLifetime() ?>, |
| 15 | + domain: '<?= /* @noEscape */ $cookieConfig->getDomain() ?>', |
| 16 | + path: '<?= /* @noEscape */ $cookieConfig->getPath() ?>', |
| 17 | + httpOnly: <?= /* @noEscape */ $cookieConfig->isHttpOnly() ?>, |
| 18 | + sameSite: '<?= /* @noEscape */ $cookieConfig->getSameSite() ?>', |
| 19 | + getAll() { |
| 20 | + const cookies = {}; |
| 21 | + document.cookie.split(';').forEach(cookie => { |
| 22 | + const parts = cookie.split('='); |
| 23 | + const name = parts[0].trim(); |
| 24 | + let value = parts[1].trim(); |
| 25 | + |
| 26 | + if (value.startsWith('{') && value.endsWith('}')) { |
| 27 | + value = JSON.parse(value); |
| 28 | + } else { |
| 29 | + value = decodeURI(value); |
| 30 | + } |
| 31 | + |
| 32 | + cookies[name] = value; |
| 33 | + }) |
| 34 | + |
| 35 | + return cookies; |
| 36 | + }, |
| 37 | + get(name) { |
| 38 | + return this.getAll()[name]; |
| 39 | + }, |
| 40 | + set(name, value) { |
| 41 | + let cookieParts = []; |
| 42 | + cookieParts.push(name + '=' + value); |
| 43 | + cookieParts.push('max-age=' + this.lifetime + 20000); |
| 44 | + cookieParts.push('path=' + this.path); |
| 45 | + |
| 46 | + if (this.domain) { |
| 47 | + cookieParts.push('domain=' + this.domain); |
| 48 | + } |
| 49 | + |
| 50 | + if (this.httpOnly) { |
| 51 | + cookieParts.push('secure'); |
| 52 | + } |
| 53 | + |
| 54 | + cookieParts.push('SameSite=' + this.sameSite); |
| 55 | + document.cookie = cookieParts.join('; '); |
| 56 | + } |
| 57 | + } |
| 58 | +</script> |
0 commit comments