/** * New resource file update handler. * * @param string $type Resource type(extension) * @param string $content Resource content */ public function renderer($type, &$content) { // If CSS resource has been updated if ($type === 'css') { // Read updated CSS resource file and compile it $content = CSSMin::process($content); } elseif ($type === 'js') { $content = JSMin::minify($content); } }
/** @see ModuleConnector::init() */ public function init(array $params = array()) { // Pointer to resourcerouter $rr = m('resourcer'); try { if (isset($rr->updated['js'])) { // Запишем новое содержание JS file_put_contents($rr->updated['js'], JSMin::minify(file_get_contents($rr->updated['js']))); } if (isset($rr->updated['css'])) { // Запишем новое содержание JS file_put_contents($rr->updated['css'], CSSMin::process(file_get_contents($rr->updated['css']))); } } catch (Exception $e) { e('Error minifying resource: ' . $e->getMessage()); } // Вызовем родительский метод parent::init($params); }
/** * Minify Javascript * * @param string $js Javascript to be minified * @return string */ public static function minify($js) { // look out for syntax like "++ +" and "- ++" $p = '\\+'; $m = '\\-'; if (preg_match("/([{$p}{$m}])(?:\\1 [{$p}{$m}]| (?:{$p}{$p}|{$m}{$m}))/", $js)) { // likely pre-minified and would be broken by JSMin return $js; } $jsmin = new JSMin($js); return $jsmin->min(); }