pubStaticDir = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); $this->bundleFactory = $bundleFactory; $this->bundleConfig = $bundleConfig; $this->utilityFiles = $files; } /** * Deploy bundles for the given area, theme and locale * * @param string $area * @param string $theme * @param string $locale * @return void */ public function deploy($area, $theme, $locale) { $bundle = $this->bundleFactory->create([ 'area' => $area, 'theme' => $theme, 'locale' => $locale ]); // delete all previously created bundle files $bundle->clear(); $files = []; $mapFilePath = $area . '/' . $theme . '/' . $locale . '/' . RepositoryMap::RESULT_MAP_NAME; if ($this->pubStaticDir->isFile($mapFilePath)) { // map file is available in compact mode, so no need to scan filesystem one more time $resultMap = $this->pubStaticDir->readFile($mapFilePath); if ($resultMap) { $files = json_decode($resultMap, true); } } else { $packageDir = $this->pubStaticDir->getAbsolutePath($area . '/' . $theme . '/' . $locale); $files = $this->utilityFiles->getFiles([$packageDir], '*.*'); } foreach ($files as $filePath => $sourcePath) { if (is_array($sourcePath)) { $filePath = str_replace(Repository::FILE_ID_SEPARATOR, '/', $filePath); $sourcePath = $sourcePath['area'] . '/' . $sourcePath['theme'] . '/' . $sourcePath['locale'] . '/' . $filePath; } else { $sourcePath = str_replace('\\', '/', $sourcePath); $sourcePath = $this->pubStaticDir->getRelativePath($sourcePath); $filePath = substr($sourcePath, strlen($area . '/' . $theme . '/' . $locale) + 1); } $contentType = pathinfo($filePath, PATHINFO_EXTENSION); if (!in_array($contentType, self::$availableTypes)) { continue; } if ($this->hasMinVersion($filePath) || $this->isExcluded($filePath, $area, $theme)) { continue; } $bundle->addFile($filePath, $sourcePath, $contentType); } $bundle->flush(); } /** * Check if file is minified version or there is a minified version of the file * * @param string $filePath * @return bool */ private function hasMinVersion($filePath) { if (in_array($filePath, $this->excludedCache)) { return true; } $info = pathinfo($filePath); if (strpos($filePath, '.min.') !== false) { $this->excludedCache[] = str_replace(".min.{$info['extension']}", ".{$info['extension']}", $filePath); } else { $pathToMinVersion = $info['dirname'] . '/' . $info['filename'] . '.min.' . $info['extension']; if ($this->pubStaticDir->isExist($pathToMinVersion)) { $this->excludedCache[] = $filePath; return true; } } return false; } /** * Check if file is in exclude list * * @param string $filePath * @param string $area * @param string $theme * @return bool */ private function isExcluded($filePath, $area, $theme) { $excludedFiles = $this->bundleConfig->getExcludedFiles($area, $theme); foreach ($excludedFiles as $excludedFileId) { $excludedFilePath = $this->prepareExcludePath($excludedFileId); if ($excludedFilePath === $filePath) { return true; } } $excludedDirs = $this->bundleConfig->getExcludedDirectories($area, $theme); foreach ($excludedDirs as $directoryId) { $directoryPath = $this->prepareExcludePath($directoryId); if (strpos($filePath, $directoryPath) === 0) { return true; } } return false; } /** * Get excluded path * * @param string $path * @return array|bool */ private function prepareExcludePath($path) { if (strpos($path, Repository::FILE_ID_SEPARATOR) !== false) { list($excludedModule, $excludedPath) = explode(Repository::FILE_ID_SEPARATOR, $path); if ($excludedModule == 'Lib') { return $excludedPath; } else { return $excludedModule . '/' . $excludedPath; } } return $path; } }