Example #1
0
 public function action_js()
 {
     $group = (string) $this->request->param('group');
     if (empty($group)) {
         $group = 'default';
     }
     if (!($content = Kohana::cache('minify::js::' . $group))) {
         $path = isset($this->_config['js'][$group]['path']) ? $this->_config['js'][$group]['path'] : '';
         $files = isset($this->_config['js'][$group]['files']) ? $this->_config['js'][$group]['files'] : array();
         if (!is_array($files)) {
             $files = array();
         }
         $content = '';
         foreach ($files as $file) {
             $content .= file_get_contents($path . DIRECTORY_SEPARATOR . $file) . "\n";
         }
         if (!empty($content)) {
             $pack = isset($this->_config['js'][$group]['packer']) ? (bool) $this->_config['js'][$group]['packer'] : false;
             $is_min = isset($this->_config['js'][$group]['is_min']) ? (bool) $this->_config['js'][$group]['is_min'] : false;
             if ($pack) {
                 $packer = new Minify_Packer($content, 'Normal', TRUE, FALSE);
                 $content = $packer->pack();
             } else {
                 if (!$is_min) {
                     $minifier = isset($this->_config['js'][$group]['minifier']) ? $this->_config['js'][$group]['minifier'] : '';
                     if (!empty($minifier) && class_exists($minifier)) {
                         $class = new ReflectionClass($minifier);
                         $js_min = $class->newInstance($content);
                         $content = $class->getMethod('min')->invoke($js_min);
                     } else {
                         $content = Minify_JS::minify($content);
                     }
                 }
             }
         }
         if ((bool) $this->_config['cache']) {
             Kohana::cache('minify::js::' . $group, $content, (int) $this->_config['cache_lifetime']);
         }
     }
     $this->response->body($content);
 }
Example #2
0
 private function _bootstrap($packed, $keywords)
 {
     $_ENCODE = $this->_safe_reg_exp('$encode\\($count\\)');
     // $packed: the packed script
     $packed = "'" . $this->_escape($packed) . "'";
     // $ascii: base for encoding
     $ascii = min(count($keywords['sorted']), $this->_encoding);
     if ($ascii == 0) {
         $ascii = 1;
     }
     // $count: number of words contained in the script
     $count = count($keywords['sorted']);
     // $keywords: list of words contained in the script
     foreach ($keywords['protected'] as $i => $value) {
         $keywords['sorted'][$i] = '';
     }
     // convert from a string to an array
     ksort($keywords['sorted']);
     $keywords = "'" . implode('|', $keywords['sorted']) . "'.split('|')";
     $encode = $this->_encoding > 62 ? '_encode95' : $this->_get_encoder($ascii);
     $encode = $this->_get_js_function($encode);
     $encode = preg_replace('/_encoding/', '$ascii', $encode);
     $encode = preg_replace('/arguments\\.callee/', '$encode', $encode);
     $inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : '');
     // $decode: code snippet to speed up decoding
     if ($this->_fast_decode) {
         // create the decoder
         $decode = $this->_get_js_function('_decodeBody');
         if ($this->_encoding > 62) {
             $decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
         } else {
             if ($ascii < 36) {
                 $decode = preg_replace($_ENCODE, $inline, $decode);
             }
         }
         // special case: when $count==0 there are no keywords. I want to keep
         //  the basic shape of the unpacking funcion so i'll frig the code...
         if ($count == 0) {
             $decode = preg_replace($this->_safe_reg_exp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
         }
     }
     // boot function
     $unpack = $this->_get_js_function('_unpack');
     if ($this->_fast_decode) {
         // insert the decoder
         $this->buffer = $decode;
         $unpack = preg_replace_callback('/\\{/', array(&$this, '_insert_fast_decode'), $unpack, 1);
     }
     $unpack = preg_replace('/"/', "'", $unpack);
     if ($this->_encoding > 62) {
         // get rid of the word-boundaries for regexp matches
         $unpack = preg_replace('/\'\\\\\\\\b\'\\s*\\+|\\+\\s*\'\\\\\\\\b\'/', '', $unpack);
     }
     if ($ascii > 36 || $this->_encoding > 62 || $this->_fast_decode) {
         // insert the encode function
         $this->buffer = $encode;
         $unpack = preg_replace_callback('/\\{/', array(&$this, '_insert_fast_encode'), $unpack, 1);
     } else {
         // perform the encoding inline
         $unpack = preg_replace($_ENCODE, $inline, $unpack);
     }
     // pack the boot function too
     $unpack_packer = new Minify_Packer($unpack, 0, FALSE, TRUE);
     $unpack = $unpack_packer->pack();
     // arguments
     $params = array($packed, $ascii, $count, $keywords);
     if ($this->_fast_decode) {
         $params[] = 0;
         $params[] = '{}';
     }
     $params = implode(',', $params);
     // the whole thing
     return 'eval(' . $unpack . '(' . $params . "))\n";
 }