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

Improved calculating version hash for the js-translation.json file. #10378

Merged
merged 9 commits into from
Aug 18, 2017
8 changes: 8 additions & 0 deletions app/code/Magento/Translation/Block/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ public function getTranslationFilePath()
{
return $this->fileManager->getTranslationFilePath();
}

/**
* @return string
*/
public function getTranslationFileFullPath()
{
return $this->fileManager->getTranslationFileFullPath();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this breaks the backwards compatibility rules, since it adds a public method to an @api annotated class?

This change indeed breaks backwards compatibility rules, but with existing block design this case becomes quite complicated to solve properly without adding a method.

If we are going to violate the rule for the specific case – I would suggest implementing this in the "desirable state" way, so it would cover all the theoretical cases which may come up in the future.

Briefly, this could be a method with more general name, something like getTranslationFileVersion, which would proxy the call to the dedicated model, which calculates the value (doing so in the template is not a best practice anyway). This way it becomes a more usable "extension point" for third-party developers to possibly customize the behavior and improves ability to maintain this code for the core team.

}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Translation/Model/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getTranslationFileTimestamp()
/**
* @return string
*/
protected function getTranslationFileFullPath()
public function getTranslationFileFullPath()
Copy link
Contributor

Choose a reason for hiding this comment

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

Change of method visibility may break potential customization or extension which inherited from this class.

{
return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
\DIRECTORY_SEPARATOR .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$.initNamespaceStorage('mage-translation-file-version');
versionObj = $.localStorage.get('mage-translation-file-version');

<?php $version = sha1($block->getTranslationFileTimestamp() . $block->getTranslationFilePath()); ?>
<?php $version = sha1(sha1_file($block->getTranslationFileFullPath()) . $block->getTranslationFilePath()); ?>
Copy link
Contributor

Choose a reason for hiding this comment

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

Hashes calculation and operations on the filesystem should not be a responsibility of the presentation layer.


if (versionObj.version !== '<?= /* @noEscape */ $version ?>') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shoudn't we escape version there? Previously it was always sha1, but from now it uses raw value from public method, that could be overridden by plugin.
@ishakhsuvarov i think it should be escaped. what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds completely reasonable to me

Copy link
Contributor

Choose a reason for hiding this comment

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

@ishakhsuvarov pull request prepared #10904

dependencies.push(
Expand Down