/** * Generate a snapshot of the style */ public function generate() { // Get CSS files $stylesheetsDirectory = $this->basePath . '/stylesheets'; $filePaths = glob($stylesheetsDirectory . '/*.css'); // If specified, pre-compile the original files if ($this->sass) { foreach ($filePaths as $filePath) { $sassFilePath = $this->basePath . '/sass/' . pathinfo($filePath, PATHINFO_FILENAME) . '.scss'; $this->_sassCompile($sassFilePath, $filePath); } } // Unify the original files // They must be readable $unified = ''; foreach ($filePaths as $filePath) { if (!is_readable($filePath)) { $logger = Logger::getInstance(); $logger->warning('The file ' . $filePath . ' is not readable'); return; } $fileContent = file_get_contents($filePath); $unified .= $fileContent; } // Get the checksum $checksum = md5($unified); // Copy the entire style into a new directory // If the directory already exists, then do nothing $snapshotDirectory = $this->basePath . '/generated-styles/' . $checksum; if (is_dir($snapshotDirectory)) { return; } mkdir($snapshotDirectory, 0777 - umask(), true); FileSystem::copyDirectory($this->basePath . '/stylesheets', $snapshotDirectory . '/stylesheets'); FileSystem::copyDirectory($this->basePath . '/images', $snapshotDirectory . '/images'); FileSystem::copyDirectory($this->basePath . '/fonts', $snapshotDirectory . '/fonts'); // Minify $compressor = new \CSSmin(); $newFilePaths = glob($snapshotDirectory . '/stylesheets/*.css'); foreach ($newFilePaths as $newFilePath) { $newFileContent = file_get_contents($newFilePath); $minified = $compressor->run($newFileContent); file_put_contents($newFilePath, $minified); } // Save the version $versionPath = $this->basePath . '/generated-styles/version.txt'; if (file_exists($versionPath) && !is_writable($versionPath)) { $logger = Logger::getInstance(); $logger->warning('The file ' . $versionPath . ' is not writable'); return; } file_put_contents($versionPath, $checksum); }
/** * Generate a snapshot of the javascripts */ public function generate() { $javascriptsDirectory = realpath($this->basePath) . '/javascripts'; $generatedDirectory = realpath($this->basePath) . '/generated-js'; // Get JS files $filePaths = FileSystem::rglob('*.js', $javascriptsDirectory . '/'); // Minify the original files // They must be readable foreach ($filePaths as $filePath) { if (!is_readable($filePath)) { $logger = Logger::getInstance(); $logger->warning('The file ' . $filePath . ' is not readable'); return; } // Get the file name and the directory paths $fileName = pathinfo($filePath, PATHINFO_FILENAME); $fileDirectory = pathinfo($filePath, PATHINFO_DIRNAME); $fileSubDirectory = substr($fileDirectory, strlen($javascriptsDirectory)); $fileGeneratedDirectory = $generatedDirectory . $fileSubDirectory; // Create the generated directory if necessary if (!is_dir($fileGeneratedDirectory)) { mkdir($fileGeneratedDirectory, 0777 - umask(), true); } // Get the file content $fileContent = file_get_contents($filePath); // Get the checksum $checksum = md5($fileContent); // Minify $generatedFilePath = $fileGeneratedDirectory . '/' . $fileName . '.' . $checksum . '.js'; $minified = Minifier::minify($fileContent); file_put_contents($generatedFilePath, $minified); // Save the version $versionPath = $fileGeneratedDirectory . '/' . $fileName . '.version.txt'; if (file_exists($versionPath) && !is_writable($versionPath)) { $logger = Logger::getInstance(); $logger->warning('The file ' . $versionPath . ' is not writable'); return; } file_put_contents($versionPath, $checksum); } }