Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileManager.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Translation\Model\Inline\File as TranslationFile;
11 
16 {
20  const TRANSLATION_CONFIG_FILE_NAME = 'Magento_Translation/js/i18n-config.js';
21 
25  private $assetRepo;
26 
30  private $directoryList;
31 
35  private $driverFile;
36 
40  private $translationFile;
41 
48  public function __construct(
49  \Magento\Framework\View\Asset\Repository $assetRepo,
50  \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
51  \Magento\Framework\Filesystem\Driver\File $driverFile,
52  \Magento\Translation\Model\Inline\File $translationFile = null
53  ) {
54  $this->assetRepo = $assetRepo;
55  $this->directoryList = $directoryList;
56  $this->driverFile = $driverFile;
57  $this->translationFile = $translationFile ?: ObjectManager::getInstance()->get(TranslationFile::class);
58  }
59 
65  public function createTranslateConfigAsset()
66  {
67  return $this->assetRepo->createArbitrary(
68  $this->assetRepo->getStaticViewFileContext()->getPath() . '/' . self::TRANSLATION_CONFIG_FILE_NAME,
69  ''
70  );
71  }
72 
78  public function getTranslationFileTimestamp()
79  {
80  $translationFilePath = $this->getTranslationFileFullPath();
81  if ($this->driverFile->isExists($translationFilePath)) {
82  $statArray = $this->driverFile->stat($translationFilePath);
83  if (array_key_exists('mtime', $statArray)) {
84  return $statArray['mtime'];
85  }
86  }
87  }
88 
92  protected function getTranslationFileFullPath()
93  {
94  return $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
95  \DIRECTORY_SEPARATOR .
96  $this->assetRepo->getStaticViewFileContext()->getPath() .
97  \DIRECTORY_SEPARATOR .
99  }
100 
104  public function getTranslationFilePath()
105  {
106  return $this->assetRepo->getStaticViewFileContext()->getPath();
107  }
108 
114  {
115  $translationDir = $this->directoryList->getPath(DirectoryList::STATIC_VIEW) .
116  \DIRECTORY_SEPARATOR .
117  $this->assetRepo->getStaticViewFileContext()->getPath();
118  if (!$this->driverFile->isExists($this->getTranslationFileFullPath())) {
119  $this->driverFile->createDirectory($translationDir);
120  }
121  $this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content);
122  }
123 
129  public function getTranslationFileVersion()
130  {
131  $translationFile = $this->getTranslationFileFullPath();
132  $translationFileHash = '';
133 
134  if ($this->driverFile->isExists($translationFile)) {
135  $translationFileHash = sha1_file($translationFile);
136  }
137 
138  return sha1($translationFileHash . $this->getTranslationFilePath());
139  }
140 }
__construct(\Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\App\Filesystem\DirectoryList $directoryList, \Magento\Framework\Filesystem\Driver\File $driverFile, \Magento\Translation\Model\Inline\File $translationFile=null)
Definition: FileManager.php:48