Skip to content

Remove zend json from customer data #10259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 32 additions & 5 deletions app/code/Magento/Customer/Block/CustomerScopeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
private $storeManager;

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

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Serialize\Serializer\Json $serializer,
array $data = []
) {
parent::__construct($context, $data);
$this->storeManager = $context->getStoreManager();
$this->jsonEncoder = $jsonEncoder;
$this->serializer = $serializer;
}

/**
Expand All @@ -50,4 +50,31 @@ public function getWebsiteId()
{
return (int)$this->_storeManager->getStore()->getWebsiteId();
}

/**
* Get the invalidation rules json encoded
*
* @return bool|string
* @throws \InvalidArgumentException
*/
public function getSerializedInvalidationRules()
{
return $this->serializer->serialize(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think serialization in block is a good idea. It is better to just return raw data and JSON (or whatever else) encode in template.

Due to such get...Json methods in Magento 1 some extensions had to decode string and then re-encode just to modify array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, generally I agree with you here. I considered putting in one function to cover the serialization of the data. And one to cover the getting of the data. This would then allow 3rd party extensions to manipulate the data. Currently with the data stuck in the template it could not be changed easily anyway. I will have a look around for other examples of code like this.

[
'*' => [
'Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => $this->getWebsiteId(),
]
]
]
]
]
],
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
/** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
private $scopeConfigMock;

/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
private $encoderMock;
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
private $serializerMock;

protected function setUp()
{
Expand All @@ -41,7 +41,7 @@ protected function setUp()
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMock();

$this->encoderMock = $this->getMockBuilder(EncoderInterface::class)
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
->getMock();

$this->contextMock->expects($this->exactly(2))
Expand All @@ -54,7 +54,7 @@ protected function setUp()

$this->model = new CustomerScopeData(
$this->contextMock,
$this->encoderMock,
$this->serializerMock,
[]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@
<script type="text/x-magento-init">
<?php
/* @noEscape */
echo \Zend_Json::encode([
'*' => ['Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => $block->getWebsiteId(),
]
]
]
]
]],
]);
?>
echo $block->getSerializedInvalidationRules();
?>
</script>