示例#1
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options (currently ignored)
  * 
  * @return string
  */
 public static function process($css, $options = array())
 {
     $obj = new styles_compressor($options);
     return $obj->_process($css);
 }
示例#2
0
文件: Assets.php 项目: escproxy/RPS
 /**
  * Outputs data
  *
  * @access	public
  * @param	mixed	$asset		The asset
  * @param	string	$group		Group
  * @param	bool	$default	If the group should be the default __{$asset}__ only
  * @return	void
  */
 public function output($asset = null, $group = null, $default = false, $just_link = false)
 {
     $this->super->benchmark->mark("assets::output(" . $asset . "/" . $group . ")_start");
     $output = '';
     $files = array();
     $file_names = array();
     if (is_null($asset)) {
         return;
     }
     // Override $group
     if ($default == true) {
         $group = $this->default_group[$asset];
     }
     // All files?
     if (is_null($group)) {
         if (isset($this->queue[$asset])) {
             foreach ($this->queue[$asset] as $group => $details) {
                 $output .= $this->output($asset, $group, $default, $just_link);
             }
             return $output;
         }
     } else {
         if (!isset($this->outputted)) {
             $this->outputted = array();
         }
         if (!isset($this->outputted[$asset])) {
             $this->outputted[$asset] = array();
         }
         if (in_array($group, $this->outputted[$asset])) {
             //return;
         } else {
             if (isset($this->queue[$asset][$group])) {
                 $files = $this->queue[$asset][$group];
                 $this->outputted[$asset][] = $group;
             } else {
                 return;
             }
         }
     }
     if (empty($files)) {
         return;
     }
     $file_names = array();
     // Try and find the actual files:
     foreach ($files as $index => $value) {
         $found = false;
         $file = $value['file'];
         foreach ($this->path[$asset] as $location) {
             if (file_exists($location . DIRECTORY_SEPARATOR . $file)) {
                 $found = true;
                 $files[$index]['path'] = reduce_double_slashes($location . DIRECTORY_SEPARATOR);
             }
         }
         // Uh oh
         if (!isset($files[$index]['path'])) {
             // Check if a module has been specified
             $path = '';
             $module = $this->super->router->get_module();
             // Is this in a sub-folder? If so, parse out the filename and path.
             if (($last_slash = strrpos($file, '/')) !== FALSE) {
                 $path = substr($file, 0, ++$last_slash - 1);
                 $file = substr($file, $last_slash);
             }
             $_file = modules::file_path($module, $this->dir[$asset] . DIRECTORY_SEPARATOR . $path, $file);
             if (is_null($_file)) {
                 $segments = explode(DIRECTORY_SEPARATOR, $path);
                 $module = $segments[0];
                 array_shift($segments);
                 $path = implode(DIRECTORY_SEPARATOR, $segments);
                 $_file = modules::file_path($module, $this->dir[$asset] . DIRECTORY_SEPARATOR . $path, $file);
             }
             if (!is_null($_file)) {
                 $found = true;
                 $files[$index]['file'] = basename($_file);
                 $files[$index]['path'] = reduce_double_slashes(dirname($_file)) . DIRECTORY_SEPARATOR;
             }
         }
         if ($found) {
             $file_names[] = $file;
         } else {
             unset($files[$index]);
         }
     }
     $cache_name = sha1(implode(',', $file_names)) . '.' . current($this->config['file_types'][$asset]);
     $cache_path = reduce_double_slashes($this->config['cache_path'] . DIRECTORY_SEPARATOR . $this->config['paths'][$asset] . DIRECTORY_SEPARATOR . $cache_name);
     // Keep regenerating cache if development mode
     if (ENVIRONMENT == 'development' || !$this->super->cache->file->get($cache_path)) {
         $output = '';
         $time = time();
         switch ($asset) {
             case 'styles':
                 $finished = array();
                 if ($this->config['enable_less'] == true) {
                     $this->_init_lesscss();
                     foreach ($files as $file) {
                         //var_dump($this->path[$asset] . $file['file']);
                         // Search for less files..
                         if (preg_match('/\\.less/i', $file['file'])) {
                             $output .= $this->_lesscss->compileFile($file['path'] . $file['file']);
                             /*
                             $file_names = array();
                             foreach($this->_less->imported as $file) {
                             	$file_names[] = end(explode('/', $file));
                             }
                             */
                             $finished[$asset][] = $file['file'];
                         }
                     }
                 }
                 foreach ($files as $file) {
                     if (!in_array($file['file'], $finished[$asset])) {
                         $output .= file_get_contents($file['path'] . $file['file']) . "\n";
                     }
                 }
                 if ($this->config['minify_styles'] == true) {
                     $this->_init_styles_compressor();
                     $output = styles_compressor::process($output);
                 }
                 break;
             case 'scripts':
                 foreach ($files as $file) {
                     $output .= file_get_contents($file['path'] . $file['file']) . "\n";
                 }
                 if ($this->config['minify_scripts'] == true) {
                     $this->_init_scripts_compressor();
                     $output = scripts_compressor::process($output);
                 }
                 if ($this->config['minify_scripts_closure'] == true) {
                     $this->_init_scripts_closure();
                     $output = $this->_closure->addCode($output)->simpleMode()->write();
                 }
                 break;
         }
         $output = "/**\n" . " * " . $cache_name . "\n *\n" . (ENVIRONMENT !== 'production' ? " * @includes\t" . implode(",\n *\t\t", $file_names) . "\n *\n" : '') . " * @version\tv" . $time . "\n" . " * @copyright\t(c) " . date('Y') . "\n" . " */\n" . $output;
         $this->super->cache->file->save($cache_path, $output, 0, 0775);
     } else {
         $meta = $this->super->cache->file->get_metadata($cache_path);
         $time = $meta['mtime'];
     }
     $link = prep_url($this->url[$asset] . $cache_name . ($this->config['version_date'] == true ? '?v' . $time : ''));
     if ($just_link == true) {
         $return = $link;
     } else {
         switch ($asset) {
             case 'styles':
                 $return = link_tag($link);
                 break;
             case 'scripts':
                 $return = script_tag($link);
                 break;
             default:
                 $return = '';
                 break;
         }
     }
     $this->super->benchmark->mark("assets::output(" . $asset . "/" . $group . ")_end");
     return $return;
 }