function do_minify($content) { global $config; $c = File::open(__DIR__ . DS . 'states' . DS . 'config.txt')->unserialize(); $url = $config->protocol . $config->host; // Minify HTML if (isset($c['html_minifier'])) { $config->html_minifier = true; Config::set('html_minifier', $config->html_minifier); $content = Converter::detractSkeleton($content); } // Minify URL if (isset($c['url_minifier']) && Text::check($content)->has('="' . $url)) { $content = str_replace(array('="' . $url . '"', '="' . $url . '/', '="' . $url . '?', '="' . $url . '#'), array('="/"', '="/', '="?', '="#'), $content); } // Minify Embedded CSS if (isset($c['css_minifier'])) { $content = preg_replace_callback('#<style(>| .*?>)([\\s\\S]*?)<\\/style>#i', function ($matches) use($config, $c, $url) { $css = Converter::detractShell($matches[2]); if (isset($c['url_minifier'])) { $css = preg_replace('#(?<=[\\s:])(src|url)\\(' . preg_quote($url, '/') . '#', '$1(', $css); } return '<style' . $matches[1] . $css . '</style>'; }, $content); } // Minify Embedded JavaScript if (isset($c['js_minifier'])) { $content = preg_replace_callback('#<script(>| .*?>)([\\s\\S]*?)<\\/script>#i', function ($matches) { $js = Converter::detractSword($matches[2]); return '<script' . $matches[1] . $js . '</script>'; }, $content); } return $content; }
function do_minify($content) { global $config; $c_minify = $config->states->{'plugin_' . md5(File::B(__DIR__))}; $url = $config->protocol . $config->host; // Minify HTML if (isset($c_minify->html_minify)) { Config::set('html_minify', true); $content = Converter::detractSkeleton($content); } // Minify URL if (isset($c_minify->url_minify) && Text::check($content)->has('="' . $url)) { Config::set('url_minify', true); $content = str_replace(array('="' . $url . '"', '="' . $url . '/', '="' . $url . '?', '="' . $url . '#'), array('="/"', '="/', '="?', '="#'), $content); } // Minify Embedded CSS if (isset($c_minify->css_minify)) { Config::set('css_minify', true); $content = preg_replace_callback('#<style(>| .*?>)([\\s\\S]*?)<\\/style>#i', function ($matches) use($config, $c_minify, $url) { $css = Converter::detractShell($matches[2]); if (isset($c_minify->url_minify)) { $css = preg_replace('#(?<=[\\s:])(src|url)\\(' . preg_quote($url, '/') . '#', '$1(', $css); } return '<style' . $matches[1] . $css . '</style>'; }, $content); } // Minify Embedded JavaScript if (isset($c_minify->js_minify)) { Config::set('js_minify', true); $content = preg_replace_callback('#<script(>| .*?>)([\\s\\S]*?)<\\/script>#i', function ($matches) { $js = Converter::detractSword($matches[2]); return '<script' . $matches[1] . $js . '</script>'; }, $content); } return $content; }
public static function merge($path, $name = null, $addon = "", $call = null) { $path = is_string($path) && strpos($path, ';') !== false ? explode(';', $path) : (array) $path; $the_path = ASSET . DS . File::path($name); $the_path_log = SYSTEM . DS . 'log' . DS . 'asset.' . str_replace(array(ASSET . DS, DS), array("", '__'), $the_path) . '.log'; $is_valid = true; if (!file_exists($the_path_log)) { $is_valid = false; } else { $the_path_time = explode("\n", file_get_contents($the_path_log)); if (count($the_path_time) !== count($path)) { $is_valid = false; } else { foreach ($the_path_time as $i => $time) { $p = self::path($path[$i]); if (!file_exists($p) || (int) filemtime($p) !== (int) $time) { $is_valid = false; break; } } } } $time = ""; $content = ""; $e = File::E($name); if (!file_exists($the_path) || !$is_valid) { if (Text::check($e)->in(array('gif', 'jpeg', 'jpg', 'png'))) { foreach ($path as $p) { if (!self::ignored($p)) { $p = self::path($p); if (file_exists($p)) { $time .= filemtime($p) . "\n"; } } } File::write(trim($time))->saveTo($the_path_log); Image::take($path)->merge()->saveTo($the_path); } else { foreach ($path as $p) { if (!self::ignored($p)) { $p = self::path($p); if (file_exists($p)) { $time .= filemtime($p) . "\n"; $c = file_get_contents($p); if (strpos(File::B($p), '.min.') === false) { if (strpos(File::B($the_path), '.min.css') !== false) { $content .= Converter::detractShell($c) . "\n"; } else { if (strpos(File::B($the_path), '.min.js') !== false) { $content .= Converter::detractSword($c) . "\n"; } else { $content .= $c . "\n\n"; } } } else { $content .= $c . "\n\n"; } } } } File::write(trim($time))->saveTo($the_path_log); File::write(trim($content))->saveTo($the_path); } } if (is_null($call)) { $call = Mecha::alter($e, array('css' => 'stylesheet', 'js' => 'javascript', 'gif' => 'image', 'jpeg' => 'image', 'jpg' => 'image', 'png' => 'image')); } return call_user_func_array('self::' . $call, array($the_path, $addon)); }
public static function merge($path, $name = null, $addon = "", $call = null) { $the_path = strpos($name, ROOT) === 0 ? File::path($name) : ASSET . DS . File::path($name); $the_log = LOG . DS . 'asset.' . str_replace(array(ASSET . DS, DS), array("", '__'), $the_path) . '.log'; $is_valid = true; if (!file_exists($the_log)) { $is_valid = false; } else { $the_time = explode("\n", file_get_contents($the_log)); if (count($the_time) !== count($path)) { $is_valid = false; } else { foreach ($the_time as $i => $unix) { $p = self::path($path[$i]); if (!file_exists($p) || (int) filemtime($p) !== (int) $unix) { $is_valid = false; break; } } } } $unix = ""; $content = ""; $e = File::E($name); if (!file_exists($the_path) || !$is_valid) { if (Text::check($e)->in(array('gif', 'jpeg', 'jpg', 'png'))) { foreach ($path as &$p) { $p = Filter::colon('asset:source', $p); if (!self::ignored($p) && ($p = Filter::colon('asset:path', self::path($p, false)))) { $unix .= filemtime($p) . "\n"; } } File::write(trim($unix))->saveTo($the_log); Image::take($path)->merge()->saveTo($the_path); } else { foreach ($path as $p) { $p = Filter::colon('asset:source', $p); if (!self::ignored($p) && ($p = Filter::colon('asset:path', self::path($p, false)))) { $unix .= filemtime($p) . "\n"; $c = Filter::apply('asset:input', file_get_contents($p) . "\n", $p); if (strpos(File::B($p), '.min.') === false) { if (strpos(File::B($the_path), '.min.css') !== false) { $c = Converter::detractShell($c); } else { if (strpos(File::B($the_path), '.min.js') !== false) { $c = Converter::detractSword($c); } else { $c = $c . "\n"; } } $content .= Filter::apply('asset:output', $c, $p); } else { $content .= Filter::apply('asset:output', $c . "\n", $p); } } } File::write(trim($unix))->saveTo($the_log); File::write(trim($content))->saveTo($the_path); } } if (is_null($call)) { $call = Mecha::alter($e, array('css' => 'stylesheet', 'js' => 'javascript', 'gif' => 'image', 'jpeg' => 'image', 'jpg' => 'image', 'png' => 'image')); } return call_user_func('self::' . $call, $the_path, $addon); }