filesystem = $filesystem; $this->htmlDirectory = $filesystem->getDirectoryWrite(DirectoryList::TMP_MATERIALIZATION_DIR); $this->readFactory = $readFactory; } /** * Return path to minified template file, or minify if file not exist * * @param string $file * @return string */ public function getMinified($file) { $file = $this->htmlDirectory->getDriver()->getRealPathSafety($file); if (!$this->htmlDirectory->isExist($this->getRelativeGeneratedPath($file))) { $this->minify($file); } return $this->getPathToMinified($file); } /** * Return path to minified template file * * @param string $file * @return string */ public function getPathToMinified($file) { return $this->htmlDirectory->getAbsolutePath($this->getRelativeGeneratedPath($file)); } /** * Minify template file * * @param string $file * @return void */ public function minify($file) { $dir = dirname($file); $fileName = basename($file); $content = preg_replace( '#(?)\s+)\s+#', '$1 ', preg_replace( '#(?inlineHtmlTags) . ')\> \<#', '><', preg_replace( '#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)' . '(?:<(?>textarea|pre|script)\b|\z))#', ' ', preg_replace( '#(?)[^\n\r]*#', '', preg_replace( '#(?)#', ' $1', preg_replace( '#(?)[^\n\r]*#', '', $this->readFactory->create($dir)->readFile($fileName) ) ) ) ) ) ) ); if (!$this->htmlDirectory->isExist()) { $this->htmlDirectory->create(); } $this->htmlDirectory->writeFile($this->getRelativeGeneratedPath($file), rtrim($content)); } /** * Gets the relative path of minified file to generation directory * * @param string $sourcePath * @return string */ private function getRelativeGeneratedPath($sourcePath) { return $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getRelativePath($sourcePath); } }