|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe. |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Persistent\Block\Header; |
| 9 | + |
| 10 | +use Magento\Framework\App\ObjectManager; |
| 11 | +use Magento\Framework\Serialize\Serializer\JsonHexTag; |
| 12 | +use Magento\Framework\Serialize\SerializerInterface; |
| 13 | +use Magento\Framework\View\Element\Template; |
| 14 | +use Magento\Framework\View\Element\Template\Context; |
| 15 | +use Magento\Persistent\Model\CheckoutConfigProvider; |
| 16 | + |
| 17 | +class RememberMeInit extends Template |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @param Context $context |
| 21 | + * @param array $data |
| 22 | + * @param SerializerInterface|null $serializer |
| 23 | + * @param CheckoutConfigProvider|null $checkoutConfigProvider |
| 24 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 25 | + */ |
| 26 | + public function __construct( |
| 27 | + Context $context, |
| 28 | + array $data = [], |
| 29 | + private ?SerializerInterface $serializer = null, |
| 30 | + private ?CheckoutConfigProvider $checkoutConfigProvider = null |
| 31 | + ) { |
| 32 | + parent::__construct($context, $data); |
| 33 | + $this->serializer = $serializer ?: ObjectManager::getInstance() |
| 34 | + ->get(JsonHexTag::class); |
| 35 | + $this->checkoutConfigProvider = $checkoutConfigProvider ?: ObjectManager::getInstance() |
| 36 | + ->get(CheckoutConfigProvider::class); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Retrieve serialized config. |
| 41 | + * |
| 42 | + * @return string|bool |
| 43 | + */ |
| 44 | + private function getSerializedCheckoutConfig(): string|bool |
| 45 | + { |
| 46 | + return $this->serializer->serialize($this->checkoutConfigProvider->getConfig()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritDoc |
| 51 | + */ |
| 52 | + public function toHtml() |
| 53 | + { |
| 54 | + $html = parent::toHtml(); |
| 55 | + $serializedConfig = $this->getSerializedCheckoutConfig(); |
| 56 | + $jsString = '<script type="text/x-magento-init">{"*": |
| 57 | + {"Magento_Persistent/js/remember-me-config": { |
| 58 | + "config": ' . $serializedConfig . ' |
| 59 | + }}}</script>'; |
| 60 | + |
| 61 | + $html .= $jsString; |
| 62 | + return $html; |
| 63 | + } |
| 64 | +} |
0 commit comments