Exemple #1
0
 /**
  * Save and reuse the results of compiled less files.
  * The first call to Get() will generate css and save it.
  * Subsequent calls to Get() with the same arguments will return the same css filename
  *
  * @param array $less_files Array of .less files to compile
  * @param array $parser_options Array of compiler options
  * @param array $modify_vars Array of variables
  * @return string Name of the css file
  */
 public static function Get($less_files, $parser_options = array(), $modify_vars = array())
 {
     //check $cache_dir
     if (isset($parser_options['cache_dir'])) {
         avada_Less_Cache::$cache_dir = $parser_options['cache_dir'];
     }
     if (empty(avada_Less_Cache::$cache_dir)) {
         throw new Exception('cache_dir not set');
     }
     self::CheckCacheDir();
     $less_files = (array) $less_files;
     //create a file for variables
     if (!empty($modify_vars)) {
         $lessvars = avada_Less_Parser::serializeVars($modify_vars);
         $vars_file = avada_Less_Cache::$cache_dir . 'lessphpvars_' . sha1($lessvars) . '.less';
         if (!file_exists($vars_file)) {
             file_put_contents($vars_file, $lessvars);
         }
         $less_files += array($vars_file => '/');
     }
     // generate name for compiled css file
     $hash = md5(json_encode($less_files));
     $list_file = avada_Less_Cache::$cache_dir . 'lessphp_' . $hash . '.list';
     // check cached content
     if (!isset($parser_options['use_cache']) || $parser_options['use_cache'] === true) {
         if (file_exists($list_file)) {
             $list = explode("\n", file_get_contents($list_file));
             //pop the cached name that should match $compiled_name
             $cached_name = array_pop($list);
             if (!preg_match('/^lessphp_[a-f0-9]+\\.css$/', $cached_name)) {
                 $list[] = $cached_name;
                 $cached_name = false;
             }
             $compiled_name = self::CompiledName($list);
             // if $cached_name != $compiled_name, we know we need to recompile
             if (!$cached_name || $cached_name === $compiled_name) {
                 $output_file = self::OutputFile($compiled_name, $parser_options);
                 if ($output_file && file_exists($output_file)) {
                     @touch($list_file);
                     @touch($output_file);
                     return basename($output_file);
                     // for backwards compatibility, we just return the name of the file
                 }
             }
         }
     }
     $compiled = self::Cache($less_files, $parser_options);
     if (!$compiled) {
         return false;
     }
     $compiled_name = self::CompiledName($less_files);
     $output_file = self::OutputFile($compiled_name, $parser_options);
     //save the file list
     $list = $less_files;
     $list[] = $compiled_name;
     $cache = implode("\n", $list);
     file_put_contents($list_file, $cache);
     //save the css
     file_put_contents($output_file, $compiled);
     //clean up
     self::CleanCache();
     return basename($output_file);
 }
Exemple #2
0
 /**
  * Allows a user to set variables values
  * @param array $vars
  * @return avada_Less_Parser
  */
 public function ModifyVars($vars)
 {
     $this->input = avada_Less_Parser::serializeVars($vars);
     $this->_parse();
     return $this;
 }