/** * Takes a list of files, and combines them into a single minified file. * Doesn't bother if none of the files have been modified since the cache * file was written. * * @param string $type 'css' / 'js' * @param array $file_group Array of ('file' => filename, 'minified' => is_minified) * to combine and minify. * @param bool $minify whether to minify the files, as well as combining them * @return string The path to the cache file which was written. */ private static function combine($type, $file_group, $minify, $inline) { $filename = md5(implode('', array_map(function ($a) { return $a['file']; }, $file_group)) . ($minify ? 'min' : '')) . '.' . $type; // Get the last modified time of all of the component files $last_mod = 0; foreach ($file_group as $file) { // If it's a remote file just assume it isn't modified, otherwise // we're stuck making a ton of HTTP requests if (strpos($file['file'], '//') !== false) { continue; } $mod = filemtime(DOCROOT . $file['file']); if ($mod > $last_mod) { $last_mod = $mod; } } $filepath = DOCROOT . static::$cache_path . '/' . $filename; $needs_update = !file_exists($filepath) || ($mtime = filemtime($filepath)) < $last_mod; if ($needs_update) { $content = ''; foreach ($file_group as $file) { if (static::$show_files_inline) { $content .= PHP_EOL . '/* ' . $file['file'] . ' */' . PHP_EOL . PHP_EOL; } if ($file['minified'] || !$minify) { $content .= file_get_contents($file['file']) . PHP_EOL; } else { $file_content = file_get_contents($file['file']); if ($file_content === false) { throw new \Fuel_Exception("Couldn't not open file {$file['file']}"); } if ($type == 'js') { $content .= Casset_JSMin::minify($file_content) . PHP_EOL; } elseif ($type == 'css') { $css = Casset_Csscompressor::process($file_content) . PHP_EOL; $content .= Casset_Cssurirewriter::rewrite($css, dirname($file['file'])); } } } file_put_contents($filepath, $content, LOCK_EX); $mtime = time(); } if (!$inline) { $filename .= '?' . $mtime; } return $filename; }
/** * Takes a list of files, and combines them into a single minified file. * Doesn't bother if none of the files have been modified since the cache * file was written. * * @param string $type 'css' / 'js' * @param array $file_group Array of ('file' => filename, 'minified' => is_minified) * to combine and minify. * @param bool $minify whether to minify the files, as well as combining them * @return string The path to the cache file which was written. */ protected static function combine($type, $file_group, $minify, $inline) { // Get the last modified time of all of the component files $last_mod = 0; foreach ($file_group as $file) { // If it's a remote file just assume it isn't modified, otherwise // we're stuck making a ton of HTTP requests if (strpos($file['file'], '//') !== false) { continue; } $mod = filemtime(static::$root_path . $file['file']); if ($mod > $last_mod) { $last_mod = $mod; } } $filename = md5(implode('', array_map(function ($a) { return $a['file']; }, $file_group)) . ($minify ? 'min' : '') . $last_mod) . '.' . $type; $rel_filepath = static::$cache_path . '/' . $filename; $abs_filepath = static::$root_path . $rel_filepath; $needs_update = !file_exists($abs_filepath); if ($needs_update) { $content = ''; foreach ($file_group as $file) { if (static::$show_files_inline) { $content .= PHP_EOL . '/* ' . $file['file'] . ' */' . PHP_EOL . PHP_EOL; } if ($file['minified'] || !$minify) { $content .= static::load_file($file['file'], $type, $file_group, $rel_filepath) . PHP_EOL; } else { $file_content = static::load_file($file['file'], $type, $file_group, $rel_filepath); if ($file_content === false) { throw new Casset_Exception("Couldn't not open file {$file['file']}"); } if ($type == 'js') { $content .= Casset_JSMin::minify($file_content) . PHP_EOL; } elseif ($type == 'css') { $content .= Casset_Csscompressor::process($file_content) . PHP_EOL; } } } if ($type == 'css' && static::$move_imports_to_top) { // Remove @import lines, and record them $imports = array(); $content = preg_replace_callback('/@import.*?;/', function ($match) use(&$imports) { $imports[] = $match[0]; return ''; }, $content); if (count($imports)) { $content = implode(PHP_EOL, $imports) . PHP_EOL . $content; } } file_put_contents($abs_filepath, $content, LOCK_EX); $mtime = time(); } return $filename; }