forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MDL-44357-master-noplugin' of git://github.com/FMCorz/m…
…oodle
- Loading branch information
Showing
81 changed files
with
11,171 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Moodle implementation of LESS. | ||
* | ||
* @package core | ||
* @copyright 2014 Frédéric Massart | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
require_once($CFG->libdir . '/lessphp/Autoloader.php'); | ||
Less_Autoloader::register(); | ||
|
||
/** | ||
* Moodle LESS compiler class. | ||
* | ||
* @package core | ||
* @copyright 2014 Frédéric Massart | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class core_lessc extends Less_Parser { | ||
|
||
/** | ||
* Parse the content of a file. | ||
* | ||
* The purpose of this method is to provide a way to import the | ||
* content of a file without messing with the import directories | ||
* as {@link self::parseFile()} would do. But of course you should | ||
* have manually set your import directories previously. | ||
* | ||
* @see self::SetImportDirs() | ||
* @param string $filepath The path to the file. | ||
* @return void | ||
*/ | ||
public function parse_file_content($filepath) { | ||
$this->parse(file_get_contents($filepath)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/** | ||
* Autoloader | ||
* | ||
* @package Less | ||
* @subpackage autoload | ||
*/ | ||
class Less_Autoloader { | ||
|
||
/** | ||
* Registered flag | ||
* | ||
* @var boolean | ||
*/ | ||
protected static $registered = false; | ||
|
||
/** | ||
* Library directory | ||
* | ||
* @var string | ||
*/ | ||
protected static $libDir; | ||
|
||
/** | ||
* Register the autoloader in the spl autoloader | ||
* | ||
* @return void | ||
* @throws Exception If there was an error in registration | ||
*/ | ||
public static function register(){ | ||
if( self::$registered ){ | ||
return; | ||
} | ||
|
||
self::$libDir = dirname(__FILE__); | ||
|
||
if(false === spl_autoload_register(array('Less_Autoloader', 'loadClass'))){ | ||
throw new Exception('Unable to register Less_Autoloader::loadClass as an autoloading method.'); | ||
} | ||
|
||
self::$registered = true; | ||
} | ||
|
||
/** | ||
* Unregisters the autoloader | ||
* | ||
* @return void | ||
*/ | ||
public static function unregister(){ | ||
spl_autoload_unregister(array('Less_Autoloader', 'loadClass')); | ||
self::$registered = false; | ||
} | ||
|
||
/** | ||
* Loads the class | ||
* | ||
* @param string $className The class to load | ||
*/ | ||
public static function loadClass($className){ | ||
|
||
|
||
// handle only package classes | ||
if(strpos($className, 'Less_') !== 0){ | ||
return; | ||
} | ||
|
||
$className = substr($className,5); | ||
$fileName = self::$libDir . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; | ||
|
||
if(file_exists($fileName)){ | ||
require $fileName; | ||
return true; | ||
}else{ | ||
throw new Exception('file not loadable '.$fileName); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
<?php | ||
|
||
require_once( dirname(__FILE__).'/Version.php'); | ||
|
||
/** | ||
* Utility for handling the generation and caching of css files | ||
* | ||
* @package Less | ||
* @subpackage cache | ||
* | ||
*/ | ||
class Less_Cache{ | ||
|
||
public static $cache_dir = false; // directory less.php can use for storing data | ||
|
||
|
||
/** | ||
* 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 boolean $use_cache Set to false to regenerate the css file | ||
* @return string Name of the css file | ||
*/ | ||
public static function Get( $less_files, $parser_options = array(), $use_cache = true ){ | ||
|
||
|
||
//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'); | ||
} | ||
|
||
self::CheckCacheDir(); | ||
|
||
// 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'; | ||
|
||
|
||
// check cached content | ||
if( $use_cache === true && 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); | ||
} | ||
|
||
/** | ||
* 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 | ||
* @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 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 'lessphp_'.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); | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
public static function CleanCache(){ | ||
static $clean = false; | ||
|
||
if( $clean ){ | ||
return; | ||
} | ||
|
||
$files = scandir(Less_Cache::$cache_dir); | ||
if( $files ){ | ||
$check_time = time() - 604800; | ||
foreach($files as $file){ | ||
if( strpos($file,'lessphp_') !== 0 ){ | ||
continue; | ||
} | ||
$full_path = Less_Cache::$cache_dir.'/'.$file; | ||
if( filemtime($full_path) > $check_time ){ | ||
continue; | ||
} | ||
unlink($full_path); | ||
} | ||
} | ||
|
||
$clean = true; | ||
} | ||
|
||
} |
Oops, something went wrong.