示例#1
0
 public static function compressCSS()
 {
     //function to compress css files
     require_once __DIR__ . '/classes/cssmin.php';
     $doc = JFactory::getDocument();
     $app = JFactory::getApplication();
     $cachetime = $app->get('cachetime', 15);
     $all_stylesheets = $doc->_styleSheets;
     $cache_path = JPATH_CACHE . '/com_templates/templates/' . self::getTemplate();
     $stylesheets = array();
     $root_url = JURI::root(true);
     $minifiedCode = '';
     $md5sum = '';
     //Check all local stylesheets
     foreach ($all_stylesheets as $key => $value) {
         $css_file = str_replace($root_url, JPATH_ROOT, $key);
         global $absolute_url;
         $absolute_url = $key;
         //absoulte path of each css file
         if (JFile::exists($css_file)) {
             $stylesheets[] = $key;
             $md5sum .= md5($key);
             $compressed = CSSMinify::process(JFile::read($css_file));
             $fixUrl = preg_replace_callback('/url\\(([^\\)]*)\\)/', function ($matches) {
                 $url = str_replace(array('"', '\''), '', $matches[1]);
                 global $absolute_url;
                 $base = dirname($absolute_url);
                 while (preg_match('/^\\.\\.\\//', $url)) {
                     $base = dirname($base);
                     $url = substr($url, 3);
                 }
                 $url = $base . '/' . $url;
                 return "url('{$url}')";
             }, $compressed);
             $minifiedCode .= "/*------ " . JFile::getName($css_file) . " ------*/\n" . $fixUrl . "\n\n";
             //add file name to compressed css
             unset($doc->_styleSheets[$key]);
             //Remove sripts
         }
     }
     //Compress All stylesheets
     if ($minifiedCode) {
         if (!JFolder::exists($cache_path)) {
             JFolder::create($cache_path, 0755);
         } else {
             $file = $cache_path . '/' . md5($md5sum) . '.css';
             if (!JFile::exists($file)) {
                 JFile::write($file, $minifiedCode);
             } else {
                 if (filesize($file) == 0 || filemtime($file) + $cachetime * 60 < time()) {
                     JFile::write($file, $minifiedCode);
                 }
             }
             $doc->addStylesheet(JURI::base(true) . '/cache/com_templates/templates/' . self::getTemplate() . '/' . md5($md5sum) . '.css');
         }
     }
     return;
 }
示例#2
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options (currently ignored)
  * 
  * @return string
  */
 public static function process($css, $options = array())
 {
     $obj = new CSSMinify($options);
     return $obj->_process($css);
 }
示例#3
0
	function compressCSS() {//function to compress css files
		require_once (dirname(__FILE__).DS."class.cssminify.php");//include cssminify class
		$css_files = array();
		$cache_time = $this->getParam('cache_time');//Cache time in minute
		$helix_folder='helix_assets';//path of cache where to save
        $output = array();
        $md5sum = null;
		
        $csss = $this->API->_styleSheets;//get all CSS
		
        foreach ($csss as $fileSrc => $fileAttr) {//separate path from attribute
            $md5sum .= md5($fileSrc);
            $css_files[] = $fileSrc;
        }
		
        if (!is_writable(JPATH_CACHE)) {//check for cache path writable, if not return
            return;
        } 
		
		if (is_writable(JPATH_CACHE)) {//add helix_assets folder under cache directory
			if (!file_exists(JPATH_CACHE.DS.$helix_folder)) mkdir (JPATH_CACHE.DS.$helix_folder);
		}

        if (count($css_files) > 0) {//if any css file available
            $cache_name = md5($md5sum) . ".css";
            $cache_path = JPATH_CACHE . DS . $helix_folder . DS . $cache_name;
			$diff=false;

            //see if file is stale
            if (!file_exists($cache_path)) {
				$diff=true;   
            } elseif(filesize($cache_path) == 0 || ((filemtime($cache_path) + $cache_time * 60) < time())) {
				$diff=true; 
            }

			foreach ($css_files as $files) {
				unset($this->API->_styleSheets[$files]); //Remove all css files from the header
			}
			
            if ($diff) {
                $output = '';
                foreach ($css_files as $files) {
					$filepath = $this->realPath($files);//convert to real url
					
				    global $absolute_url;
					$absolute_url = $files;//absoulte path of each css file
                    if (JFile::exists($filepath)) {
                        $css = CSSMinify::process(JFile::read($filepath));//read and compress css files
						
						$css=preg_replace_callback('/url\(([^\)]*)\)/', array($this, 'replaceUrl'), $css);//call replaceUrl function to set absolute value to the urls
						
                        $output .= "/*------ " . $files . " ------*/\n" . $css . "\n\n";//add file name to compressed css
                    }
                }
                JFile::write($cache_path, $output);//write cache to the joomla cache directory
            }
			
            $cache_url = $this->API->baseurl . "/cache/" . $helix_folder . '/' . $cache_name;//path of css cache to add as stylesheet
            $this->API->addStyleSheet($cache_url);//add stylesheet to the header
        }
    }
示例#4
0
 private static function compressCSS()
 {
     //function to compress css files
     self::getInstance()->Import('core/classes/cssmin.php');
     $css_files = array();
     $cache_time = self::getInstance()->Param('cache_time');
     //Cache time in minute
     $helix_folder = 'helix_assets';
     //path of cache where to save
     $output = array();
     $md5sum = null;
     $csss = self::getInstance()->getDocument('_styleSheets');
     //get all css
     foreach ($csss as $fileSrc => $fileAttr) {
         //separate path from attribute
         $md5sum .= md5($fileSrc);
         $css_files[] = $fileSrc;
     }
     if (!is_writable(JPATH_CACHE)) {
         //check for cache path writable, if not return
         return;
     }
     if (is_writable(JPATH_CACHE)) {
         //add helix_assets folder under cache directory
         if (!file_exists(JPATH_CACHE . DIRECTORY_SEPARATOR . $helix_folder)) {
             mkdir(JPATH_CACHE . DIRECTORY_SEPARATOR . $helix_folder);
         }
     }
     if (count($css_files) > 0) {
         //if any css file available
         $cache_name = md5($md5sum) . ".css";
         $cache_path = JPATH_CACHE . DIRECTORY_SEPARATOR . $helix_folder . DIRECTORY_SEPARATOR . $cache_name;
         $diff = false;
         //see if file is stale
         if (!file_exists($cache_path)) {
             $diff = true;
         } elseif (filesize($cache_path) == 0 || filemtime($cache_path) + $cache_time * 60 < time()) {
             $diff = true;
         }
         foreach ($css_files as $files) {
             $external = self::getInstance()->isExternalURL($files);
             if ($external) {
                 continue;
             }
             unset(self::getInstance()->getDocument()->_styleSheets[$files]);
             //Remove all css files from the header
         }
         if ($diff) {
             $output = '';
             foreach ($css_files as $files) {
                 $external = self::getInstance()->isExternalURL($files);
                 if ($external) {
                     continue;
                 }
                 $filepath = self::getInstance()->realPath($files);
                 //convert to real url
                 global $absolute_url;
                 $absolute_url = $files;
                 //absoulte path of each css file
                 if (JFile::exists($filepath)) {
                     $css = CSSMinify::process(JFile::read($filepath));
                     //read and compress css files
                     $css = preg_replace_callback('/url\\(([^\\)]*)\\)/', array(self::getInstance(), 'replaceUrl'), $css);
                     //call replaceUrl function to set absolute value to the urls
                     $output .= "/*------ " . $files . " ------*/\n" . $css . "\n\n";
                     //add file name to compressed css
                 }
             }
             JFile::write($cache_path, $output);
             //write cache to the joomla cache directory
         }
         $cache_url = self::getInstance()->baseURL() . "/cache/" . $helix_folder . '/' . $cache_name;
         //path of css cache to add as stylesheet
         self::getInstance()->document->addStyleSheet($cache_url);
         //add stylesheet to the header
     }
 }