Exemplo n.º 1
0
Arquivo: js.php Projeto: pihizi/qf
 static function load($js, $params = NULL)
 {
     static $_lamda_cache;
     $output = '';
     if (is_array($js)) {
         foreach ($js as $j) {
             $output .= self::load($j, $params);
         }
         return $output;
     }
     $output = '<script type="text/javascript">(function(){';
     $js_key = md5($js);
     $params = (array) $params;
     ksort($params);
     if (!isset($_lamda_cache[$js_key])) {
         list($category, $path) = explode(':', $js, 2);
         if (!$path) {
             $path = $category;
             $category = NULL;
         }
         $path = PRIVATE_BASE . 'js/' . $path . '.js';
         $path = Core::file_exists($path, $category);
         if (!$path) {
             return '';
         }
         $mtime = filemtime($path);
         $lamda = 'jslamda_' . $js_key;
         $prefix_path = Config::get('system.tmp_dir') . 'js/';
         $locale = Config::get('system.locale');
         if ($locale) {
             $prefix_path .= $locale . '_';
         }
         $mode = Config::get('page.js_mode');
         if ($mode) {
             $prefix_path .= $mode . '_';
         }
         $cache_file = $prefix_path . $lamda;
         $re_cache = TRUE;
         if (file_exists($cache_file)) {
             $cache_mtime = filemtime($cache_file);
             if ($cache_mtime > $mtime) {
                 $re_cache = FALSE;
             }
         } else {
             File::check_path($cache_file);
         }
         if ($re_cache) {
             $cache = 'Q.' . $lamda . '=function(_oO){';
             $i = 0;
             foreach ($params as $k => $v) {
                 $cache .= sprintf('var %s = _oO[%d]; ', $k, $i);
                 $i++;
             }
             $cache .= JS::format(@file_get_contents($path));
             $cache .= '};';
             @file_put_contents($cache_file, $cache);
         } else {
             $cache = @file_get_contents($cache_file);
         }
         $output .= $cache;
         $_lamda_cache[$js_key] = $lamda;
     } else {
         $lamda = $_lamda_cache[$js_key];
     }
     $vars = array();
     foreach ($params as $k => $v) {
         $vars[] = $v;
     }
     $output .= 'Q.' . $lamda . '(' . json_encode($vars) . ');';
     $output .= '})();</script>';
     return $output;
 }