summaryrefslogtreecommitdiffstats
path: root/vendor/wikimedia/less.php/lessc.inc.php
blob: a3c66dd2644afa7c1fba39130390e6ad61a29359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
 * This file provides the part of lessphp API (https://github.com/leafo/lessphp)
 * to be a drop-in replacement for following products:
 *  - Drupal 7, by the less module v3.0+ (https://drupal.org/project/less)
 *  - Symfony 2
 */

// Register autoloader for non-composer installations
if ( !class_exists( 'Less_Parser' ) ) {
	require_once __DIR__ . '/lib/Less/Autoloader.php';
	Less_Autoloader::register();
}

class lessc {

	public static $VERSION = Less_Version::less_version;

	public $importDir = '';
	protected $allParsedFiles = [];
	protected $libFunctions = [];
	protected $registeredVars = [];
	private $formatterName;
	private $options = [];

	public function __construct( $lessc = null, $sourceName = null ) {
	}

	public function setImportDir( $dirs ) {
		$this->importDir = (array)$dirs;
	}

	public function addImportDir( $dir ) {
		$this->importDir = (array)$this->importDir;
		$this->importDir[] = $dir;
	}

	public function setFormatter( $name ) {
		$this->formatterName = $name;
	}

	public function setPreserveComments( $preserve ) {
	}

	public function registerFunction( $name, $func ) {
		$this->libFunctions[$name] = $func;
	}

	public function unregisterFunction( $name ) {
		unset( $this->libFunctions[$name] );
	}

	public function setVariables( $variables ) {
		foreach ( $variables as $name => $value ) {
			$this->setVariable( $name, $value );
		}
	}

	public function setVariable( $name, $value ) {
		$this->registeredVars[$name] = $value;
	}

	public function unsetVariable( $name ) {
		unset( $this->registeredVars[$name] );
	}

	public function setOptions( $options ) {
		foreach ( $options as $name => $value ) {
			$this->setOption( $name, $value );
		}
	}

	public function setOption( $name, $value ) {
		$this->options[$name] = $value;
	}

	public function parse( $buffer, $presets = [] ) {
		$this->setVariables( $presets );

		$parser = new Less_Parser( $this->getOptions() );
		$parser->setImportDirs( $this->getImportDirs() );
		foreach ( $this->libFunctions as $name => $func ) {
			$parser->registerFunction( $name, $func );
		}
		$parser->parse( $buffer );
		if ( count( $this->registeredVars ) ) {
			$parser->ModifyVars( $this->registeredVars );
		}

		return $parser->getCss();
	}

	protected function getOptions() {
		$options = [ 'relativeUrls' => false ];
		switch ( $this->formatterName ) {
			case 'compressed':
				$options['compress'] = true;
				break;
		}
		if ( is_array( $this->options ) ) {
			$options = array_merge( $options, $this->options );
		}
		return $options;
	}

	protected function getImportDirs() {
		$dirs_ = (array)$this->importDir;
		$dirs = [];
		foreach ( $dirs_ as $dir ) {
			$dirs[$dir] = '';
		}
		return $dirs;
	}

	public function compile( $string, $name = null ) {
		$oldImport = $this->importDir;
		$this->importDir = (array)$this->importDir;

		$this->allParsedFiles = [];

		$parser = new Less_Parser( $this->getOptions() );
		$parser->SetImportDirs( $this->getImportDirs() );
		if ( count( $this->registeredVars ) ) {
			$parser->ModifyVars( $this->registeredVars );
		}
		foreach ( $this->libFunctions as $name => $func ) {
			$parser->registerFunction( $name, $func );
		}
		$parser->parse( $string );
		$out = $parser->getCss();

		$parsed = Less_Parser::AllParsedFiles();
		foreach ( $parsed as $file ) {
			$this->addParsedFile( $file );
		}

		$this->importDir = $oldImport;

		return $out;
	}

	public function compileFile( $fname, $outFname = null ) {
		if ( !is_readable( $fname ) ) {
			throw new Exception( 'load error: failed to find ' . $fname );
		}

		$pi = pathinfo( $fname );

		$oldImport = $this->importDir;

		$this->importDir = (array)$this->importDir;
		$this->importDir[] = Less_Parser::AbsPath( $pi['dirname'] ) . '/';

		$this->allParsedFiles = [];
		$this->addParsedFile( $fname );

		$parser = new Less_Parser( $this->getOptions() );
		$parser->SetImportDirs( $this->getImportDirs() );
		if ( count( $this->registeredVars ) ) {
			$parser->ModifyVars( $this->registeredVars );
		}
		foreach ( $this->libFunctions as $name => $func ) {
			$parser->registerFunction( $name, $func );
		}
		$parser->parseFile( $fname );
		$out = $parser->getCss();

		$parsed = Less_Parser::AllParsedFiles();
		foreach ( $parsed as $file ) {
			$this->addParsedFile( $file );
		}

		$this->importDir = $oldImport;

		if ( $outFname !== null ) {
			return file_put_contents( $outFname, $out );
		}

		return $out;
	}

	public function checkedCompile( $in, $out ) {
		if ( !is_file( $out ) || filemtime( $in ) > filemtime( $out ) ) {
			$this->compileFile( $in, $out );
			return true;
		}
		return false;
	}

	/**
	 * Execute lessphp on a .less file or a lessphp cache structure
	 *
	 * The lessphp cache structure contains information about a specific
	 * less file having been parsed. It can be used as a hint for future
	 * calls to determine whether or not a rebuild is required.
	 *
	 * The cache structure contains two important keys that may be used
	 * externally:
	 *
	 * compiled: The final compiled CSS
	 * updated: The time (in seconds) the CSS was last compiled
	 *
	 * The cache structure is a plain-ol' PHP associative array and can
	 * be serialized and unserialized without a hitch.
	 *
	 * @param mixed $in Input
	 * @param bool $force Force rebuild?
	 * @return array lessphp cache structure
	 */
	public function cachedCompile( $in, $force = false ) {
		// assume no root
		$root = null;

		if ( is_string( $in ) ) {
			$root = $in;
		} elseif ( is_array( $in ) and isset( $in['root'] ) ) {
			if ( $force or !isset( $in['files'] ) ) {
				// If we are forcing a recompile or if for some reason the
				// structure does not contain any file information we should
				// specify the root to trigger a rebuild.
				$root = $in['root'];
			} elseif ( isset( $in['files'] ) and is_array( $in['files'] ) ) {
				foreach ( $in['files'] as $fname => $ftime ) {
					if ( !file_exists( $fname ) or filemtime( $fname ) > $ftime ) {
						// One of the files we knew about previously has changed
						// so we should look at our incoming root again.
						$root = $in['root'];
						break;
					}
				}
			}
		} else {
			// TODO: Throw an exception? We got neither a string nor something
			// that looks like a compatible lessphp cache structure.
			return null;
		}

		if ( $root !== null ) {
			// If we have a root value which means we should rebuild.
			$out = [];
			$out['root'] = $root;
			$out['compiled'] = $this->compileFile( $root );
			$out['files'] = $this->allParsedFiles();
			$out['updated'] = time();
			return $out;
		} else {
			// No changes, pass back the structure
			// we were given initially.
			return $in;
		}
	}

	public function ccompile( $in, $out, $less = null ) {
		if ( $less === null ) {
			$less = new self;
		}
		return $less->checkedCompile( $in, $out );
	}

	public static function cexecute( $in, $force = false, $less = null ) {
		if ( $less === null ) {
			$less = new self;
		}
		return $less->cachedCompile( $in, $force );
	}

	public function allParsedFiles() {
		return $this->allParsedFiles;
	}

	protected function addParsedFile( $file ) {
		$this->allParsedFiles[Less_Parser::AbsPath( $file )] = filemtime( $file );
	}
}