/** * Minify Javascript. * * @param string $js Javascript to be minified * @return string */ public static function minify($js) { $jsmin = new JSmin($js); return $jsmin->min(); }
protected function _regenerateAssets() { $this->header('Regenerating assets'); $version_file = APP_ROOT . 'app/ASSETSVERSION'; $assetsversion = (int) file_get_contents($version_file); $assetsversion++; file_put_contents($version_file, $assetsversion); $assetsfolder = APP_ROOT . 'public/assets/'; $jsfolder = $assetsfolder . 'js/'; foreach (FileUtils::getFolders($jsfolder) as $folder) { if (preg_match('`^\\.`', $folder)) { continue; } $this->line('Regenerating ' . $folder . ' javascript asset'); $out = ''; foreach (FileUtils::getAllFiles($jsfolder . $folder) as $file) { $out .= file_get_contents($file) . "\n"; } if (MODE == 'production') { $out = JSmin::minify($out); } $version = self::getOption('assetsurlrewriting') ? '' : $assetsversion; file_put_contents(APP_ROOT . 'public/cache/' . $folder . $version . '.js', $out); } // css $cssfolder = $assetsfolder . 'css/'; foreach (FileUtils::getFolders($cssfolder) as $folder) { if (preg_match('`^\\.`', $folder)) { continue; } $this->line('Regenerating ' . $folder . ' CSS asset'); $out = ''; foreach (FileUtils::getAllFiles($cssfolder . $folder) as $file) { $out .= file_get_contents($file) . "\n"; } if (MODE == 'production') { $out = CSSmin::minify($out); } $version = self::getOption('assetsurlrewriting') ? '' : $assetsversion; file_put_contents(APP_ROOT . 'public/cache/' . $folder . $version . '.css', $out); } }
/** * Extends list of javascript commands to execute * * @param string javascript code */ public static final function js($js, $del_on_loc = true) { if (!is_string($js) || strlen($js) == 0) { return false; } $js = rtrim($js, ';'); if (STRIP_OUTPUT) { require_once 'libs/minify/JSMin.php'; $js = JSmin::minify($js); } $js_def = array($js, $del_on_loc); if (DEBUG_JS && function_exists('debug_backtrace')) { $arg = false; if (version_compare(PHP_VERSION, '5.3.6', '>=')) { $arg = DEBUG_BACKTRACE_IGNORE_ARGS; } $bt = debug_backtrace($arg); array_shift($bt); // remove first, because it's this function $debug_str = ''; $limit = (int) DEBUG_JS; while ($limit--) { $x = array_shift($bt); if (!$x) { break; } $file =& $x['file']; $line =& $x['line']; $func =& $x['function']; $debug_str .= "{$func} ({$file}:{$line})"; if ($limit) { $debug_str .= ' <-- '; } } $js_def[] = $debug_str; } self::$jses[] = $js_def; return true; }
protected static function _removeScriptCB($m) { $openScript = $m[1]; $js = $m[2]; // remove HTML comments (and ending "//" if present) $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js); // remove CDATA section markers $js = self::_removeCdata($js); // minify $jsmin = new JSmin($js); $js = $jsmin->min(); return self::_reservePlace(self::_needsCdata($js) ? "{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>" : "{$openScript}{$js}</script>"); }
/** * Minify the JavaScript * * @param string $content * * @return string */ public function minifyJs($content) { return \JSmin::minify($content); }