PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/framework/View/Asset/Repository.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 412 lines | 216 code | 31 blank | 165 comment | 16 complexity | 901ea6ab3436e06bc1a30ab90b1ceb6c 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\Asset;
  7. use Magento\Framework\UrlInterface;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Filesystem;
  10. /**
  11. * A repository service for view assets
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Repository
  15. {
  16. /**
  17. * Scope separator for module notation of file ID
  18. */
  19. const FILE_ID_SEPARATOR = '::';
  20. /**
  21. * @var \Magento\Framework\UrlInterface
  22. */
  23. private $baseUrl;
  24. /**
  25. * @var \Magento\Framework\View\DesignInterface
  26. */
  27. private $design;
  28. /**
  29. * @var \Magento\Framework\View\Design\Theme\ListInterface
  30. */
  31. private $themeList;
  32. /**
  33. * @var \Magento\Framework\View\Asset\Source
  34. */
  35. private $assetSource;
  36. /**
  37. * @var \Magento\Framework\View\Asset\ContextInterface[]
  38. */
  39. private $fallbackContext;
  40. /**
  41. * @var \Magento\Framework\View\Asset\ContextInterface[]
  42. */
  43. private $fileContext;
  44. /**
  45. * @var null|array
  46. */
  47. private $defaults = null;
  48. /**
  49. * @var FileFactory
  50. */
  51. private $fileFactory;
  52. /**
  53. * @var File\FallbackContextFactory
  54. */
  55. private $fallbackContextFactory;
  56. /**
  57. * @var File\ContextFactory
  58. */
  59. private $contextFactory;
  60. /**
  61. * @var RemoteFactory
  62. */
  63. private $remoteFactory;
  64. /**
  65. * @param \Magento\Framework\UrlInterface $baseUrl
  66. * @param \Magento\Framework\View\DesignInterface $design
  67. * @param \Magento\Framework\View\Design\Theme\ListInterface $themeList
  68. * @param \Magento\Framework\View\Asset\Source $assetSource
  69. * @param \Magento\Framework\App\Request\Http $request
  70. * @param FileFactory $fileFactory
  71. * @param File\FallbackContextFactory $fallbackContextFactory
  72. * @param File\ContextFactory $contextFactory
  73. * @param RemoteFactory $remoteFactory
  74. */
  75. public function __construct(
  76. \Magento\Framework\UrlInterface $baseUrl,
  77. \Magento\Framework\View\DesignInterface $design,
  78. \Magento\Framework\View\Design\Theme\ListInterface $themeList,
  79. \Magento\Framework\View\Asset\Source $assetSource,
  80. \Magento\Framework\App\Request\Http $request,
  81. FileFactory $fileFactory,
  82. File\FallbackContextFactory $fallbackContextFactory,
  83. File\ContextFactory $contextFactory,
  84. RemoteFactory $remoteFactory
  85. ) {
  86. $this->baseUrl = $baseUrl;
  87. $this->design = $design;
  88. $this->themeList = $themeList;
  89. $this->assetSource = $assetSource;
  90. $this->request = $request;
  91. $this->fileFactory = $fileFactory;
  92. $this->fallbackContextFactory = $fallbackContextFactory;
  93. $this->contextFactory = $contextFactory;
  94. $this->remoteFactory = $remoteFactory;
  95. }
  96. /**
  97. * Update required parameters with default values if custom not specified
  98. *
  99. * @param array &$params
  100. * @throws \UnexpectedValueException
  101. * @return $this
  102. *
  103. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  104. */
  105. public function updateDesignParams(array &$params)
  106. {
  107. // Set area
  108. if (empty($params['area'])) {
  109. $params['area'] = $this->getDefaultParameter('area');
  110. }
  111. // Set themeModel
  112. $theme = null;
  113. $area = $params['area'];
  114. if (!empty($params['themeId'])) {
  115. $theme = $params['themeId'];
  116. } elseif (isset($params['theme'])) {
  117. $theme = $params['theme'];
  118. } elseif (empty($params['themeModel']) && $area !== $this->getDefaultParameter('area')) {
  119. $theme = $this->design->getConfigurationDesignTheme($area);
  120. }
  121. if ($theme) {
  122. $params['themeModel'] = $this->themeList->getThemeByFullPath($area . '/' . $theme);
  123. if (!$params['themeModel']) {
  124. throw new \UnexpectedValueException("Could not find theme '$theme' for area '$area'");
  125. }
  126. } elseif (empty($params['themeModel'])) {
  127. $params['themeModel'] = $this->getDefaultParameter('themeModel');
  128. }
  129. // Set module
  130. if (!array_key_exists('module', $params)) {
  131. $params['module'] = false;
  132. }
  133. // Set locale
  134. if (empty($params['locale'])) {
  135. $params['locale'] = $this->getDefaultParameter('locale');
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Get default design parameter
  141. *
  142. * @param string $name
  143. * @return mixed
  144. */
  145. private function getDefaultParameter($name)
  146. {
  147. $this->defaults = $this->design->getDesignParams();
  148. return $this->defaults[$name];
  149. }
  150. /**
  151. * Create a file asset that's subject of fallback system
  152. *
  153. * @param string $fileId
  154. * @param array $params
  155. * @return File
  156. */
  157. public function createAsset($fileId, array $params = [])
  158. {
  159. $this->updateDesignParams($params);
  160. list($module, $filePath) = self::extractModule($fileId);
  161. if (!$module && $params['module']) {
  162. $module = $params['module'];
  163. }
  164. $isSecure = isset($params['_secure']) ? (bool) $params['_secure'] : null;
  165. $themePath = isset($params['theme']) ? $params['theme'] : $this->design->getThemePath($params['themeModel']);
  166. $context = $this->getFallbackContext(
  167. UrlInterface::URL_TYPE_STATIC,
  168. $isSecure,
  169. $params['area'],
  170. $themePath,
  171. $params['locale']
  172. );
  173. return $this->fileFactory->create(
  174. [
  175. 'source' => $this->assetSource,
  176. 'context' => $context,
  177. 'filePath' => $filePath,
  178. 'module' => $module,
  179. 'contentType' => $this->assetSource->getContentType($filePath)
  180. ]
  181. );
  182. }
  183. /**
  184. * Get current context for static view files
  185. *
  186. * @return \Magento\Framework\View\Asset\ContextInterface
  187. */
  188. public function getStaticViewFileContext()
  189. {
  190. $params = [];
  191. $this->updateDesignParams($params);
  192. $themePath = $this->design->getThemePath($params['themeModel']);
  193. $isSecure = $this->request->isSecure();
  194. return $this->getFallbackContext(
  195. UrlInterface::URL_TYPE_STATIC,
  196. $isSecure,
  197. $params['area'],
  198. $themePath,
  199. $params['locale']
  200. );
  201. }
  202. /**
  203. * Get a fallback context value object
  204. *
  205. * Create only one instance per combination of parameters
  206. *
  207. * @param string $urlType
  208. * @param bool|null $isSecure
  209. * @param string $area
  210. * @param string $themePath
  211. * @param string $locale
  212. * @return \Magento\Framework\View\Asset\File\FallbackContext
  213. */
  214. private function getFallbackContext($urlType, $isSecure, $area, $themePath, $locale)
  215. {
  216. $secureKey = null === $isSecure ? 'null' : (int)$isSecure;
  217. $baseDirType = DirectoryList::STATIC_VIEW;
  218. $id = implode('|', [$baseDirType, $urlType, $secureKey, $area, $themePath, $locale]);
  219. if (!isset($this->fallbackContext[$id])) {
  220. $url = $this->baseUrl->getBaseUrl(['_type' => $urlType, '_secure' => $isSecure]);
  221. $this->fallbackContext[$id] = $this->fallbackContextFactory->create(
  222. [
  223. 'baseUrl' => $url,
  224. 'areaType' => $area,
  225. 'themePath' => $themePath,
  226. 'localeCode' => $locale,
  227. 'isSecure' => $isSecure
  228. ]
  229. );
  230. }
  231. return $this->fallbackContext[$id];
  232. }
  233. /**
  234. * Create a file asset similar to an existing local asset by using its context
  235. *
  236. * @param string $fileId
  237. * @param LocalInterface $similarTo
  238. * @return File
  239. */
  240. public function createSimilar($fileId, LocalInterface $similarTo)
  241. {
  242. list($module, $filePath) = self::extractModule($fileId);
  243. if (!$module) {
  244. $module = $similarTo->getModule();
  245. }
  246. return $this->fileFactory->create(
  247. [
  248. 'source' => $this->assetSource,
  249. 'context' => $similarTo->getContext(),
  250. 'filePath' => $filePath,
  251. 'module' => $module,
  252. 'contentType' => $this->assetSource->getContentType($filePath)
  253. ]
  254. );
  255. }
  256. /**
  257. * Create a file asset with an arbitrary path
  258. *
  259. * This kind of file is not subject of fallback system
  260. * Client code is responsible for ensuring that the file is in specified directory
  261. *
  262. * @param string $filePath
  263. * @param string $dirPath
  264. * @param string $baseDirType
  265. * @param string $baseUrlType
  266. * @return File
  267. */
  268. public function createArbitrary(
  269. $filePath,
  270. $dirPath,
  271. $baseDirType = DirectoryList::STATIC_VIEW,
  272. $baseUrlType = UrlInterface::URL_TYPE_STATIC
  273. ) {
  274. $context = $this->getFileContext($baseDirType, $baseUrlType, $dirPath);
  275. $contentType = $this->assetSource->getContentType($filePath);
  276. return $this->fileFactory->create(
  277. [
  278. 'source' => $this->assetSource,
  279. 'context' => $context,
  280. 'filePath' => $filePath,
  281. 'module' => '',
  282. 'contentType' => $contentType
  283. ]
  284. );
  285. }
  286. /**
  287. * Get a file context value object
  288. *
  289. * Same instance per set of parameters
  290. *
  291. * @param string $baseDirType
  292. * @param string $urlType
  293. * @param string $dirPath
  294. * @return \Magento\Framework\View\Asset\File\Context
  295. */
  296. private function getFileContext($baseDirType, $urlType, $dirPath)
  297. {
  298. $id = implode('|', [$baseDirType, $urlType, $dirPath]);
  299. if (!isset($this->fileContext[$id])) {
  300. $url = $this->baseUrl->getBaseUrl(['_type' => $urlType]);
  301. $this->fileContext[$id] = $this->contextFactory->create(
  302. ['baseUrl' => $url, 'baseDirType' => $baseDirType, 'contextPath' => $dirPath]
  303. );
  304. }
  305. return $this->fileContext[$id];
  306. }
  307. /**
  308. * Create a file asset with path relative to specified local asset
  309. *
  310. * @param string $fileId
  311. * @param LocalInterface $relativeTo
  312. * @return File
  313. */
  314. public function createRelated($fileId, LocalInterface $relativeTo)
  315. {
  316. list($module, $filePath) = self::extractModule($fileId);
  317. if ($module) {
  318. return $this->createSimilar($fileId, $relativeTo);
  319. }
  320. $filePath = \Magento\Framework\View\FileSystem::getRelatedPath($relativeTo->getFilePath(), $filePath);
  321. return $this->createSimilar($filePath, $relativeTo);
  322. }
  323. /**
  324. * Create a remote asset value object
  325. *
  326. * @param string $url
  327. * @param string $contentType
  328. * @return Remote
  329. * @codeCoverageIgnore
  330. */
  331. public function createRemoteAsset($url, $contentType)
  332. {
  333. return $this->remoteFactory->create(['url' => $url, 'contentType' => $contentType]);
  334. }
  335. /**
  336. * Getter for static view file URL
  337. *
  338. * @param string $fileId
  339. * @return string
  340. */
  341. public function getUrl($fileId)
  342. {
  343. $asset = $this->createAsset($fileId);
  344. return $asset->getUrl();
  345. }
  346. /**
  347. * A getter for static view file URL with special parameters
  348. *
  349. * To omit parameters and have them automatically determined from application state, use getUrl()
  350. *
  351. * @param string $fileId
  352. * @param array $params
  353. * @return string
  354. * @see getUrl()
  355. */
  356. public function getUrlWithParams($fileId, array $params)
  357. {
  358. $asset = $this->createAsset($fileId, $params);
  359. return $asset->getUrl();
  360. }
  361. /**
  362. * Extract module name from specified file ID
  363. *
  364. * @param string $fileId
  365. * @return array
  366. * @throws \Magento\Framework\Exception\LocalizedException
  367. */
  368. public static function extractModule($fileId)
  369. {
  370. if (strpos($fileId, self::FILE_ID_SEPARATOR) === false) {
  371. return ['', $fileId];
  372. }
  373. $result = explode(self::FILE_ID_SEPARATOR, $fileId, 2);
  374. if (empty($result[0])) {
  375. throw new \Magento\Framework\Exception\LocalizedException(
  376. new \Magento\Framework\Phrase('Scope separator "::" cannot be used without scope identifier.')
  377. );
  378. }
  379. return [$result[0], $result[1]];
  380. }
  381. }