Skip to content

Commit

Permalink
MDL-49518 libraries: Update lessphp to 1.7.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Cooper committed Mar 24, 2015
1 parent a149d6a commit 657b127
Show file tree
Hide file tree
Showing 61 changed files with 511 additions and 289 deletions.
150 changes: 121 additions & 29 deletions lib/lessphp/Cache.php 100644 → 100755
Expand Up @@ -11,7 +11,17 @@
*/
class Less_Cache{

public static $cache_dir = false; // directory less.php can use for storing data
// 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;


/**
Expand All @@ -21,10 +31,10 @@ class Less_Cache{
*
* @param array $less_files Array of .less files to compile
* @param array $parser_options Array of compiler options
* @param boolean $use_cache Set to false to regenerate the css file
* @param array $modify_vars Array of variables
* @return string Name of the css file
*/
public static function Get( $less_files, $parser_options = array(), $use_cache = true ){
public static function Get( $less_files, $parser_options = array(), $modify_vars = array() ){


//check $cache_dir
Expand All @@ -36,39 +46,62 @@ public static function Get( $less_files, $parser_options = array(), $use_cache =
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
$less_files = (array)$less_files;
$hash = md5(json_encode($less_files));
$list_file = Less_Cache::$cache_dir.'lessphp_'.$hash.'.list';
$list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list';


// check cached content
if( $use_cache === true && file_exists($list_file) ){
if( !isset($parser_options['use_cache']) || $parser_options['use_cache'] === true ){
if( file_exists($list_file) ){

$list = explode("\n",file_get_contents($list_file));
self::ListFiles($list_file, $list, $cached_name);
$compiled_name = self::CompiledName($list);

//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 ){
// 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 );
$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
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 );
Expand Down Expand Up @@ -102,10 +135,12 @@ public static function Get( $less_files, $parser_options = array(), $use_cache =
*
* @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() ){
return self::Get( $less_files, $parser_options, false );
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() ){
Expand Down Expand Up @@ -167,7 +202,7 @@ private static function CompiledName( $files ){
$temp[] = filemtime($file)."\t".filesize($file)."\t".$file;
}

return 'lessphp_'.sha1(json_encode($temp)).'.css';
return Less_Cache::$prefix.sha1(json_encode($temp)).'.css';
}


Expand Down Expand Up @@ -196,6 +231,10 @@ public static function CheckCacheDir(){
}


/**
* Delete unused less.php files
*
*/
public static function CleanCache(){
static $clean = false;

Expand All @@ -205,20 +244,73 @@ public static function CleanCache(){

$files = scandir(Less_Cache::$cache_dir);
if( $files ){
$check_time = time() - 604800;
$check_time = time() - self::$gc_lifetime;
foreach($files as $file){
if( strpos($file,'lessphp_') !== 0 ){

// 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;
if( filemtime($full_path) > $check_time ){

// 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;
}

}

}
Empty file modified lib/lessphp/Colors.php 100644 → 100755
Empty file.
Empty file modified lib/lessphp/Configurable.php 100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion lib/lessphp/Environment.php 100644 → 100755
Expand Up @@ -47,6 +47,11 @@ class Less_Environment{

public static $mixin_stack = 0;

/**
* @var array
*/
public $functions = array();


public function Init(){

Expand Down Expand Up @@ -114,7 +119,7 @@ public static function isPathRelative($path){
* @return string Canonicalized path
*
*/
static function normalizePath($path){
public static function normalizePath($path){

$segments = explode('/',$path);
$segments = array_reverse($segments);
Expand Down
6 changes: 3 additions & 3 deletions lib/lessphp/Exception/Chunk.php 100644 → 100755
Expand Up @@ -46,7 +46,7 @@ public function __construct($input, Exception $previous = null, $index = null, $
* We don't actually need the chunks
*
*/
function Chunks(){
protected function Chunks(){
$level = 0;
$parenLevel = 0;
$lastMultiCommentEndBrace = null;
Expand Down Expand Up @@ -173,12 +173,12 @@ function Chunks(){
//$this->emitChunk(true);
}

function CharCode($pos){
public function CharCode($pos){
return ord($this->input[$pos]);
}


function fail( $msg, $index = null ){
public function fail( $msg, $index = null ){

if( !$index ){
$this->index = $this->parserCurrentIndex;
Expand Down
Empty file modified lib/lessphp/Exception/Compiler.php 100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion lib/lessphp/Exception/Parser.php 100644 → 100755
Expand Up @@ -99,7 +99,12 @@ public function genMessage(){
*/
public function getLineNumber(){
if( $this->index ){
return substr_count($this->input, "\n", 0, $this->index) + 1;
// https://bugs.php.net/bug.php?id=49790
if (ini_get("mbstring.func_overload")) {
return substr_count(substr($this->input, 0, $this->index), "\n") + 1;
} else {
return substr_count($this->input, "\n", 0, $this->index) + 1;
}
}
return 1;
}
Expand Down
24 changes: 12 additions & 12 deletions lib/lessphp/Functions.php 100644 → 100755
Expand Up @@ -21,7 +21,7 @@ function __construct($env, $currentFileInfo = null ){
/**
* @param string $op
*/
static public function operate( $op, $a, $b ){
public static function operate( $op, $a, $b ){
switch ($op) {
case '+': return $a + $b;
case '-': return $a - $b;
Expand All @@ -30,11 +30,11 @@ static public function operate( $op, $a, $b ){
}
}

static public function clamp($val, $max = 1){
public static function clamp($val, $max = 1){
return min( max($val, 0), $max);
}

static function fround( $value ){
public static function fround( $value ){

if( $value === 0 ){
return $value;
Expand All @@ -47,7 +47,7 @@ static function fround( $value ){
return $value;
}

static public function number($n){
public static function number($n){

if ($n instanceof Less_Tree_Dimension) {
return floatval( $n->unit->is('%') ? $n->value / 100 : $n->value);
Expand All @@ -58,7 +58,7 @@ static public function number($n){
}
}

static public function scaled($n, $size = 255 ){
public static function scaled($n, $size = 255 ){
if( $n instanceof Less_Tree_Dimension && $n->unit->is('%') ){
return (float)$n->value * $size / 100;
} else {
Expand Down Expand Up @@ -102,7 +102,7 @@ public function hsla($h, $s, $l, $a){
/**
* @param double $h
*/
function hsla_hue($h, $m1, $m2){
public function hsla_hue($h, $m1, $m2){
$h = $h < 0 ? $h + 1 : ($h > 1 ? $h - 1 : $h);
if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
else if ($h * 2 < 1) return $m2;
Expand Down Expand Up @@ -520,7 +520,7 @@ private function _math() {
/**
* @param boolean $isMin
*/
function _minmax( $isMin, $args ){
private function _minmax( $isMin, $args ){

$arg_count = count($args);

Expand Down Expand Up @@ -715,12 +715,12 @@ public function extract($values, $index ){
return null;
}

function length($values){
public function length($values){
$n = (property_exists($values,'value') && is_array($values->value)) ? count($values->value) : 1;
return new Less_Tree_Dimension($n);
}

function datauri($mimetypeNode, $filePathNode = null ) {
public function datauri($mimetypeNode, $filePathNode = null ) {

$filePath = ( $filePathNode ? $filePathNode->value : null );
$mimetype = $mimetypeNode->value;
Expand Down Expand Up @@ -796,7 +796,7 @@ function datauri($mimetypeNode, $filePathNode = null ) {
}

//svg-gradient
function svggradient( $direction ){
public function svggradient( $direction ){

$throw_message = 'svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position]';
$arguments = func_get_args();
Expand Down Expand Up @@ -995,15 +995,15 @@ public function average($color1, $color2){
}

// non-w3c functions:
function colorBlendAverage($cb, $cs ){
public function colorBlendAverage($cb, $cs ){
return ($cb + $cs) / 2;
}

public function negation($color1, $color2 ){
return $this->colorBlend( array($this,'colorBlendNegation'), $color1, $color2 );
}

function colorBlendNegation($cb, $cs){
public function colorBlendNegation($cb, $cs){
return 1 - abs($cb + $cs - 1);
}

Expand Down
Empty file modified lib/lessphp/Less.php.combine 100644 → 100755
Empty file.

0 comments on commit 657b127

Please sign in to comment.