123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\RequireJs\Model;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\Framework\App\State as AppState;
- use Magento\Framework\RequireJs\Config;
- /**
- * A service for handling RequireJS files in the application
- */
- class FileManager
- {
- /**
- * @var Config
- */
- private $config;
- /**
- * @var \Magento\Framework\Filesystem
- */
- private $filesystem;
- /**
- * @var AppState
- */
- private $appState;
- /**
- * @var \Magento\Framework\View\Asset\Repository
- */
- private $assetRepo;
- /**
- * @param Config $config
- * @param \Magento\Framework\Filesystem $appFilesystem
- * @param AppState $appState
- * @param \Magento\Framework\View\Asset\Repository $assetRepo
- */
- public function __construct(
- Config $config,
- \Magento\Framework\Filesystem $appFilesystem,
- AppState $appState,
- \Magento\Framework\View\Asset\Repository $assetRepo
- ) {
- $this->config = $config;
- $this->filesystem = $appFilesystem;
- $this->appState = $appState;
- $this->assetRepo = $assetRepo;
- }
- /**
- * Create a view asset representing the aggregated configuration file
- *
- * @return \Magento\Framework\View\Asset\File
- */
- public function createRequireJsConfigAsset()
- {
- $relPath = $this->config->getConfigFileRelativePath();
- $this->ensureSourceFile($relPath);
- return $this->assetRepo->createArbitrary($relPath, '');
- }
- /**
- * Create '.min' files resolver asset
- *
- * @return \Magento\Framework\View\Asset\File
- */
- public function createMinResolverAsset()
- {
- $relPath = $this->config->getMinResolverRelativePath();
- $this->ensureMinResolverFile($relPath);
- return $this->assetRepo->createArbitrary($relPath, '');
- }
- /**
- * Create a view asset representing the aggregated configuration file
- *
- * @return \Magento\Framework\View\Asset\File
- */
- public function createRequireJsMixinsAsset()
- {
- return $this->assetRepo->createArbitrary($this->config->getMixinsFileRelativePath(), '');
- }
- /**
- * Create a view asset representing the aggregated configuration file
- *
- * @return \Magento\Framework\View\Asset\File
- */
- public function createRequireJsAsset()
- {
- return $this->assetRepo->createArbitrary($this->config->getRequireJsFileRelativePath(), '');
- }
- /**
- * Create a view asset representing the theme fallback mapping resolver file.
- *
- * @return \Magento\Framework\View\Asset\File
- */
- public function createUrlResolverAsset()
- {
- return $this->assetRepo->createArbitrary($this->config->getUrlResolverFileRelativePath(), '');
- }
- /**
- * Create a view asset representing the theme fallback mapping configuration file.
- *
- * @return \Magento\Framework\View\Asset\File|null
- */
- public function createRequireJsMapConfigAsset()
- {
- if ($this->checkIfExist($this->config->getMapFileRelativePath())) {
- return $this->assetRepo->createArbitrary($this->config->getMapFileRelativePath(), '');
- } else {
- return null;
- }
- }
- /**
- * Make sure the aggregated configuration is materialized
- *
- * By default write the file if it doesn't exist, but in developer mode always do it
- *
- * @param string $relPath
- * @return void
- */
- private function ensureSourceFile($relPath)
- {
- $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
- if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
- $dir->writeFile($relPath, $this->config->getConfig());
- }
- }
- /**
- * Make sure the '.min' assets resolver is materialized
- *
- * @param string $relPath
- * @return void
- */
- private function ensureMinResolverFile($relPath)
- {
- $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
- if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
- $dir->writeFile($relPath, $this->config->getMinResolverCode());
- }
- }
- /**
- * Create a view asset representing the static js functionality
- *
- * @return \Magento\Framework\View\Asset\File|false
- */
- public function createStaticJsAsset()
- {
- if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
- return false;
- }
- return $this->assetRepo->createAsset(Config::STATIC_FILE_NAME);
- }
- /**
- * Create a view assets representing the bundle js functionality
- *
- * @return \Magento\Framework\View\Asset\File[]
- */
- public function createBundleJsPool()
- {
- $bundles = [];
- if ($this->appState->getMode() == AppState::MODE_PRODUCTION) {
- $libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
- /** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
- $context = $this->assetRepo->getStaticViewFileContext();
- $bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
- if (!$libDir->isExist($bundleDir)) {
- return [];
- }
- foreach ($libDir->read($bundleDir) as $bundleFile) {
- if (pathinfo($bundleFile, PATHINFO_EXTENSION) !== 'js') {
- continue;
- }
- $relPath = $libDir->getRelativePath($bundleFile);
- $bundles[] = $this->assetRepo->createArbitrary($relPath, '');
- }
- }
- return $bundles;
- }
- /**
- * Remove all bundles from pool
- * @deprecated 100.1.1
- *
- * @return bool
- */
- public function clearBundleJsPool()
- {
- $dirWrite = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
- /** @var $context \Magento\Framework\View\Asset\File\FallbackContext */
- $context = $this->assetRepo->getStaticViewFileContext();
- $bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
- return $dirWrite->delete($bundleDir);
- }
- /**
- * Check if file exist
- *
- * @param string $relPath
- * @return bool
- */
- private function checkIfExist($relPath)
- {
- $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
- return $dir->isExist($relPath);
- }
- }
|