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(), ''); } /** * 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 */ public function createStaticJsAsset() { if ($this->appState->getMode() != AppState::MODE_PRODUCTION) { return false; } $libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW); $relPath = $libDir->getRelativePath(Config::STATIC_FILE_NAME); /** @var $context \Magento\Framework\View\Asset\File\FallbackContext */ $context = $this->assetRepo->getStaticViewFileContext(); return $this->assetRepo->createArbitrary($relPath, $context->getPath()); } /** * 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) { $relPath = $libDir->getRelativePath($bundleFile); $bundles[] = $this->assetRepo->createArbitrary($relPath, ''); } } return $bundles; } /** * Remove all bundles from pool * * @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); } }