public static function url($files) { $ident = self::ident($files); if (!\SYSTEM\CACHE\cache_css::get($ident)) { \LIB\lib_minify::php(); $minifier = new \MatthiasMullie\Minify\CSS(); foreach ($files as $f) { $minifier->add($f->SERVERPATH()); } \SYSTEM\CACHE\cache_css::put($ident, $minifier->minify()); } return './api.php?call=cache&id=' . self::CACHE_CSS . '&ident=' . $ident; }
/** * Get file information * * @param string $fileName The file name * @param string $targetIe Target IE of file * @param string $media Media of file * @param bool $forceMinify Whether this file should be minified * @return stdClass The file information */ private function getFileInfo($fileName, $targetIe = '', $media = 'all', $forceMinify = false) { static $existsInfo = array(); if (self::$minify === null) { self::$minify = Context::getDBInfo()->minify_scripts ?: 'common'; } if (isset($existsInfo[$existsKey])) { return $existsInfo[$existsKey]; } $pathInfo = pathinfo($fileName); $file = new stdClass(); $file->fileName = $pathInfo['basename']; $file->filePath = $this->_getAbsFileUrl($pathInfo['dirname']); $file->fileRealPath = FileHandler::getRealPath($pathInfo['dirname']); $file->fileExtension = strtolower($pathInfo['extension']); if (preg_match('/^(.+)\\.min$/', $pathInfo['filename'], $matches)) { $file->fileNameNoExt = $matches[1]; $file->isMinified = true; } else { $file->fileNameNoExt = $pathInfo['filename']; $file->isMinified = false; } $file->isExternalURL = preg_match('@^(https?:)?//@i', $file->filePath) ? true : false; $file->isCachedScript = !$file->isExternalURL && strpos($file->filePath, 'files/cache/') !== false; $file->keyName = $file->fileNameNoExt . '.' . $file->fileExtension; $file->cdnPath = $this->_normalizeFilePath($pathInfo['dirname']); $originalFilePath = $file->fileRealPath . '/' . $pathInfo['basename']; // Fix incorrectly minified URL if ($file->isMinified && !$file->isExternalURL && (!file_exists($originalFilePath) || is_link($originalFilePath) || filesize($originalFilePath) < 32 && trim(file_get_contents($originalFilePath)) === $file->keyName)) { if (file_exists($file->fileRealPath . '/' . $file->fileNameNoExt . '.' . $file->fileExtension)) { $file->fileName = $file->fileNameNoExt . '.' . $file->fileExtension; $file->isMinified = false; $originalFilePath = $file->fileRealPath . '/' . $file->fileNameNoExt . '.' . $file->fileExtension; } } // Decide whether to minify this file if (self::$minify === 'all') { $minify_enabled = true; } elseif (self::$minify === 'none') { $minify_enabled = false; } else { $minify_enabled = $forceMinify; } // Minify file if ($minify_enabled && !$file->isMinified && !$file->isExternalURL && !$file->isCachedScript && strpos($file->filePath, 'common/js/plugins') === false) { if (($file->fileExtension === 'css' || $file->fileExtension === 'js') && file_exists($originalFilePath)) { $minifiedFileName = $file->fileNameNoExt . '.min.' . $file->fileExtension; $minifiedFileHash = ltrim(str_replace(array('/', '\\'), '.', $pathInfo['dirname']), '.'); $minifiedFilePath = _XE_PATH_ . 'files/cache/minify/' . $minifiedFileHash . '.' . $minifiedFileName; if (!file_exists($minifiedFilePath) || filemtime($minifiedFilePath) < filemtime($originalFilePath)) { if ($file->fileExtension === 'css') { $minifier = new MatthiasMullie\Minify\CSS($originalFilePath); $content = $minifier->execute($minifiedFilePath); } else { $minifier = new MatthiasMullie\Minify\JS($originalFilePath); $content = $minifier->execute($minifiedFilePath); } FileHandler::writeFile($minifiedFilePath, $content); } $file->fileName = $minifiedFileHash . '.' . $minifiedFileName; $file->filePath = $this->_getAbsFileUrl('./files/cache/minify'); $file->fileRealPath = _XE_PATH_ . 'files/cache/minify'; $file->keyName = $minifiedFileHash . '.' . $file->fileNameNoExt . '.' . $file->fileExtension; $file->cdnPath = $this->_normalizeFilePath('./files/cache/minify'); $file->isMinified = true; } } // Process targetIe and media attributes $file->targetIe = $targetIe; if ($file->fileExtension == 'css') { $file->media = $media; if (!$file->media) { $file->media = 'all'; } $file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe . "\t" . $file->media; } else { if ($file->fileExtension == 'js') { $file->key = $file->filePath . $file->keyName . "\t" . $file->targetIe; } } return $file; }
/** * Minify CSS. * * @param string|array $source_filename * @param string $target_filename * @return bool */ public static function minifyCSS($source_filename, $target_filename) { $minifier = new \MatthiasMullie\Minify\CSS(); if (is_array($source_filename)) { foreach ($source_filename as $filename) { $minifier->add($filename); } } else { $minifier->add($source_filename); } $content = $minifier->execute($target_filename); Storage::write($target_filename, $content); return strlen($content) ? true : false; }
// Load Composer's PSR-4 autoloader (necessary to load Slim, Mini etc.) require '../vendor/autoload.php'; // Initialize Slim (the router/micro framework used). $app = new \Slim\Slim(); // and define the engine used for the view @see http://twig.sensiolabs.org $app->view = new \Slim\Views\Twig(); $app->view->setTemplatesDirectory("../Mini/view"); /******************************************* THE CONFIGS *******************************************************/ // Configs for mode "development" (Slim's default), see the GitHub readme for details on setting the environment $app->configureMode('development', function () use($app) { // pre-application hook, performs stuff before real action happens @see http://docs.slimframework.com/#Hooks $app->hook('slim.before', function () use($app) { // SASS-to-CSS compiler @see https://github.com/panique/php-sass SassCompiler::run("scss/", "css/"); // CSS minifier @see https://github.com/matthiasmullie/minify $minifier = new MatthiasMullie\Minify\CSS('css/style.css'); $minifier->minify('css/style.css'); // JS minifier @see https://github.com/matthiasmullie/minify // DON'T overwrite your real .js files, always save into a different file //$minifier = new MatthiasMullie\Minify\JS('js/application.js'); //$minifier->minify('js/application.minified.js'); }); // Set the configs for development environment $app->config(array('debug' => true, 'database' => array('db_host' => 'localhost', 'db_port' => '', 'db_name' => 'scdm', 'db_user' => 'root', 'db_pass' => ''))); }); // Configs for mode "production" $app->configureMode('production', function () use($app) { // Set the configs for production environment $app->config(array('debug' => false, 'database' => array('db_host' => '', 'db_port' => '', 'db_name' => '', 'db_user' => '', 'db_pass' => ''))); }); /************************************* Load Organization Defaults ***********************************************/
/** * Compressor libraries */ public function _compressor($language, $code = "") { $root = realpath(__DIR__); require_once $root . "/min-css.php"; require_once $root . "/jshrink.php"; require_once $root . "/html-compress.php"; if (mb_strlen($code, '8bit') > 500000) { return $code; } else { if ($language == "css") { /** * What kind of css stuff should it convert */ $minifier = new MatthiasMullie\Minify\CSS(); $minifier->add($code); return $minifier->minify(); } elseif ($language == "js") { return \JShrink\Minifier::minify($code); } elseif ($language == "html") { $html = new Tinyfier_HTML_Tool(); if ($this->curOptions['compress']["minInline"]) { return $html->process($code, array("compress_all" => true)); } else { return $html->process($code, array("compress_all" => false)); } } } }
/** * Minify CSS code. * * @param string $content * @return string minified CSS */ public static function css($content) { $error = 'unknown'; try { $minifier = new MatthiasMullie\Minify\CSS($content); return $minifier->minify(); } catch (Exception $e) { $error = $e->getMessage(); } $return = <<<EOD /* Error: {$error} */ /* Problem detected during CSS minimisation, please review the following code */ /* ========================================================================== */ EOD; return $return . $content; }
public function compress_css_action($full_path) { if (is_file($full_path)) { require_once 'Minify/Minify.php'; require_once 'Minify/CSS.php'; require_once 'Minify/Converter.php'; require_once 'Minify/Exception.php'; require_once 'Minify/JS.php'; file_put_contents($full_path . '.log', date('c') . " - COMPRESSING CSS\n", FILE_APPEND); $file_size_before = filesize($full_path); $minifier = new MatthiasMullie\Minify\CSS($full_path); $min_path = str_replace('.css', '.min.css', $full_path); $minifier->minify($min_path); $file_size_after = filesize($min_path); file_put_contents($full_path . '.log', date('c') . " - COMPRESSION COMPLETE - " . $this->human_filesize($file_size_before - $file_size_after) . " saved\n", FILE_APPEND); } }
/** * Minify and concatenate CSS * * @param string $group * * @return bool|string */ protected function pipelineCss($group = 'head') { // temporary list of assets to pipeline $temp_css = []; // clear no-pipeline assets lists $this->css_no_pipeline = []; // Compute uid based on assets and timestamp $uid = md5(json_encode($this->css) . $this->css_minify . $this->css_rewrite . $group); $file = $uid . '.css'; $inline_file = $uid . '-inline.css'; $relative_path = "{$this->base_url}{$this->assets_url}/{$file}"; // If inline files exist set them on object if (file_exists($this->assets_dir . $inline_file)) { $this->css_no_pipeline = json_decode(file_get_contents($this->assets_dir . $inline_file), true); } // If pipeline exist return it if (file_exists($this->assets_dir . $file)) { return $relative_path . $this->getTimestamp(); } // Remove any non-pipeline files foreach ($this->css as $id => $asset) { if ($asset['group'] == $group) { if (!$asset['pipeline'] || $asset['remote'] && $this->css_pipeline_include_externals === false) { $this->css_no_pipeline[$id] = $asset; } else { $temp_css[$id] = $asset; } } } //if nothing found get out of here! if (count($temp_css) == 0) { return false; } // Write non-pipeline files out if (!empty($this->css_no_pipeline)) { file_put_contents($this->assets_dir . $inline_file, json_encode($this->css_no_pipeline)); } $css_minify = $this->css_minify; // If this is a Windows server, and minify_windows is false (default value) skip the // minification process because it will cause Apache to die/crash due to insufficient // ThreadStackSize in httpd.conf - See: https://bugs.php.net/bug.php?id=47689 if (strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN' && !$this->css_minify_windows) { $css_minify = false; } // Concatenate files $buffer = $this->gatherLinks($temp_css, CSS_ASSET); if ($css_minify) { $minifier = new \MatthiasMullie\Minify\CSS(); $minifier->add($buffer); $buffer = $minifier->minify(); } // Write file if (strlen(trim($buffer)) > 0) { file_put_contents($this->assets_dir . $file, $buffer); return $relative_path . $this->getTimestamp(); } else { return false; } }
<?php require 'resources/minify/src/Minify.php'; require 'resources/minify/src/CSS.php'; require 'resources/minify/src/JS.php'; require 'resources/minify/src/Exception.php'; require 'resources/path-converter/src/Converter.php'; // var_dump(get_declared_classes()); $minifier = new MatthiasMullie\Minify\CSS('assets/css/all.css'); // we can even add another file, they'll then be // joined in 1 output file // $sourcePath2 = '/path/to/second/source/css/file.css'; $minifier->add("assets/css/team.css"); $minifier->add("assets/css/style.css"); $minifier->add("assets/css/eventsmain.css"); $minifier->add("assets/css/intro.css"); $minifier->add("assets/css/jquery-ui.structure.min.css"); $minifier->add("assets/css/jquery-ui.min.css"); $minifier->add("assets/css/jquery-ui.theme.min.css"); // or we can just add plain CSS // $css = 'body { color: #000000; }'; // $minifier->add($css); // save minified file to disk // $minifiedPath = '/path/to/minified/css/file.css'; // $minifier->minify($minifiedPath); header("Content-type: text/css"); echo $minifier->minify();
/** * Compress each languages * @param string $language The language of source code * @param string $code The source code * @return string Compressed code */ public static function _compressor($language, $code = "", $minInline) { /** * Skip if file size is > 500KB */ if (mb_strlen($code, '8bit') > 500000) { return $code; } else { if ($language == "css") { /** * What kind of css stuff should it convert */ $minifier = new MatthiasMullie\Minify\CSS(); $minifier->add($code); return $minifier->minify(); } else { if ($language == "js") { return JShrink\Minifier::minify($code); } else { if ($language == "html") { $html = new Tinyfier_HTML_Tool(); if ($minInline) { return $html->process($code, array("compress_all" => true)); } else { return $html->process($code, array("compress_all" => false)); } } } } } }