/vendor/magento/framework/View/FileSystem.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 249 lines · 119 code · 17 blank · 113 comment · 15 complexity · e4c28f442afbcb57f42bde52a283eccb MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View;
  7. /**
  8. * Model that finds file paths by their fileId
  9. */
  10. class FileSystem
  11. {
  12. /**
  13. * @var \Magento\Framework\View\Design\FileResolution\Fallback\File
  14. */
  15. protected $_fileResolution;
  16. /**
  17. * @var \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile
  18. */
  19. protected $_templateFileResolution;
  20. /**
  21. * @var \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile
  22. */
  23. protected $_localeFileResolution;
  24. /**
  25. * @var \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile
  26. */
  27. protected $_staticFileResolution;
  28. /**
  29. * @var \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile
  30. */
  31. protected $_emailTemplateFileResolution;
  32. /**
  33. * View service
  34. *
  35. * @var \Magento\Framework\View\Asset\Repository
  36. */
  37. protected $_assetRepo;
  38. /**
  39. * Constructor
  40. *
  41. * @param \Magento\Framework\View\Design\FileResolution\Fallback\File $fallbackFile
  42. * @param \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile $fallbackTemplateFile
  43. * @param \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile $fallbackLocaleFile
  44. * @param \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallbackStaticFile
  45. * @param \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile $fallbackEmailTemplateFile
  46. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  47. */
  48. public function __construct(
  49. \Magento\Framework\View\Design\FileResolution\Fallback\File $fallbackFile,
  50. \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile $fallbackTemplateFile,
  51. \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile $fallbackLocaleFile,
  52. \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallbackStaticFile,
  53. \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile $fallbackEmailTemplateFile,
  54. \Magento\Framework\View\Asset\Repository $assetRepo
  55. ) {
  56. $this->_fileResolution = $fallbackFile;
  57. $this->_templateFileResolution = $fallbackTemplateFile;
  58. $this->_localeFileResolution = $fallbackLocaleFile;
  59. $this->_staticFileResolution = $fallbackStaticFile;
  60. $this->_emailTemplateFileResolution = $fallbackEmailTemplateFile;
  61. $this->_assetRepo = $assetRepo;
  62. }
  63. /**
  64. * Get existing file name with fallback to default
  65. *
  66. * @param string $fileId
  67. * @param array $params
  68. * @return string
  69. */
  70. public function getFilename($fileId, array $params = [])
  71. {
  72. list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
  73. $this->normalizePath($fileId)
  74. );
  75. if ($module) {
  76. $params['module'] = $module;
  77. }
  78. $this->_assetRepo->updateDesignParams($params);
  79. $file = $this->_fileResolution
  80. ->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
  81. return $file;
  82. }
  83. /**
  84. * Get a locale file
  85. *
  86. * @param string $file
  87. * @param array $params
  88. * @return string
  89. */
  90. public function getLocaleFileName($file, array $params = [])
  91. {
  92. $this->_assetRepo->updateDesignParams($params);
  93. return $this->_localeFileResolution
  94. ->getFile($params['area'], $params['themeModel'], $params['locale'], $file);
  95. }
  96. /**
  97. * Get a template file
  98. *
  99. * @param string $fileId
  100. * @param array $params
  101. * @return string|bool
  102. */
  103. public function getTemplateFileName($fileId, array $params = [])
  104. {
  105. list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
  106. $this->normalizePath($fileId)
  107. );
  108. if ($module) {
  109. $params['module'] = $module;
  110. }
  111. $this->_assetRepo->updateDesignParams($params);
  112. return $this->_templateFileResolution
  113. ->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
  114. }
  115. /**
  116. * Find a static view file using fallback mechanism
  117. *
  118. * @param string $fileId
  119. * @param array $params
  120. * @return string
  121. */
  122. public function getStaticFileName($fileId, array $params = [])
  123. {
  124. list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
  125. $this->normalizePath($fileId)
  126. );
  127. if ($module) {
  128. $params['module'] = $module;
  129. }
  130. $this->_assetRepo->updateDesignParams($params);
  131. return $this->_staticFileResolution
  132. ->getFile($params['area'], $params['themeModel'], $params['locale'], $filePath, $params['module']);
  133. }
  134. /**
  135. * Get an email template file
  136. *
  137. * @param string $fileId
  138. * @param array $params
  139. * @param string $module
  140. * @return string|bool
  141. */
  142. public function getEmailTemplateFileName($fileId, array $params, $module)
  143. {
  144. $this->_assetRepo->updateDesignParams($params);
  145. return $this->_emailTemplateFileResolution
  146. ->getFile($params['area'], $params['themeModel'], $params['locale'], $fileId, $module);
  147. }
  148. /**
  149. * Remove excessive "." and ".." parts from a path
  150. *
  151. * For example foo/bar/../file.ext -> foo/file.ext
  152. *
  153. * @param string $path
  154. * @return string
  155. */
  156. public static function normalizePath($path)
  157. {
  158. $parts = explode('/', $path);
  159. $result = [];
  160. foreach ($parts as $part) {
  161. if ('..' === $part) {
  162. if (!count($result) || ($result[count($result) - 1] == '..')) {
  163. $result[] = $part;
  164. } else {
  165. array_pop($result);
  166. }
  167. } elseif ('.' !== $part) {
  168. $result[] = $part;
  169. }
  170. }
  171. return implode('/', $result);
  172. }
  173. /**
  174. * Get a relative path between $relatedPath and $path paths as if $path was to refer to $relatedPath
  175. * relatively of itself
  176. *
  177. * Returns new calculated relative path.
  178. * Examples:
  179. * $path: /some/directory/one/file.ext
  180. * $relatedPath: /some/directory/two/another/file.ext
  181. * Result: ../two/another
  182. *
  183. * $path: http://example.com/themes/demo/css/styles.css
  184. * $relatedPath: http://example.com/images/logo.gif
  185. * Result: ../../../images
  186. *
  187. * @param string $relatedPath
  188. * @param string $path
  189. * @return string
  190. */
  191. public static function offsetPath($relatedPath, $path)
  192. {
  193. $relatedPath = self::normalizePath($relatedPath);
  194. $path = self::normalizePath($path);
  195. list($relatedPath, $path) = self::ltrimSamePart($relatedPath, $path);
  196. $toDir = ltrim(dirname($path), '/');
  197. if ($toDir == '.') {
  198. $offset = '';
  199. } else {
  200. $offset = str_repeat('../', count(explode('/', $toDir)));
  201. }
  202. return rtrim($offset . dirname($relatedPath), '/');
  203. }
  204. /**
  205. * Concatenate/normalize a path to another path as a relative, assuming it will be relative to its directory
  206. *
  207. * @param string $relativeTo
  208. * @param string $path
  209. * @return string
  210. */
  211. public static function getRelatedPath($relativeTo, $path)
  212. {
  213. return self::normalizePath(dirname($relativeTo) . '/' . $path);
  214. }
  215. /**
  216. * Left-trim same part of two paths
  217. *
  218. * @param string $pathOne
  219. * @param string $pathTwo
  220. * @return array
  221. */
  222. private static function ltrimSamePart($pathOne, $pathTwo)
  223. {
  224. $one = explode('/', $pathOne);
  225. $two = explode('/', $pathTwo);
  226. while (isset($one[0]) && isset($two[0]) && $one[0] == $two[0]) {
  227. array_shift($one);
  228. array_shift($two);
  229. }
  230. return [implode('/', $one), implode('/', $two)];
  231. }
  232. }