Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileSystem.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\View;
7 
15 {
19  protected $_fileResolution;
20 
25 
30 
35 
40 
46  protected $_assetRepo;
47 
58  public function __construct(
59  \Magento\Framework\View\Design\FileResolution\Fallback\File $fallbackFile,
60  \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile $fallbackTemplateFile,
61  \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile $fallbackLocaleFile,
62  \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallbackStaticFile,
63  \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile $fallbackEmailTemplateFile,
64  \Magento\Framework\View\Asset\Repository $assetRepo
65  ) {
66  $this->_fileResolution = $fallbackFile;
67  $this->_templateFileResolution = $fallbackTemplateFile;
68  $this->_localeFileResolution = $fallbackLocaleFile;
69  $this->_staticFileResolution = $fallbackStaticFile;
70  $this->_emailTemplateFileResolution = $fallbackEmailTemplateFile;
71  $this->_assetRepo = $assetRepo;
72  }
73 
81  public function getFilename($fileId, array $params = [])
82  {
83  list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
84  $this->normalizePath($fileId)
85  );
86  if ($module) {
87  $params['module'] = $module;
88  }
89  $this->_assetRepo->updateDesignParams($params);
90  $file = $this->_fileResolution
91  ->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
92  return $file;
93  }
94 
102  public function getLocaleFileName($file, array $params = [])
103  {
104  $this->_assetRepo->updateDesignParams($params);
105  return $this->_localeFileResolution
106  ->getFile($params['area'], $params['themeModel'], $params['locale'], $file);
107  }
108 
116  public function getTemplateFileName($fileId, array $params = [])
117  {
118  list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
119  $this->normalizePath($fileId)
120  );
121  if ($module) {
122  $params['module'] = $module;
123  }
124  $this->_assetRepo->updateDesignParams($params);
125  return $this->_templateFileResolution
126  ->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
127  }
128 
136  public function getStaticFileName($fileId, array $params = [])
137  {
138  list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
139  $this->normalizePath($fileId)
140  );
141  if ($module) {
142  $params['module'] = $module;
143  }
144  $this->_assetRepo->updateDesignParams($params);
145  return $this->_staticFileResolution
146  ->getFile($params['area'], $params['themeModel'], $params['locale'], $filePath, $params['module']);
147  }
148 
157  public function getEmailTemplateFileName($fileId, array $params, $module)
158  {
159  $this->_assetRepo->updateDesignParams($params);
160  return $this->_emailTemplateFileResolution
161  ->getFile($params['area'], $params['themeModel'], $params['locale'], $fileId, $module);
162  }
163 
172  public static function normalizePath($path)
173  {
174  $parts = explode('/', $path);
175  $result = [];
176 
177  foreach ($parts as $part) {
178  if ('..' === $part) {
179  if (!count($result) || ($result[count($result) - 1] == '..')) {
180  $result[] = $part;
181  } else {
182  array_pop($result);
183  }
184  } elseif ('.' !== $part) {
185  $result[] = $part;
186  }
187  }
188  return implode('/', $result);
189  }
190 
209  public static function offsetPath($relatedPath, $path)
210  {
211  $relatedPath = self::normalizePath($relatedPath);
213  list($relatedPath, $path) = self::ltrimSamePart($relatedPath, $path);
214  $toDir = ltrim(dirname($path), '/');
215  if ($toDir == '.') {
216  $offset = '';
217  } else {
218  $offset = str_repeat('../', count(explode('/', $toDir)));
219  }
220  return rtrim($offset . dirname($relatedPath), '/');
221  }
222 
230  public static function getRelatedPath($relativeTo, $path)
231  {
232  return self::normalizePath(dirname($relativeTo) . '/' . $path);
233  }
234 
242  private static function ltrimSamePart($pathOne, $pathTwo)
243  {
244  $one = explode('/', $pathOne);
245  $two = explode('/', $pathTwo);
246  while (isset($one[0]) && isset($two[0]) && $one[0] == $two[0]) {
247  array_shift($one);
248  array_shift($two);
249  }
250  return [implode('/', $one), implode('/', $two)];
251  }
252 }
getFilename($fileId, array $params=[])
Definition: FileSystem.php:81
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\View\Design\FileResolution\Fallback\File $fallbackFile, \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile $fallbackTemplateFile, \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile $fallbackLocaleFile, \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallbackStaticFile, \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile $fallbackEmailTemplateFile, \Magento\Framework\View\Asset\Repository $assetRepo)
Definition: FileSystem.php:58
getTemplateFileName($fileId, array $params=[])
Definition: FileSystem.php:116
getEmailTemplateFileName($fileId, array $params, $module)
Definition: FileSystem.php:157
getStaticFileName($fileId, array $params=[])
Definition: FileSystem.php:136
getLocaleFileName($file, array $params=[])
Definition: FileSystem.php:102
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
static offsetPath($relatedPath, $path)
Definition: FileSystem.php:209
static getRelatedPath($relativeTo, $path)
Definition: FileSystem.php:230