示例#1
0
 /**
  * Convert a .scss or .less files to .css and include it in the page
  *
  * @param mixed $scss_files A string or array of scss files
  * @param string $type
  * @return bool
  */
 public static function Cache($file_array, $type = 'scss')
 {
     global $dataDir;
     //generage the name of the css file from the modified times and content length of each imported file
     $file_array = (array) $file_array;
     $type = strtolower($type);
     $files_hash = \gp\tool::ArrayHash($file_array);
     $list_file = $dataDir . '/data/_cache/' . $type . '_' . $files_hash . '.list';
     if (file_exists($list_file)) {
         $list = explode("\n", file_get_contents($list_file));
         //pop the etag
         $etag = array_pop($list);
         // generate an etag if needed or if logged in
         if (\gp\tool::LoggedIn()) {
             $etag = \gp\tool::FilesEtag($list);
         }
         $compiled_name = $type . '_' . $files_hash . '_' . $etag . '.css';
         $compiled_file = '/data/_cache/' . $compiled_name;
         if (file_exists($dataDir . $compiled_file)) {
             return $compiled_file;
         }
     }
     if ($type == 'less') {
         $compiled = self::ParseLess($file_array);
     } else {
         $compiled = self::ParseScss($file_array);
     }
     if (!$compiled) {
         return false;
     }
     // generate the file name
     $etag = \gp\tool::FilesEtag($file_array);
     $compiled_name = $type . '_' . $files_hash . '_' . $etag . '.css';
     $compiled_file = '/data/_cache/' . $compiled_name;
     // save the cache
     // use the last line for the etag
     $list = $file_array;
     $list[] = $etag;
     $cache = implode("\n", $list);
     if (!\gp\tool\Files::Save($list_file, $cache)) {
         return false;
     }
     //save the css
     if (\gp\tool\Files::Save($dataDir . $compiled_file, $compiled)) {
         return $compiled_file;
     }
     return false;
 }