示例#1
0
 public function minify($group, $assets)
 {
     $combined_assets = array();
     foreach ($assets as $name => $data) {
         asort($data['attributes']);
         $attributes_string = json_encode($data['attributes']);
         $combined_assets[$attributes_string][$name] = $data;
     }
     $assets_html = '';
     $compile = false;
     foreach ($combined_assets as $attributes_string => $assets) {
         $files_to_compile = array();
         $output_files = array();
         foreach ($assets as $name => $data) {
             $file = path('public') . $data['source'];
             if (!File::exists($file)) {
                 throw new \Exception('The Asset you are trying to compress does not exist (' . $file . ')');
             }
             $output_files[] = File::modified($file);
             $files_to_compile[] = $data['source'];
         }
         $output_file = md5(implode('', $output_files)) . '.' . ($group == 'script' ? 'js' : 'css');
         if (!count($output_files)) {
             return;
         }
         if (!file_exists($this->config['cache_dir_path'] . $output_file)) {
             $compile = true;
         } else {
             foreach ($files_to_compile as $file) {
                 if (File::modified($this->config['cache_dir_path'] . $output_file) < File::modified(path('public') . $file)) {
                     $compile = true;
                 }
             }
         }
         $method = 'minify_' . $group;
         $assets_html .= $this->{$method}($assets, $data['attributes'], $files_to_compile, $output_file, $compile);
     }
     return $assets_html;
 }
示例#2
0
 protected function minify_script($assets, $attributes, $files_to_compile, $output_file, $compile)
 {
     if ($compile) {
         $scripts_to_minify = array();
         foreach ($files_to_compile as $file) {
             $scripts_to_minify[] = '--js=' . path('public') . $file;
         }
         $scripts_to_minify = implode(' ', $scripts_to_minify);
         $java = 'java';
         // find java binary
         foreach ($this->config['java_binary_path_overrides'] as $java_bin_path) {
             if (file_exists($java_bin_path)) {
                 $java = $java_bin_path;
             }
         }
         // generate command line
         $jar = '-jar ' . __DIR__ . '/drivers/closure-compiler/compiler.jar';
         $out_script = '--js_output_file=' . $this->config['cache_dir_path'] . $output_file;
         // red team, go!
         exec("{$java} {$jar} {$scripts_to_minify} {$out_script}");
     }
     return HTML::script($this->config['cache_dir'] . $output_file . '?t=' . File::modified($this->config['cache_dir_path'] . $output_file), $attributes);
 }