Config.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\RequireJs;
  7. use Magento\Framework\Filesystem\DriverPool;
  8. use Magento\Framework\Filesystem\File\ReadFactory;
  9. use Magento\Framework\View\Asset\Minification;
  10. use Magento\Framework\View\Asset\RepositoryMap;
  11. /**
  12. * Provider of RequireJs config information
  13. */
  14. class Config
  15. {
  16. /**
  17. * Name of sub-directory where generated RequireJs config is placed
  18. *
  19. * @deprecated since 2.2.0 RequireJS Configuration file is moved into package directory
  20. */
  21. const DIR_NAME = '_requirejs';
  22. /**
  23. * File name of RequireJs config
  24. */
  25. const CONFIG_FILE_NAME = 'requirejs-config.js';
  26. /**
  27. * File name of RequireJs mixins
  28. */
  29. const MIXINS_FILE_NAME = 'mage/requirejs/mixins.js';
  30. /**
  31. * File name of RequireJs
  32. */
  33. const REQUIRE_JS_FILE_NAME = 'requirejs/require.js';
  34. /**
  35. * File name of StaticJs
  36. */
  37. const STATIC_FILE_NAME = 'mage/requirejs/static.js';
  38. /**
  39. * File name of minified files resolver
  40. */
  41. const MIN_RESOLVER_FILENAME = 'requirejs-min-resolver.js';
  42. /**
  43. * File name of RequireJs mixins
  44. */
  45. const MAP_FILE_NAME = 'requirejs-map.js';
  46. /**
  47. * File name of BaseUrlInterceptorJs
  48. */
  49. const URL_RESOLVER_FILE_NAME = 'mage/requirejs/baseUrlResolver.js';
  50. /**
  51. * File name of StaticJs
  52. */
  53. const BUNDLE_JS_DIR = 'js/bundle';
  54. /**
  55. * Template for combined RequireJs config file
  56. */
  57. const FULL_CONFIG_TEMPLATE = <<<config
  58. (function(require){
  59. %function%
  60. %usages%
  61. })(require);
  62. config;
  63. /**
  64. * Template for wrapped partial config
  65. */
  66. const PARTIAL_CONFIG_TEMPLATE = <<<config
  67. (function() {
  68. %config%
  69. require.config(config);
  70. })();
  71. config;
  72. /**
  73. * @var \Magento\Framework\RequireJs\Config\File\Collector\Aggregated
  74. */
  75. private $fileSource;
  76. /**
  77. * @var \Magento\Framework\View\DesignInterface
  78. */
  79. private $design;
  80. /**
  81. * @var \Magento\Framework\Filesystem\File\ReadFactory
  82. */
  83. private $readFactory;
  84. /**
  85. * @var \Magento\Framework\View\Asset\ContextInterface
  86. */
  87. private $staticContext;
  88. /**
  89. * @var \Magento\Framework\Code\Minifier\AdapterInterface
  90. */
  91. private $minifyAdapter;
  92. /**
  93. * @var Minification
  94. */
  95. private $minification;
  96. /**
  97. * @var RepositoryMap
  98. */
  99. private $repositoryMap;
  100. /**
  101. * @param \Magento\Framework\RequireJs\Config\File\Collector\Aggregated $fileSource
  102. * @param \Magento\Framework\View\DesignInterface $design
  103. * @param ReadFactory $readFactory
  104. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  105. * @param \Magento\Framework\Code\Minifier\AdapterInterface $minifyAdapter
  106. * @param Minification $minification
  107. * @param RepositoryMap $repositoryMap
  108. */
  109. public function __construct(
  110. \Magento\Framework\RequireJs\Config\File\Collector\Aggregated $fileSource,
  111. \Magento\Framework\View\DesignInterface $design,
  112. ReadFactory $readFactory,
  113. \Magento\Framework\View\Asset\Repository $assetRepo,
  114. \Magento\Framework\Code\Minifier\AdapterInterface $minifyAdapter,
  115. Minification $minification,
  116. RepositoryMap $repositoryMap
  117. ) {
  118. $this->fileSource = $fileSource;
  119. $this->design = $design;
  120. $this->readFactory = $readFactory;
  121. $this->staticContext = $assetRepo->getStaticViewFileContext();
  122. $this->minifyAdapter = $minifyAdapter;
  123. $this->minification = $minification;
  124. $this->repositoryMap = $repositoryMap;
  125. }
  126. /**
  127. * Get aggregated distributed configuration
  128. *
  129. * @return string
  130. */
  131. public function getConfig()
  132. {
  133. $distributedConfig = '';
  134. $customConfigFiles = $this->fileSource->getFiles($this->design->getDesignTheme(), self::CONFIG_FILE_NAME);
  135. foreach ($customConfigFiles as $file) {
  136. /** @var $fileReader \Magento\Framework\Filesystem\File\Read */
  137. $fileReader = $this->readFactory->create($file->getFilename(), DriverPool::FILE);
  138. $config = $fileReader->readAll($file->getName());
  139. $distributedConfig .= str_replace(
  140. ['%config%', '%context%'],
  141. [$config, $file->getModule()],
  142. self::PARTIAL_CONFIG_TEMPLATE
  143. );
  144. }
  145. $fullConfig = str_replace(
  146. ['%function%', '%usages%'],
  147. [$distributedConfig],
  148. self::FULL_CONFIG_TEMPLATE
  149. );
  150. if ($this->minification->isEnabled('js')) {
  151. $fullConfig = $this->minifyAdapter->minify($fullConfig);
  152. }
  153. return $fullConfig;
  154. }
  155. /**
  156. * Get path to config file relative to directory, where all config files with different context are located
  157. *
  158. * @return string
  159. */
  160. public function getConfigFileRelativePath()
  161. {
  162. return $this->staticContext->getConfigPath() . '/' . $this->getConfigFileName();
  163. }
  164. /**
  165. * Get path to config file relative to directory, where all config files with different context are located
  166. *
  167. * @return string
  168. */
  169. public function getMixinsFileRelativePath()
  170. {
  171. $map = $this->getRepositoryFilesMap(Config::MIXINS_FILE_NAME, [
  172. 'area' => $this->staticContext->getAreaCode(),
  173. 'theme' => $this->staticContext->getThemePath(),
  174. 'locale' => $this->staticContext->getLocale(),
  175. ]);
  176. if ($map) {
  177. $relativePath = implode('/', $map) . '/' . Config::MIXINS_FILE_NAME;
  178. } else {
  179. $relativePath = $this->staticContext->getPath() . '/' . self::MIXINS_FILE_NAME;
  180. }
  181. return $relativePath;
  182. }
  183. /**
  184. * Get path to config file relative to directory, where all config files with different context are located
  185. *
  186. * @return string
  187. */
  188. public function getRequireJsFileRelativePath()
  189. {
  190. return $this->staticContext->getConfigPath() . '/' . self::REQUIRE_JS_FILE_NAME;
  191. }
  192. /**
  193. * Get base RequireJs configuration necessary for working with Magento application
  194. *
  195. * @return string
  196. */
  197. public function getBaseConfig()
  198. {
  199. $config = [
  200. 'baseUrl' => $this->staticContext->getBaseUrl() . $this->staticContext->getPath(),
  201. ];
  202. $config = json_encode($config, JSON_UNESCAPED_SLASHES);
  203. $result = "require.config($config);";
  204. return $result;
  205. }
  206. /**
  207. * Get path to '.min' files resolver relative to config files directory
  208. *
  209. * @return string
  210. */
  211. public function getMinResolverRelativePath()
  212. {
  213. return
  214. $this->staticContext->getConfigPath() .
  215. '/' .
  216. $this->minification->addMinifiedSign(self::MIN_RESOLVER_FILENAME);
  217. }
  218. /**
  219. * Get path to URL map resover file
  220. *
  221. * @return string
  222. */
  223. public function getUrlResolverFileRelativePath()
  224. {
  225. $map = $this->getRepositoryFilesMap(Config::URL_RESOLVER_FILE_NAME, [
  226. 'area' => $this->staticContext->getAreaCode(),
  227. 'theme' => $this->staticContext->getThemePath(),
  228. 'locale' => $this->staticContext->getLocale(),
  229. ]);
  230. if ($map) {
  231. $relativePath = implode('/', $map) . '/' . Config::URL_RESOLVER_FILE_NAME;
  232. } else {
  233. $relativePath = $this->staticContext->getPath() . '/' . self::URL_RESOLVER_FILE_NAME;
  234. }
  235. return $relativePath;
  236. }
  237. /**
  238. * Get path to map file
  239. *
  240. * @return string
  241. */
  242. public function getMapFileRelativePath()
  243. {
  244. return $this->minification->addMinifiedSign($this->staticContext->getPath() . '/' . self::MAP_FILE_NAME);
  245. }
  246. /**
  247. * @return string
  248. */
  249. protected function getConfigFileName()
  250. {
  251. return $this->minification->addMinifiedSign(self::CONFIG_FILE_NAME);
  252. }
  253. /**
  254. * @return string
  255. */
  256. public function getMinResolverCode()
  257. {
  258. $excludes = [];
  259. foreach ($this->minification->getExcludes('js') as $expression) {
  260. $excludes[] = '!url.match(/' . str_replace('/', '\/', $expression) . '/)';
  261. }
  262. $excludesCode = empty($excludes) ? 'true' : implode('&&', $excludes);
  263. $result = <<<code
  264. var ctx = require.s.contexts._,
  265. origNameToUrl = ctx.nameToUrl;
  266. ctx.nameToUrl = function() {
  267. var url = origNameToUrl.apply(ctx, arguments);
  268. if ({$excludesCode}) {
  269. url = url.replace(/(\.min)?\.js$/, '.min.js');
  270. }
  271. return url;
  272. };
  273. code;
  274. if ($this->minification->isEnabled('js')) {
  275. $result = $this->minifyAdapter->minify($result);
  276. }
  277. return $result;
  278. }
  279. /**
  280. * @param string $fileId
  281. * @param array $params
  282. * @return array
  283. */
  284. private function getRepositoryFilesMap($fileId, array $params)
  285. {
  286. return $this->repositoryMap->getMap($fileId, $params);
  287. }
  288. }