Debian Codegraph @

vendor/wikimedia/less.php/lessc.inc.php

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