FileManager.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\RequireJs\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\App\State as AppState;
  9. use Magento\Framework\RequireJs\Config;
  10. /**
  11. * A service for handling RequireJS files in the application
  12. */
  13. class FileManager
  14. {
  15. /**
  16. * @var Config
  17. */
  18. private $config;
  19. /**
  20. * @var \Magento\Framework\Filesystem
  21. */
  22. private $filesystem;
  23. /**
  24. * @var AppState
  25. */
  26. private $appState;
  27. /**
  28. * @var \Magento\Framework\View\Asset\Repository
  29. */
  30. private $assetRepo;
  31. /**
  32. * @param Config $config
  33. * @param \Magento\Framework\Filesystem $appFilesystem
  34. * @param AppState $appState
  35. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  36. */
  37. public function __construct(
  38. Config $config,
  39. \Magento\Framework\Filesystem $appFilesystem,
  40. AppState $appState,
  41. \Magento\Framework\View\Asset\Repository $assetRepo
  42. ) {
  43. $this->config = $config;
  44. $this->filesystem = $appFilesystem;
  45. $this->appState = $appState;
  46. $this->assetRepo = $assetRepo;
  47. }
  48. /**
  49. * Create a view asset representing the aggregated configuration file
  50. *
  51. * @return \Magento\Framework\View\Asset\File
  52. */
  53. public function createRequireJsConfigAsset()
  54. {
  55. $relPath = $this->config->getConfigFileRelativePath();
  56. $this->ensureSourceFile($relPath);
  57. return $this->assetRepo->createArbitrary($relPath, '');
  58. }
  59. /**
  60. * Create '.min' files resolver asset
  61. *
  62. * @return \Magento\Framework\View\Asset\File
  63. */
  64. public function createMinResolverAsset()
  65. {
  66. $relPath = $this->config->getMinResolverRelativePath();
  67. $this->ensureMinResolverFile($relPath);
  68. return $this->assetRepo->createArbitrary($relPath, '');
  69. }
  70. /**
  71. * Create a view asset representing the aggregated configuration file
  72. *
  73. * @return \Magento\Framework\View\Asset\File
  74. */
  75. public function createRequireJsMixinsAsset()
  76. {
  77. return $this->assetRepo->createArbitrary($this->config->getMixinsFileRelativePath(), '');
  78. }
  79. /**
  80. * Create a view asset representing the aggregated configuration file
  81. *
  82. * @return \Magento\Framework\View\Asset\File
  83. */
  84. public function createRequireJsAsset()
  85. {
  86. return $this->assetRepo->createArbitrary($this->config->getRequireJsFileRelativePath(), '');
  87. }
  88. /**
  89. * Create a view asset representing the theme fallback mapping resolver file.
  90. *
  91. * @return \Magento\Framework\View\Asset\File
  92. */
  93. public function createUrlResolverAsset()
  94. {
  95. return $this->assetRepo->createArbitrary($this->config->getUrlResolverFileRelativePath(), '');
  96. }
  97. /**
  98. * Create a view asset representing the theme fallback mapping configuration file.
  99. *
  100. * @return \Magento\Framework\View\Asset\File|null
  101. */
  102. public function createRequireJsMapConfigAsset()
  103. {
  104. if ($this->checkIfExist($this->config->getMapFileRelativePath())) {
  105. return $this->assetRepo->createArbitrary($this->config->getMapFileRelativePath(), '');
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * Make sure the aggregated configuration is materialized
  112. *
  113. * By default write the file if it doesn't exist, but in developer mode always do it
  114. *
  115. * @param string $relPath
  116. * @return void
  117. */
  118. private function ensureSourceFile($relPath)
  119. {
  120. $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  121. if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
  122. $dir->writeFile($relPath, $this->config->getConfig());
  123. }
  124. }
  125. /**
  126. * Make sure the '.min' assets resolver is materialized
  127. *
  128. * @param string $relPath
  129. * @return void
  130. */
  131. private function ensureMinResolverFile($relPath)
  132. {
  133. $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  134. if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
  135. $dir->writeFile($relPath, $this->config->getMinResolverCode());
  136. }
  137. }
  138. /**
  139. * Create a view asset representing the static js functionality
  140. *
  141. * @return \Magento\Framework\View\Asset\File|false
  142. */
  143. public function createStaticJsAsset()
  144. {
  145. if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
  146. return false;
  147. }
  148. return $this->assetRepo->createAsset(Config::STATIC_FILE_NAME);
  149. }
  150. /**
  151. * Create a view assets representing the bundle js functionality
  152. *
  153. * @return \Magento\Framework\View\Asset\File[]
  154. */
  155. public function createBundleJsPool()
  156. {
  157. $bundles = [];
  158. if ($this->appState->getMode() == AppState::MODE_PRODUCTION) {
  159. $libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
  160. /** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
  161. $context = $this->assetRepo->getStaticViewFileContext();
  162. $bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
  163. if (!$libDir->isExist($bundleDir)) {
  164. return [];
  165. }
  166. foreach ($libDir->read($bundleDir) as $bundleFile) {
  167. if (pathinfo($bundleFile, PATHINFO_EXTENSION) !== 'js') {
  168. continue;
  169. }
  170. $relPath = $libDir->getRelativePath($bundleFile);
  171. $bundles[] = $this->assetRepo->createArbitrary($relPath, '');
  172. }
  173. }
  174. return $bundles;
  175. }
  176. /**
  177. * Remove all bundles from pool
  178. * @deprecated 100.1.1
  179. *
  180. * @return bool
  181. */
  182. public function clearBundleJsPool()
  183. {
  184. $dirWrite = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  185. /** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
  186. $context = $this->assetRepo->getStaticViewFileContext();
  187. $bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
  188. return $dirWrite->delete($bundleDir);
  189. }
  190. /**
  191. * Check if file exist
  192. *
  193. * @param string $relPath
  194. * @return bool
  195. */
  196. private function checkIfExist($relPath)
  197. {
  198. $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
  199. return $dir->isExist($relPath);
  200. }
  201. }