lessc.inc.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * This file provides the part of lessphp API (https://github.com/leafo/lessphp)
  4. * to be a drop-in replacement for following products:
  5. * - Drupal 7, by the less module v3.0+ (https://drupal.org/project/less)
  6. * - Symfony 2
  7. */
  8. // Register autoloader for non-composer installations
  9. if ( !class_exists( 'Less_Parser' ) ) {
  10. require_once __DIR__ . '/lib/Less/Autoloader.php';
  11. Less_Autoloader::register();
  12. }
  13. class lessc {
  14. static public $VERSION = Less_Version::less_version;
  15. public $importDir = '';
  16. protected $allParsedFiles = array();
  17. protected $libFunctions = array();
  18. protected $registeredVars = array();
  19. private $formatterName;
  20. private $options = array();
  21. public function __construct( $lessc=null, $sourceName=null ) {}
  22. public function setImportDir( $dirs ) {
  23. $this->importDir = (array)$dirs;
  24. }
  25. public function addImportDir( $dir ) {
  26. $this->importDir = (array)$this->importDir;
  27. $this->importDir[] = $dir;
  28. }
  29. public function setFormatter( $name ) {
  30. $this->formatterName = $name;
  31. }
  32. public function setPreserveComments( $preserve ) {}
  33. public function registerFunction( $name, $func ) {
  34. $this->libFunctions[$name] = $func;
  35. }
  36. public function unregisterFunction( $name ) {
  37. unset( $this->libFunctions[$name] );
  38. }
  39. public function setVariables( $variables ){
  40. foreach ( $variables as $name => $value ) {
  41. $this->setVariable( $name, $value );
  42. }
  43. }
  44. public function setVariable( $name, $value ) {
  45. $this->registeredVars[$name] = $value;
  46. }
  47. public function unsetVariable( $name ) {
  48. unset( $this->registeredVars[$name] );
  49. }
  50. public function setOptions( $options ) {
  51. foreach ( $options as $name => $value ) {
  52. $this->setOption( $name, $value);
  53. }
  54. }
  55. public function setOption( $name, $value ) {
  56. $this->options[$name] = $value;
  57. }
  58. public function parse( $buffer, $presets = array() ) {
  59. $this->setVariables( $presets );
  60. $parser = new Less_Parser( $this->getOptions() );
  61. $parser->setImportDirs( $this->getImportDirs() );
  62. foreach ( $this->libFunctions as $name => $func ) {
  63. $parser->registerFunction( $name, $func );
  64. }
  65. $parser->parse($buffer);
  66. if ( count( $this->registeredVars ) ) {
  67. $parser->ModifyVars( $this->registeredVars );
  68. }
  69. return $parser->getCss();
  70. }
  71. protected function getOptions() {
  72. $options = array( 'relativeUrls'=>false );
  73. switch( $this->formatterName ) {
  74. case 'compressed':
  75. $options['compress'] = true;
  76. break;
  77. }
  78. if (is_array($this->options))
  79. {
  80. $options = array_merge($options, $this->options);
  81. }
  82. return $options;
  83. }
  84. protected function getImportDirs() {
  85. $dirs_ = (array)$this->importDir;
  86. $dirs = array();
  87. foreach ( $dirs_ as $dir ) {
  88. $dirs[$dir] = '';
  89. }
  90. return $dirs;
  91. }
  92. public function compile( $string, $name = null ) {
  93. $oldImport = $this->importDir;
  94. $this->importDir = (array)$this->importDir;
  95. $this->allParsedFiles = array();
  96. $parser = new Less_Parser( $this->getOptions() );
  97. $parser->SetImportDirs( $this->getImportDirs() );
  98. if ( count( $this->registeredVars ) ) {
  99. $parser->ModifyVars( $this->registeredVars );
  100. }
  101. foreach ( $this->libFunctions as $name => $func ) {
  102. $parser->registerFunction( $name, $func );
  103. }
  104. $parser->parse( $string );
  105. $out = $parser->getCss();
  106. $parsed = Less_Parser::AllParsedFiles();
  107. foreach ( $parsed as $file ) {
  108. $this->addParsedFile( $file );
  109. }
  110. $this->importDir = $oldImport;
  111. return $out;
  112. }
  113. public function compileFile( $fname, $outFname = null ) {
  114. if ( !is_readable( $fname ) ) {
  115. throw new Exception( 'load error: failed to find '.$fname );
  116. }
  117. $pi = pathinfo( $fname );
  118. $oldImport = $this->importDir;
  119. $this->importDir = (array)$this->importDir;
  120. $this->importDir[] = Less_Parser::AbsPath( $pi['dirname'] ).'/';
  121. $this->allParsedFiles = array();
  122. $this->addParsedFile( $fname );
  123. $parser = new Less_Parser( $this->getOptions() );
  124. $parser->SetImportDirs( $this->getImportDirs() );
  125. if ( count( $this->registeredVars ) ) {
  126. $parser->ModifyVars( $this->registeredVars );
  127. }
  128. foreach ( $this->libFunctions as $name => $func ) {
  129. $parser->registerFunction( $name, $func );
  130. }
  131. $parser->parseFile( $fname );
  132. $out = $parser->getCss();
  133. $parsed = Less_Parser::AllParsedFiles();
  134. foreach ( $parsed as $file ) {
  135. $this->addParsedFile( $file );
  136. }
  137. $this->importDir = $oldImport;
  138. if ( $outFname !== null ) {
  139. return file_put_contents( $outFname, $out );
  140. }
  141. return $out;
  142. }
  143. public function checkedCompile( $in, $out ) {
  144. if ( !is_file( $out ) || filemtime( $in ) > filemtime( $out ) ) {
  145. $this->compileFile($in, $out);
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Execute lessphp on a .less file or a lessphp cache structure
  152. *
  153. * The lessphp cache structure contains information about a specific
  154. * less file having been parsed. It can be used as a hint for future
  155. * calls to determine whether or not a rebuild is required.
  156. *
  157. * The cache structure contains two important keys that may be used
  158. * externally:
  159. *
  160. * compiled: The final compiled CSS
  161. * updated: The time (in seconds) the CSS was last compiled
  162. *
  163. * The cache structure is a plain-ol' PHP associative array and can
  164. * be serialized and unserialized without a hitch.
  165. *
  166. * @param mixed $in Input
  167. * @param bool $force Force rebuild?
  168. * @return array lessphp cache structure
  169. */
  170. public function cachedCompile( $in, $force = false ) {
  171. // assume no root
  172. $root = null;
  173. if ( is_string( $in ) ) {
  174. $root = $in;
  175. } elseif ( is_array( $in ) and isset( $in['root'] ) ) {
  176. if ( $force or ! isset( $in['files'] ) ) {
  177. // If we are forcing a recompile or if for some reason the
  178. // structure does not contain any file information we should
  179. // specify the root to trigger a rebuild.
  180. $root = $in['root'];
  181. } elseif ( isset( $in['files'] ) and is_array( $in['files'] ) ) {
  182. foreach ( $in['files'] as $fname => $ftime ) {
  183. if ( !file_exists( $fname ) or filemtime( $fname ) > $ftime ) {
  184. // One of the files we knew about previously has changed
  185. // so we should look at our incoming root again.
  186. $root = $in['root'];
  187. break;
  188. }
  189. }
  190. }
  191. } else {
  192. // TODO: Throw an exception? We got neither a string nor something
  193. // that looks like a compatible lessphp cache structure.
  194. return null;
  195. }
  196. if ( $root !== null ) {
  197. // If we have a root value which means we should rebuild.
  198. $out = array();
  199. $out['root'] = $root;
  200. $out['compiled'] = $this->compileFile($root);
  201. $out['files'] = $this->allParsedFiles();
  202. $out['updated'] = time();
  203. return $out;
  204. } else {
  205. // No changes, pass back the structure
  206. // we were given initially.
  207. return $in;
  208. }
  209. }
  210. public function ccompile( $in, $out, $less = null ) {
  211. if ( $less === null ) {
  212. $less = new self;
  213. }
  214. return $less->checkedCompile( $in, $out );
  215. }
  216. public static function cexecute( $in, $force = false, $less = null ) {
  217. if ( $less === null ) {
  218. $less = new self;
  219. }
  220. return $less->cachedCompile($in, $force);
  221. }
  222. public function allParsedFiles() {
  223. return $this->allParsedFiles;
  224. }
  225. protected function addParsedFile( $file ) {
  226. $this->allParsedFiles[Less_Parser::AbsPath( $file )] = filemtime( $file );
  227. }
  228. }