Skip to content
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

Remove zend json from customer data #10259

Merged

Conversation

dmanners
Copy link
Contributor

@dmanners dmanners commented Jul 17, 2017

Description

As both Zend_Json and \Magento\Framework\Json\EncoderInterface are now deprecated I have migrated the usage across the the accepted \Magento\Framework\Serialize\Serializer\Json class.

Fixed Issues (if relevant)

  1. Upgrade ZF components. Zend_Json #9236: Upgrade ZF components. Zend_Json

Manual testing scenarios

  1. ...
  2. ...

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

 - remove zend call and use the Magento Framework,
 - move work from template into app/code/Magento/Customer/Block/CustomerScopeData.php
…terface in the CustomerScopeData block,

 - Use \Magento\Framework\Serialize\Serializer\Json instead when dealing with serilization
 - Update test to mirror the change in the main class
@dmanners
Copy link
Contributor Author

Not 100% sure if this is the "best way" ™️ for replacing the deprecated class that is being injected in. Happy for feedback and change requests.

@ihor-sviziev
Copy link
Contributor

@dmanners I see that app/code/Magento/Customer/Block/CustomerScopeData.php file didn't exists in 2.1 releases, so I think it's ok that we can change file before release.

@dmanners
Copy link
Contributor Author

Ah @ihor-sviziev thanks for the information. I had not noticed it before.

@okorshenko
Copy link
Contributor

Hi @dmanners
Unfortunately, we can not add new methods to existing @api classes and change constructor signatures. develop branch is designed for 2.3 but this class was added in 2.2. so this will be BiC

@dmanners
Copy link
Contributor Author

Hi @okorshenko, thanks for the feedback. I have a few follow up questions then.

  1. If this is a new class in 2.2 can I simply pr against 2.2.0-preview or is it too later to get changes into that branch?
  2. Is there a plan/blog post out there on the "perfect ™️" approach for changing these sort of classes? This might be a simple example but I am sure this is not the first and wont be the last time someone wants to make a change to an @api tagged class.

Happy to here any options for making this change.

*/
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.

@orlangur
Copy link
Contributor

So all blocks should be @api annotated. This test checks that all blocks declared in layout files are public

Hm, this is something new I was not aware of...

@okorshenko as all blocks became @api annotated are you sure that we cannot add new public methods to them? Unlike adding some methods to interface it sounds a bit weird to me.

@dmanners,

approach for changing these sort of classes?

http://devdocs.magento.com/guides/v2.1/contributor-guide/backward-compatible-development/index.html more or less describes approach, if some cases are not covered yet we can extend it with time.

…C rules

 - keep the $jsonEncoder injection but remove the type,
 - mark the constructor as PHPMD.UnusedFormalParameter,
 - split out the config and the serilization to happen in two methods,
*/
public function getSerializedInvalidationRules()
{
return $this->serializer->serialize($this->getInvalidationRules());
Copy link
Contributor

Choose a reason for hiding this comment

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

Still... What are the advantages compared to echo json_encode($block->getInvalidationRules(); in template? As to me encoding is purely responsibility of template.

Another thing which just came to my mind: do we have a recommendation to leave JS component declaration in templates? Thus they may be overridden more easily, without touching PHP classes. At least I was not able to find similar occurrence of /js/ or '*' in *.php.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about the leaving of the js declaration in the templates. Though to me this feels like config and to have to change a full template to change the config feels odd.

With regards to the json_encode in the template I dont agree here. I feel that we should either be using json_encode everywhere or wrap it's usage across the system, which would include templates. This way if a mass change is needed it is easier to manage and roll out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have no idea which kind of mass change could it be :D

Ok, I see your point, you want to encapsulate serializer logic in block class and don't make template aware of it (while it does escapeHtml and other similar things). There is nothing to discuss here, let's wait for the feedback from Oleksii and then maybe put a common recommendation for such situation somewhere to keep the code consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah something like escapeHtml would be a nice option here also thought about that. It's above this PR but I think some "common" way to do this through templates would be amazing.

@okorshenko
Copy link
Contributor

Hi @dmanners and @orlangur
Sorry for confusing.
We can not add public methods to @api classes in patch releases, but we can do that in minor. So 2.3 is a minor release and we can add this method. The issue was with constructor
Here is SVC report

  • MAJOR - Magento\Customer\Block\CustomerScopeData::__construct - [public] Method parameter changed
  • MINOR - Magento\Customer\Block\CustomerScopeData::getSerializedInvalidationRules - [public] Method has been added

So the issue was with the constructor.

*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
array $data = []
$jsonEncoder,
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you removed interface by mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, will add that back in.

@magento-team magento-team merged commit 31764a5 into magento:develop Jul 19, 2017
magento-team pushed a commit that referenced this pull request Jul 19, 2017
 - moved component configuration to the template file
@dmanners dmanners deleted the remove-zend-json-from-customer-data branch July 26, 2017 12:00
magento-team pushed a commit that referenced this pull request Oct 3, 2017
- rework fix for 2.2.1 without minor change
@Ctucker9233
Copy link

@magento-team What version of 2.2 will have this included? As of 2.2.7 it doesn't appear to be in there.

@orlangur
Copy link
Contributor

@Ctucker9233 you can check corresponding 2.2 commits: http://prntscr.com/mz7vk2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants