Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/code/Magento/Checkout/Block/Onepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Onepage extends \Magento\Framework\View\Element\Template
protected $layoutProcessors;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
* @var \Magento\Framework\Serialize\Serializer\JsonHexTag
*/
private $serializer;

Expand All @@ -48,7 +48,7 @@ class Onepage extends \Magento\Framework\View\Element\Template
* @param \Magento\Checkout\Model\CompositeConfigProvider $configProvider
* @param array $layoutProcessors
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @param \Magento\Framework\Serialize\Serializer\JsonHexTag|null $serializer
* @throws \RuntimeException
*/
public function __construct(
Expand All @@ -57,7 +57,7 @@ public function __construct(
\Magento\Checkout\Model\CompositeConfigProvider $configProvider,
array $layoutProcessors = [],
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
\Magento\Framework\Serialize\Serializer\JsonHexTag $serializer = null
) {
parent::__construct($context, $data);
$this->formKey = $formKey;
Expand All @@ -66,7 +66,7 @@ public function __construct(
$this->configProvider = $configProvider;
$this->layoutProcessors = $layoutProcessors;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
->get(\Magento\Framework\Serialize\Serializer\JsonHexTag::class);
}

/**
Expand All @@ -78,7 +78,7 @@ public function getJsLayout()
$this->jsLayout = $processor->process($this->jsLayout);
}

return json_encode($this->jsLayout, JSON_HEX_TAG);
return $this->serializer->serialize($this->jsLayout);
}

/**
Expand Down Expand Up @@ -120,6 +120,6 @@ public function getBaseUrl()
*/
public function getSerializedCheckoutConfig()
{
return json_encode($this->getCheckoutConfig(), JSON_HEX_TAG);
return $this->serializer->serialize($this->getCheckoutConfig());
}
}
8 changes: 5 additions & 3 deletions app/code/Magento/Checkout/Test/Unit/Block/OnepageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OnepageTest extends \PHPUnit\Framework\TestCase
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;
private $serializerMock;

protected function setUp()
{
Expand All @@ -49,15 +49,15 @@ protected function setUp()
\Magento\Checkout\Block\Checkout\LayoutProcessorInterface::class
);

$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
$this->serializerMock = $this->createMock(\Magento\Framework\Serialize\Serializer\JsonHexTag::class);

$this->model = new \Magento\Checkout\Block\Onepage(
$contextMock,
$this->formKeyMock,
$this->configProviderMock,
[$this->layoutProcessorMock],
[],
$this->serializer
$this->serializerMock
);
}

Expand Down Expand Up @@ -93,6 +93,7 @@ public function testGetJsLayout()
$processedLayout = ['layout' => ['processed' => true]];
$jsonLayout = '{"layout":{"processed":true}}';
$this->layoutProcessorMock->expects($this->once())->method('process')->with([])->willReturn($processedLayout);
$this->serializerMock->expects($this->once())->method('serialize')->willReturn($jsonLayout);

$this->assertEquals($jsonLayout, $this->model->getJsLayout());
}
Expand All @@ -101,6 +102,7 @@ public function testGetSerializedCheckoutConfig()
{
$checkoutConfig = ['checkout', 'config'];
$this->configProviderMock->expects($this->once())->method('getConfig')->willReturn($checkoutConfig);
$this->serializerMock->expects($this->once())->method('serialize')->willReturn(json_encode($checkoutConfig));

$this->assertEquals(json_encode($checkoutConfig), $this->model->getSerializedCheckoutConfig());
}
Expand Down
14 changes: 12 additions & 2 deletions app/code/Magento/Ui/TemplateEngine/Xhtml/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Ui\TemplateEngine\Xhtml;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\JsonHexTag;
use Magento\Framework\View\Layout\Generator\Structure;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\TemplateEngine\Xhtml\Template;
Expand Down Expand Up @@ -42,25 +44,33 @@ class Result implements ResultInterface
*/
protected $logger;

/**
* @var JsonHexTag
*/
private $jsonSerializer;

/**
* @param Template $template
* @param CompilerInterface $compiler
* @param UiComponentInterface $component
* @param Structure $structure
* @param LoggerInterface $logger
* @param JsonHexTag $jsonSerializer
*/
public function __construct(
Template $template,
CompilerInterface $compiler,
UiComponentInterface $component,
Structure $structure,
LoggerInterface $logger
LoggerInterface $logger,
JsonHexTag $jsonSerializer = null
) {
$this->template = $template;
$this->compiler = $compiler;
$this->component = $component;
$this->structure = $structure;
$this->logger = $logger;
$this->jsonSerializer = $jsonSerializer ?? ObjectManager::getInstance()->get(JsonHexTag::class);
}

/**
Expand All @@ -81,7 +91,7 @@ public function getDocumentElement()
public function appendLayoutConfiguration()
{
$layoutConfiguration = $this->wrapContent(
json_encode($this->structure->generate($this->component), JSON_HEX_TAG)
$this->jsonSerializer->serialize($this->structure->generate($this->component))
);
$this->template->append($layoutConfiguration);
}
Expand Down
33 changes: 33 additions & 0 deletions lib/internal/Magento/Framework/Serialize/Serializer/JsonHexTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\Serialize\Serializer;

use Magento\Framework\Serialize\SerializerInterface;

/**
* Serialize data to JSON, unserialize JSON encoded data
*
* @api
* @since 100.2.0
*/
class JsonHexTag extends Json implements SerializerInterface
{
/**
* {@inheritDoc}
* @since 100.2.0
*/
public function serialize($data): string
{
$result = json_encode($data, JSON_HEX_TAG);
if (false === $result) {
throw new \InvalidArgumentException('Unable to serialize value.');
}
return $result;
}
}