Compare commits

...

1 Commits
master ... dev

Author SHA1 Message Date
Bachir Soussi Chiadmi
1c16557c5f less module modification, dont know if i have to keep it 2016-10-13 11:09:58 +02:00
9 changed files with 10675 additions and 22 deletions

View File

@ -0,0 +1,316 @@
<?php
require_once( dirname(__FILE__).'/Version.php');
/**
* Utility for handling the generation and caching of css files
*
* @package Less
* @subpackage cache
*
*/
class Less_Cache{
// directory less.php can use for storing data
public static $cache_dir = false;
// prefix for the storing data
public static $prefix = 'lessphp_';
// prefix for the storing vars
public static $prefix_vars = 'lessphpvars_';
// specifies the number of seconds after which data created by less.php will be seen as 'garbage' and potentially cleaned up
public static $gc_lifetime = 604800;
/**
* 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']) ){
Less_Cache::$cache_dir = $parser_options['cache_dir'];
}
if( empty(Less_Cache::$cache_dir) ){
throw new Exception('cache_dir not set');
}
if( isset($parser_options['prefix']) ){
Less_Cache::$prefix = $parser_options['prefix'];
}
if( empty(Less_Cache::$prefix) ){
throw new Exception('prefix not set');
}
if( isset($parser_options['prefix_vars']) ){
Less_Cache::$prefix_vars = $parser_options['prefix_vars'];
}
if( empty(Less_Cache::$prefix_vars) ){
throw new Exception('prefix_vars not set');
}
self::CheckCacheDir();
$less_files = (array)$less_files;
//create a file for variables
if( !empty($modify_vars) ){
$lessvars = Less_Parser::serializeVars($modify_vars);
$vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . 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 = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list';
// check cached content
if( !isset($parser_options['use_cache']) || $parser_options['use_cache'] === true ){
if( file_exists($list_file) ){
self::ListFiles($list_file, $list, $cached_name);
$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);
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);
}
/**
* Force the compiler to regenerate the cached css file
*
* @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 Regen( $less_files, $parser_options = array(), $modify_vars = array() ){
$parser_options['use_cache'] = false;
return self::Get( $less_files, $parser_options, $modify_vars );
}
public static function Cache( &$less_files, $parser_options = array() ){
// get less.php if it exists
$file = dirname(__FILE__) . '/Less.php';
if( file_exists($file) && !class_exists('Less_Parser') ){
require_once($file);
}
$parser_options['cache_dir'] = Less_Cache::$cache_dir;
$parser = new Less_Parser($parser_options);
// combine files
foreach($less_files as $file_path => $uri_or_less ){
//treat as less markup if there are newline characters
if( strpos($uri_or_less,"\n") !== false ){
$parser->Parse( $uri_or_less );
continue;
}
$parser->ParseFile( $file_path, $uri_or_less );
}
$compiled = $parser->getCss();
$less_files = $parser->allParsedFiles();
return $compiled;
}
private static function OutputFile( $compiled_name, $parser_options ){
//custom output file
if( !empty($parser_options['output']) ){
//relative to cache directory?
if( preg_match('#[\\\\/]#',$parser_options['output']) ){
return $parser_options['output'];
}
return Less_Cache::$cache_dir.$parser_options['output'];
}
return Less_Cache::$cache_dir.$compiled_name;
}
private static function CompiledName( $files ){
//save the file list
$temp = array(Less_Version::cache_version);
foreach($files as $file){
$temp[] = filemtime($file)."\t".filesize($file)."\t".$file;
}
return Less_Cache::$prefix.sha1(json_encode($temp)).'.css';
}
public static function SetCacheDir( $dir ){
Less_Cache::$cache_dir = $dir;
}
public static function CheckCacheDir(){
Less_Cache::$cache_dir = str_replace('\\','/',Less_Cache::$cache_dir);
Less_Cache::$cache_dir = rtrim(Less_Cache::$cache_dir,'/').'/';
if( !file_exists(Less_Cache::$cache_dir) ){
if( !mkdir(Less_Cache::$cache_dir) ){
throw new Less_Exception_Parser('Less.php cache directory couldn\'t be created: '.Less_Cache::$cache_dir);
}
}elseif( !is_dir(Less_Cache::$cache_dir) ){
throw new Less_Exception_Parser('Less.php cache directory doesn\'t exist: '.Less_Cache::$cache_dir);
}elseif( !is_writable(Less_Cache::$cache_dir) ){
throw new Less_Exception_Parser('Less.php cache directory isn\'t writable: '.Less_Cache::$cache_dir);
}
}
/**
* Delete unused less.php files
*
*/
public static function CleanCache(){
static $clean = false;
if( $clean ){
return;
}
$files = scandir(Less_Cache::$cache_dir);
if( $files ){
$check_time = time() - self::$gc_lifetime;
foreach($files as $file){
// don't delete if the file wasn't created with less.php
if( strpos($file,Less_Cache::$prefix) !== 0 ){
continue;
}
$full_path = Less_Cache::$cache_dir.'/'.$file;
// make sure the file still exists
// css files may have already been deleted
if( !file_exists($full_path) ){
continue;
}
$mtime = filemtime($full_path);
// don't delete if it's a relatively new file
if( $mtime > $check_time ){
continue;
}
$parts = explode('.',$file);
$type = array_pop($parts);
// delete css files based on the list files
if( $type === 'css' ){
continue;
}
// delete the list file and associated css file
if( $type === 'list' ){
self::ListFiles($full_path, $list, $css_file_name);
if( $css_file_name ){
$css_file = Less_Cache::$cache_dir.'/'.$css_file_name;
if( file_exists($css_file) ){
unlink($css_file);
}
}
}
unlink($full_path);
}
}
$clean = true;
}
/**
* Get the list of less files and generated css file from a list file
*
*/
static function ListFiles($list_file, &$list, &$css_file_name ){
$list = explode("\n",file_get_contents($list_file));
//pop the cached name that should match $compiled_name
$css_file_name = array_pop($list);
if( !preg_match('/^' . Less_Cache::$prefix . '[a-f0-9]+\.css$/',$css_file_name) ){
$list[] = $css_file_name;
$css_file_name = false;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
<?php
/**
* Release numbers
*
* @package Less
* @subpackage version
*/
class Less_Version{
const version = '1.7.0.3'; // The current build number of less.php
const less_version = '1.7'; // The less.js version that this build should be compatible with
const cache_version = '170'; // The parser cache version
}

View File

@ -48,14 +48,17 @@ class LessAutoprefixer {
$version = NULL;
try {
if (function_exists('proc_open')) {
$version_response = self::create(NULL)->proc_open(array('--version'));
try {
$version = preg_replace('/.*?([\d\.]+).*/', '$1', $version_response);
}
catch (Exception $e) {
$version_response = self::create(NULL)->proc_open(array('--version'));
$version = preg_replace('/.*?([\d\.]+).*/', '$1', $version_response);
}
catch (Exception $e) {
}
}
return $version;

View File

@ -80,14 +80,17 @@ class Lessjs {
$version = NULL;
try {
if (function_exists('proc_open')) {
$version_response = self::create(NULL)->proc_open(array('--version'));
try {
$version = preg_replace('/.*?([\d\.]+).*/', '$1', $version_response);
}
catch (Exception $e) {
$version_response = self::create(NULL)->proc_open(array('--version'));
$version = preg_replace('/.*?([\d\.]+).*/', '$1', $version_response);
}
catch (Exception $e) {
}
}
return $version;

View File

@ -98,9 +98,4 @@ abstract class LessEngine implements LessEngineInterface {
return $this->dependencies;
}
/**
* {@inheritdoc}
*/
abstract public function compile();
}

View File

@ -10,6 +10,16 @@
*/
function less_settings_form($form, &$form_state) {
if (!function_exists('proc_open')) {
$message_vars = array(
'@proc_open_url' => url('http://php.net/manual/en/function.proc-open.php'),
'@disable_functions_url' => url('http://php.net/manual/en/ini.core.php#ini.disable-functions'),
);
drupal_set_message(t('PHP function <a href="@proc_open_url">proc_open()</a> is currently <a href="@disable_functions_url">disabled</a>. You will be unable to less.js or Autoprefixer.', $message_vars), 'warning');
}
$form['less_flush'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
@ -66,7 +76,7 @@ function less_settings_form($form, &$form_state) {
'#type' => 'checkbox',
'#title' => t('Use @name - <a href="@vendor_url">@vendor_url</a>', array('@name' => $lessautoprefixer_library['name'], '@vendor_url' => $lessautoprefixer_library['vendor url'])),
'#description' => t('Enable automatic prefixing of vendor CSS extensions.'),
'#default_value' => variable_get(LESS_AUTOPREFIXER, FALSE),
'#default_value' => variable_get(LESS_AUTOPREFIXER, FALSE) && !empty($lessautoprefixer_library['installed']),
'#disabled' => empty($lessautoprefixer_library['installed']),
);

View File

@ -22,9 +22,9 @@ files[] = classes/class.lessautoprefixer.inc
; Testing files
files[] = tests/less.test
; Information added by Drupal.org packaging script on 2015-01-30
version = "7.x-4.0"
; Information added by Drupal.org packaging script on 2015-02-04
version = "7.x-4.0+2-dev"
core = "7.x"
project = "less"
datestamp = "1422653011"
datestamp = "1423078338"

View File

@ -22,9 +22,9 @@ less[vars][@text_glow] = lighten(@gradient_start, 10%);
; this module.
less[sheets][] = styles/less_demo.drupal_add_css.css.less
; Information added by Drupal.org packaging script on 2015-01-30
version = "7.x-4.0"
; Information added by Drupal.org packaging script on 2015-02-04
version = "7.x-4.0+2-dev"
core = "7.x"
project = "less"
datestamp = "1422653011"
datestamp = "1423078338"