FileManager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Translation\Model\Inline\File as TranslationFile;
  10. /**
  11. * A service for handling Translation config files
  12. */
  13. class FileManager
  14. {
  15. /**
  16. * File name of RequireJs inline translation config
  17. */
  18. const TRANSLATION_CONFIG_FILE_NAME = 'Magento_Translation/js/i18n-config.js';
  19. /**
  20. * @var \Magento\Framework\View\Asset\Repository
  21. */
  22. private $assetRepo;
  23. /**
  24. * @var \Magento\Framework\App\Filesystem\DirectoryList
  25. */
  26. private $directoryList;
  27. /**
  28. * @var \Magento\Framework\Filesystem\Driver\File
  29. */
  30. private $driverFile;
  31. /**
  32. * @var TranslationFile
  33. */
  34. private $translationFile;
  35. /**
  36. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  37. * @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList
  38. * @param \Magento\Framework\Filesystem\Driver\File $driverFile
  39. * @param TranslationFile $translationFile
  40. */
  41. public function __construct(
  42. \Magento\Framework\View\Asset\Repository $assetRepo,
  43. \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
  44. \Magento\Framework\Filesystem\Driver\File $driverFile,
  45. \Magento\Translation\Model\Inline\File $translationFile = null
  46. ) {
  47. $this->assetRepo = $assetRepo;
  48. $this->directoryList = $directoryList;
  49. $this->driverFile = $driverFile;
  50. $this->translationFile = $translationFile ?: ObjectManager::getInstance()->get(TranslationFile::class);
  51. }
  52. /**
  53. * Create a view asset representing the requirejs config.config property for inline translation
  54. *
  55. * @return \Magento\Framework\View\Asset\File
  56. */
  57. public function createTranslateConfigAsset()
  58. {
  59. return $this->assetRepo->createArbitrary(
  60. $this->assetRepo->getStaticViewFileContext()->getPath() . '/' . self::TRANSLATION_CONFIG_FILE_NAME,
  61. ''
  62. );
  63. }
  64. /**
  65. * gets current js-translation.json timestamp
  66. *
  67. * @return string|void
  68. */
  69. public function getTranslationFileTimestamp()
  70. {
  71. $translationFilePath = $this->getTranslationFileFullPath();
  72. if ($this->driverFile->isExists($translationFilePath)) {
  73. $statArray = $this->driverFile->stat($translationFilePath);
  74. if (array_key_exists('mtime', $statArray)) {
  75. return $statArray['mtime'];
  76. }
  77. }
  78. }
  79. /**
  80. * @return string
  81. */
  82. protected function getTranslationFileFullPath()
  83. {
  84. return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
  85. \DIRECTORY_SEPARATOR .
  86. $this->assetRepo->getStaticViewFileContext()->getPath() .
  87. \DIRECTORY_SEPARATOR .
  88. Js\Config::DICTIONARY_FILE_NAME;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getTranslationFilePath()
  94. {
  95. return $this->assetRepo->getStaticViewFileContext()->getPath();
  96. }
  97. /**
  98. * @param string $content
  99. * @return void
  100. */
  101. public function updateTranslationFileContent($content)
  102. {
  103. $translationDir = $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
  104. \DIRECTORY_SEPARATOR .
  105. $this->assetRepo->getStaticViewFileContext()->getPath();
  106. if (!$this->driverFile->isExists($this->getTranslationFileFullPath())) {
  107. $this->driverFile->createDirectory($translationDir);
  108. }
  109. $this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content);
  110. }
  111. /**
  112. * Calculate translation file version hash.
  113. *
  114. * @return string
  115. */
  116. public function getTranslationFileVersion()
  117. {
  118. $translationFile = $this->getTranslationFileFullPath();
  119. $translationFileHash = '';
  120. if ($this->driverFile->isExists($translationFile)) {
  121. $translationFileHash = sha1_file($translationFile);
  122. }
  123. return sha1($translationFileHash . $this->getTranslationFilePath());
  124. }
  125. }