@@ -22,312 +22,312 @@ |
||
22 | 22 | */ |
23 | 23 | abstract class Formatter |
24 | 24 | { |
25 | - /** |
|
26 | - * @var integer |
|
27 | - */ |
|
28 | - public $indentLevel; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - public $indentChar; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - public $break; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - public $open; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - public $close; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - public $tagSeparator; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var string |
|
57 | - */ |
|
58 | - public $assignSeparator; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var boolean |
|
62 | - */ |
|
63 | - public $keepSemicolons; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var \ScssPhp\ScssPhp\Formatter\OutputBlock |
|
67 | - */ |
|
68 | - protected $currentBlock; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var integer |
|
72 | - */ |
|
73 | - protected $currentLine; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var integer |
|
77 | - */ |
|
78 | - protected $currentColumn; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var \ScssPhp\ScssPhp\SourceMap\SourceMapGenerator |
|
82 | - */ |
|
83 | - protected $sourceMapGenerator; |
|
84 | - |
|
85 | - /** |
|
86 | - * @var string |
|
87 | - */ |
|
88 | - protected $strippedSemicolon; |
|
89 | - |
|
90 | - /** |
|
91 | - * Initialize formatter |
|
92 | - * |
|
93 | - * @api |
|
94 | - */ |
|
95 | - abstract public function __construct(); |
|
96 | - |
|
97 | - /** |
|
98 | - * Return indentation (whitespace) |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - protected function indentStr() |
|
103 | - { |
|
104 | - return ''; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Return property assignment |
|
109 | - * |
|
110 | - * @api |
|
111 | - * |
|
112 | - * @param string $name |
|
113 | - * @param mixed $value |
|
114 | - * |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function property($name, $value) |
|
118 | - { |
|
119 | - return rtrim($name) . $this->assignSeparator . $value . ';'; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Return custom property assignment |
|
124 | - * differs in that you have to keep spaces in the value as is |
|
125 | - * |
|
126 | - * @api |
|
127 | - * |
|
128 | - * @param string $name |
|
129 | - * @param mixed $value |
|
130 | - * |
|
131 | - * @return string |
|
132 | - */ |
|
133 | - public function customProperty($name, $value) |
|
134 | - { |
|
135 | - return rtrim($name) . trim($this->assignSeparator) . $value . ';'; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Output lines inside a block |
|
140 | - * |
|
141 | - * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
142 | - */ |
|
143 | - protected function blockLines(OutputBlock $block) |
|
144 | - { |
|
145 | - $inner = $this->indentStr(); |
|
146 | - $glue = $this->break . $inner; |
|
147 | - |
|
148 | - $this->write($inner . implode($glue, $block->lines)); |
|
149 | - |
|
150 | - if (! empty($block->children)) { |
|
151 | - $this->write($this->break); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Output block selectors |
|
157 | - * |
|
158 | - * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
159 | - */ |
|
160 | - protected function blockSelectors(OutputBlock $block) |
|
161 | - { |
|
162 | - $inner = $this->indentStr(); |
|
163 | - |
|
164 | - $this->write($inner |
|
165 | - . implode($this->tagSeparator, $block->selectors) |
|
166 | - . $this->open . $this->break); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Output block children |
|
171 | - * |
|
172 | - * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
173 | - */ |
|
174 | - protected function blockChildren(OutputBlock $block) |
|
175 | - { |
|
176 | - foreach ($block->children as $child) { |
|
177 | - $this->block($child); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Output non-empty block |
|
183 | - * |
|
184 | - * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
185 | - */ |
|
186 | - protected function block(OutputBlock $block) |
|
187 | - { |
|
188 | - if (empty($block->lines) && empty($block->children)) { |
|
189 | - return; |
|
190 | - } |
|
191 | - |
|
192 | - $this->currentBlock = $block; |
|
193 | - |
|
194 | - $pre = $this->indentStr(); |
|
195 | - |
|
196 | - if (! empty($block->selectors)) { |
|
197 | - $this->blockSelectors($block); |
|
198 | - |
|
199 | - $this->indentLevel++; |
|
200 | - } |
|
201 | - |
|
202 | - if (! empty($block->lines)) { |
|
203 | - $this->blockLines($block); |
|
204 | - } |
|
205 | - |
|
206 | - if (! empty($block->children)) { |
|
207 | - $this->blockChildren($block); |
|
208 | - } |
|
209 | - |
|
210 | - if (! empty($block->selectors)) { |
|
211 | - $this->indentLevel--; |
|
212 | - |
|
213 | - if (! $this->keepSemicolons) { |
|
214 | - $this->strippedSemicolon = ''; |
|
215 | - } |
|
216 | - |
|
217 | - if (empty($block->children)) { |
|
218 | - $this->write($this->break); |
|
219 | - } |
|
220 | - |
|
221 | - $this->write($pre . $this->close . $this->break); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Test and clean safely empty children |
|
227 | - * |
|
228 | - * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
229 | - * |
|
230 | - * @return boolean |
|
231 | - */ |
|
232 | - protected function testEmptyChildren($block) |
|
233 | - { |
|
234 | - $isEmpty = empty($block->lines); |
|
235 | - |
|
236 | - if ($block->children) { |
|
237 | - foreach ($block->children as $k => &$child) { |
|
238 | - if (! $this->testEmptyChildren($child)) { |
|
239 | - $isEmpty = false; |
|
240 | - continue; |
|
241 | - } |
|
242 | - |
|
243 | - if ($child->type === Type::T_MEDIA || $child->type === Type::T_DIRECTIVE) { |
|
244 | - $child->children = []; |
|
245 | - $child->selectors = null; |
|
246 | - } |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - return $isEmpty; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Entry point to formatting a block |
|
255 | - * |
|
256 | - * @api |
|
257 | - * |
|
258 | - * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block An abstract syntax tree |
|
259 | - * @param \ScssPhp\ScssPhp\SourceMap\SourceMapGenerator|null $sourceMapGenerator Optional source map generator |
|
260 | - * |
|
261 | - * @return string |
|
262 | - */ |
|
263 | - public function format(OutputBlock $block, SourceMapGenerator $sourceMapGenerator = null) |
|
264 | - { |
|
265 | - $this->sourceMapGenerator = null; |
|
266 | - |
|
267 | - if ($sourceMapGenerator) { |
|
268 | - $this->currentLine = 1; |
|
269 | - $this->currentColumn = 0; |
|
270 | - $this->sourceMapGenerator = $sourceMapGenerator; |
|
271 | - } |
|
272 | - |
|
273 | - $this->testEmptyChildren($block); |
|
274 | - |
|
275 | - ob_start(); |
|
276 | - |
|
277 | - $this->block($block); |
|
278 | - |
|
279 | - $out = ob_get_clean(); |
|
280 | - |
|
281 | - return $out; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Output content |
|
286 | - * |
|
287 | - * @param string $str |
|
288 | - */ |
|
289 | - protected function write($str) |
|
290 | - { |
|
291 | - if (! empty($this->strippedSemicolon)) { |
|
292 | - echo $this->strippedSemicolon; |
|
293 | - |
|
294 | - $this->strippedSemicolon = ''; |
|
295 | - } |
|
296 | - |
|
297 | - /* |
|
25 | + /** |
|
26 | + * @var integer |
|
27 | + */ |
|
28 | + public $indentLevel; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + public $indentChar; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + public $break; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + public $open; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + public $close; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + public $tagSeparator; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var string |
|
57 | + */ |
|
58 | + public $assignSeparator; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var boolean |
|
62 | + */ |
|
63 | + public $keepSemicolons; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var \ScssPhp\ScssPhp\Formatter\OutputBlock |
|
67 | + */ |
|
68 | + protected $currentBlock; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var integer |
|
72 | + */ |
|
73 | + protected $currentLine; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var integer |
|
77 | + */ |
|
78 | + protected $currentColumn; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var \ScssPhp\ScssPhp\SourceMap\SourceMapGenerator |
|
82 | + */ |
|
83 | + protected $sourceMapGenerator; |
|
84 | + |
|
85 | + /** |
|
86 | + * @var string |
|
87 | + */ |
|
88 | + protected $strippedSemicolon; |
|
89 | + |
|
90 | + /** |
|
91 | + * Initialize formatter |
|
92 | + * |
|
93 | + * @api |
|
94 | + */ |
|
95 | + abstract public function __construct(); |
|
96 | + |
|
97 | + /** |
|
98 | + * Return indentation (whitespace) |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + protected function indentStr() |
|
103 | + { |
|
104 | + return ''; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Return property assignment |
|
109 | + * |
|
110 | + * @api |
|
111 | + * |
|
112 | + * @param string $name |
|
113 | + * @param mixed $value |
|
114 | + * |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function property($name, $value) |
|
118 | + { |
|
119 | + return rtrim($name) . $this->assignSeparator . $value . ';'; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Return custom property assignment |
|
124 | + * differs in that you have to keep spaces in the value as is |
|
125 | + * |
|
126 | + * @api |
|
127 | + * |
|
128 | + * @param string $name |
|
129 | + * @param mixed $value |
|
130 | + * |
|
131 | + * @return string |
|
132 | + */ |
|
133 | + public function customProperty($name, $value) |
|
134 | + { |
|
135 | + return rtrim($name) . trim($this->assignSeparator) . $value . ';'; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Output lines inside a block |
|
140 | + * |
|
141 | + * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
142 | + */ |
|
143 | + protected function blockLines(OutputBlock $block) |
|
144 | + { |
|
145 | + $inner = $this->indentStr(); |
|
146 | + $glue = $this->break . $inner; |
|
147 | + |
|
148 | + $this->write($inner . implode($glue, $block->lines)); |
|
149 | + |
|
150 | + if (! empty($block->children)) { |
|
151 | + $this->write($this->break); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Output block selectors |
|
157 | + * |
|
158 | + * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
159 | + */ |
|
160 | + protected function blockSelectors(OutputBlock $block) |
|
161 | + { |
|
162 | + $inner = $this->indentStr(); |
|
163 | + |
|
164 | + $this->write($inner |
|
165 | + . implode($this->tagSeparator, $block->selectors) |
|
166 | + . $this->open . $this->break); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Output block children |
|
171 | + * |
|
172 | + * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
173 | + */ |
|
174 | + protected function blockChildren(OutputBlock $block) |
|
175 | + { |
|
176 | + foreach ($block->children as $child) { |
|
177 | + $this->block($child); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Output non-empty block |
|
183 | + * |
|
184 | + * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
185 | + */ |
|
186 | + protected function block(OutputBlock $block) |
|
187 | + { |
|
188 | + if (empty($block->lines) && empty($block->children)) { |
|
189 | + return; |
|
190 | + } |
|
191 | + |
|
192 | + $this->currentBlock = $block; |
|
193 | + |
|
194 | + $pre = $this->indentStr(); |
|
195 | + |
|
196 | + if (! empty($block->selectors)) { |
|
197 | + $this->blockSelectors($block); |
|
198 | + |
|
199 | + $this->indentLevel++; |
|
200 | + } |
|
201 | + |
|
202 | + if (! empty($block->lines)) { |
|
203 | + $this->blockLines($block); |
|
204 | + } |
|
205 | + |
|
206 | + if (! empty($block->children)) { |
|
207 | + $this->blockChildren($block); |
|
208 | + } |
|
209 | + |
|
210 | + if (! empty($block->selectors)) { |
|
211 | + $this->indentLevel--; |
|
212 | + |
|
213 | + if (! $this->keepSemicolons) { |
|
214 | + $this->strippedSemicolon = ''; |
|
215 | + } |
|
216 | + |
|
217 | + if (empty($block->children)) { |
|
218 | + $this->write($this->break); |
|
219 | + } |
|
220 | + |
|
221 | + $this->write($pre . $this->close . $this->break); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Test and clean safely empty children |
|
227 | + * |
|
228 | + * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
|
229 | + * |
|
230 | + * @return boolean |
|
231 | + */ |
|
232 | + protected function testEmptyChildren($block) |
|
233 | + { |
|
234 | + $isEmpty = empty($block->lines); |
|
235 | + |
|
236 | + if ($block->children) { |
|
237 | + foreach ($block->children as $k => &$child) { |
|
238 | + if (! $this->testEmptyChildren($child)) { |
|
239 | + $isEmpty = false; |
|
240 | + continue; |
|
241 | + } |
|
242 | + |
|
243 | + if ($child->type === Type::T_MEDIA || $child->type === Type::T_DIRECTIVE) { |
|
244 | + $child->children = []; |
|
245 | + $child->selectors = null; |
|
246 | + } |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + return $isEmpty; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Entry point to formatting a block |
|
255 | + * |
|
256 | + * @api |
|
257 | + * |
|
258 | + * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block An abstract syntax tree |
|
259 | + * @param \ScssPhp\ScssPhp\SourceMap\SourceMapGenerator|null $sourceMapGenerator Optional source map generator |
|
260 | + * |
|
261 | + * @return string |
|
262 | + */ |
|
263 | + public function format(OutputBlock $block, SourceMapGenerator $sourceMapGenerator = null) |
|
264 | + { |
|
265 | + $this->sourceMapGenerator = null; |
|
266 | + |
|
267 | + if ($sourceMapGenerator) { |
|
268 | + $this->currentLine = 1; |
|
269 | + $this->currentColumn = 0; |
|
270 | + $this->sourceMapGenerator = $sourceMapGenerator; |
|
271 | + } |
|
272 | + |
|
273 | + $this->testEmptyChildren($block); |
|
274 | + |
|
275 | + ob_start(); |
|
276 | + |
|
277 | + $this->block($block); |
|
278 | + |
|
279 | + $out = ob_get_clean(); |
|
280 | + |
|
281 | + return $out; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Output content |
|
286 | + * |
|
287 | + * @param string $str |
|
288 | + */ |
|
289 | + protected function write($str) |
|
290 | + { |
|
291 | + if (! empty($this->strippedSemicolon)) { |
|
292 | + echo $this->strippedSemicolon; |
|
293 | + |
|
294 | + $this->strippedSemicolon = ''; |
|
295 | + } |
|
296 | + |
|
297 | + /* |
|
298 | 298 | * Maybe Strip semi-colon appended by property(); it's a separator, not a terminator |
299 | 299 | * will be striped for real before a closing, otherwise displayed unchanged starting the next write |
300 | 300 | */ |
301 | - if ( |
|
302 | - ! $this->keepSemicolons && |
|
303 | - $str && |
|
304 | - (strpos($str, ';') !== false) && |
|
305 | - (substr($str, -1) === ';') |
|
306 | - ) { |
|
307 | - $str = substr($str, 0, -1); |
|
308 | - |
|
309 | - $this->strippedSemicolon = ';'; |
|
310 | - } |
|
311 | - |
|
312 | - if ($this->sourceMapGenerator) { |
|
313 | - $this->sourceMapGenerator->addMapping( |
|
314 | - $this->currentLine, |
|
315 | - $this->currentColumn, |
|
316 | - $this->currentBlock->sourceLine, |
|
317 | - //columns from parser are off by one |
|
318 | - $this->currentBlock->sourceColumn > 0 ? $this->currentBlock->sourceColumn - 1 : 0, |
|
319 | - $this->currentBlock->sourceName |
|
320 | - ); |
|
321 | - |
|
322 | - $lines = explode("\n", $str); |
|
323 | - $lineCount = \count($lines); |
|
324 | - $this->currentLine += $lineCount - 1; |
|
325 | - |
|
326 | - $lastLine = array_pop($lines); |
|
327 | - |
|
328 | - $this->currentColumn = ($lineCount === 1 ? $this->currentColumn : 0) + \strlen($lastLine); |
|
329 | - } |
|
330 | - |
|
331 | - echo $str; |
|
332 | - } |
|
301 | + if ( |
|
302 | + ! $this->keepSemicolons && |
|
303 | + $str && |
|
304 | + (strpos($str, ';') !== false) && |
|
305 | + (substr($str, -1) === ';') |
|
306 | + ) { |
|
307 | + $str = substr($str, 0, -1); |
|
308 | + |
|
309 | + $this->strippedSemicolon = ';'; |
|
310 | + } |
|
311 | + |
|
312 | + if ($this->sourceMapGenerator) { |
|
313 | + $this->sourceMapGenerator->addMapping( |
|
314 | + $this->currentLine, |
|
315 | + $this->currentColumn, |
|
316 | + $this->currentBlock->sourceLine, |
|
317 | + //columns from parser are off by one |
|
318 | + $this->currentBlock->sourceColumn > 0 ? $this->currentBlock->sourceColumn - 1 : 0, |
|
319 | + $this->currentBlock->sourceName |
|
320 | + ); |
|
321 | + |
|
322 | + $lines = explode("\n", $str); |
|
323 | + $lineCount = \count($lines); |
|
324 | + $this->currentLine += $lineCount - 1; |
|
325 | + |
|
326 | + $lastLine = array_pop($lines); |
|
327 | + |
|
328 | + $this->currentColumn = ($lineCount === 1 ? $this->currentColumn : 0) + \strlen($lastLine); |
|
329 | + } |
|
330 | + |
|
331 | + echo $str; |
|
332 | + } |
|
333 | 333 | } |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | |
148 | 148 | $this->write($inner . implode($glue, $block->lines)); |
149 | 149 | |
150 | - if (! empty($block->children)) { |
|
150 | + if (!empty($block->children)) { |
|
151 | 151 | $this->write($this->break); |
152 | 152 | } |
153 | 153 | } |
@@ -193,24 +193,24 @@ discard block |
||
193 | 193 | |
194 | 194 | $pre = $this->indentStr(); |
195 | 195 | |
196 | - if (! empty($block->selectors)) { |
|
196 | + if (!empty($block->selectors)) { |
|
197 | 197 | $this->blockSelectors($block); |
198 | 198 | |
199 | 199 | $this->indentLevel++; |
200 | 200 | } |
201 | 201 | |
202 | - if (! empty($block->lines)) { |
|
202 | + if (!empty($block->lines)) { |
|
203 | 203 | $this->blockLines($block); |
204 | 204 | } |
205 | 205 | |
206 | - if (! empty($block->children)) { |
|
206 | + if (!empty($block->children)) { |
|
207 | 207 | $this->blockChildren($block); |
208 | 208 | } |
209 | 209 | |
210 | - if (! empty($block->selectors)) { |
|
210 | + if (!empty($block->selectors)) { |
|
211 | 211 | $this->indentLevel--; |
212 | 212 | |
213 | - if (! $this->keepSemicolons) { |
|
213 | + if (!$this->keepSemicolons) { |
|
214 | 214 | $this->strippedSemicolon = ''; |
215 | 215 | } |
216 | 216 | |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | |
236 | 236 | if ($block->children) { |
237 | 237 | foreach ($block->children as $k => &$child) { |
238 | - if (! $this->testEmptyChildren($child)) { |
|
238 | + if (!$this->testEmptyChildren($child)) { |
|
239 | 239 | $isEmpty = false; |
240 | 240 | continue; |
241 | 241 | } |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | */ |
289 | 289 | protected function write($str) |
290 | 290 | { |
291 | - if (! empty($this->strippedSemicolon)) { |
|
291 | + if (!empty($this->strippedSemicolon)) { |
|
292 | 292 | echo $this->strippedSemicolon; |
293 | 293 | |
294 | 294 | $this->strippedSemicolon = ''; |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * will be striped for real before a closing, otherwise displayed unchanged starting the next write |
300 | 300 | */ |
301 | 301 | if ( |
302 | - ! $this->keepSemicolons && |
|
302 | + !$this->keepSemicolons && |
|
303 | 303 | $str && |
304 | 304 | (strpos($str, ';') !== false) && |
305 | 305 | (substr($str, -1) === ';') |
@@ -19,30 +19,30 @@ |
||
19 | 19 | */ |
20 | 20 | class ParserException extends \Exception implements SassException |
21 | 21 | { |
22 | - /** |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - private $sourcePosition; |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + private $sourcePosition; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Get source position |
|
29 | - * |
|
30 | - * @api |
|
31 | - */ |
|
32 | - public function getSourcePosition() |
|
33 | - { |
|
34 | - return $this->sourcePosition; |
|
35 | - } |
|
27 | + /** |
|
28 | + * Get source position |
|
29 | + * |
|
30 | + * @api |
|
31 | + */ |
|
32 | + public function getSourcePosition() |
|
33 | + { |
|
34 | + return $this->sourcePosition; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Set source position |
|
39 | - * |
|
40 | - * @api |
|
41 | - * |
|
42 | - * @param array $sourcePosition |
|
43 | - */ |
|
44 | - public function setSourcePosition($sourcePosition) |
|
45 | - { |
|
46 | - $this->sourcePosition = $sourcePosition; |
|
47 | - } |
|
37 | + /** |
|
38 | + * Set source position |
|
39 | + * |
|
40 | + * @api |
|
41 | + * |
|
42 | + * @param array $sourcePosition |
|
43 | + */ |
|
44 | + public function setSourcePosition($sourcePosition) |
|
45 | + { |
|
46 | + $this->sourcePosition = $sourcePosition; |
|
47 | + } |
|
48 | 48 | } |
@@ -32,209 +32,209 @@ |
||
32 | 32 | */ |
33 | 33 | class Cache |
34 | 34 | { |
35 | - const CACHE_VERSION = 1; |
|
36 | - |
|
37 | - // directory used for storing data |
|
38 | - public static $cacheDir = false; |
|
39 | - |
|
40 | - // prefix for the storing data |
|
41 | - public static $prefix = 'scssphp_'; |
|
42 | - |
|
43 | - // force a refresh : 'once' for refreshing the first hit on a cache only, true to never use the cache in this hit |
|
44 | - public static $forceRefresh = false; |
|
45 | - |
|
46 | - // specifies the number of seconds after which data cached will be seen as 'garbage' and potentially cleaned up |
|
47 | - public static $gcLifetime = 604800; |
|
48 | - |
|
49 | - // array of already refreshed cache if $forceRefresh==='once' |
|
50 | - protected static $refreshed = []; |
|
51 | - |
|
52 | - /** |
|
53 | - * Constructor |
|
54 | - * |
|
55 | - * @param array $options |
|
56 | - */ |
|
57 | - public function __construct($options) |
|
58 | - { |
|
59 | - // check $cacheDir |
|
60 | - if (isset($options['cacheDir'])) { |
|
61 | - self::$cacheDir = $options['cacheDir']; |
|
62 | - } |
|
63 | - |
|
64 | - if (empty(self::$cacheDir)) { |
|
65 | - throw new Exception('cacheDir not set'); |
|
66 | - } |
|
67 | - |
|
68 | - if (isset($options['prefix'])) { |
|
69 | - self::$prefix = $options['prefix']; |
|
70 | - } |
|
71 | - |
|
72 | - if (empty(self::$prefix)) { |
|
73 | - throw new Exception('prefix not set'); |
|
74 | - } |
|
75 | - |
|
76 | - if (isset($options['forceRefresh'])) { |
|
77 | - self::$forceRefresh = $options['forceRefresh']; |
|
78 | - } |
|
79 | - |
|
80 | - self::checkCacheDir(); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get the cached result of $operation on $what, |
|
85 | - * which is known as dependant from the content of $options |
|
86 | - * |
|
87 | - * @param string $operation parse, compile... |
|
88 | - * @param mixed $what content key (e.g., filename to be treated) |
|
89 | - * @param array $options any option that affect the operation result on the content |
|
90 | - * @param integer $lastModified last modified timestamp |
|
91 | - * |
|
92 | - * @return mixed |
|
93 | - * |
|
94 | - * @throws \Exception |
|
95 | - */ |
|
96 | - public function getCache($operation, $what, $options = [], $lastModified = null) |
|
97 | - { |
|
98 | - $fileCache = self::$cacheDir . self::cacheName($operation, $what, $options); |
|
99 | - |
|
100 | - if ( |
|
101 | - ((self::$forceRefresh === false) || (self::$forceRefresh === 'once' && |
|
102 | - isset(self::$refreshed[$fileCache]))) && file_exists($fileCache) |
|
103 | - ) { |
|
104 | - $cacheTime = filemtime($fileCache); |
|
105 | - |
|
106 | - if ( |
|
107 | - (\is_null($lastModified) || $cacheTime > $lastModified) && |
|
108 | - $cacheTime + self::$gcLifetime > time() |
|
109 | - ) { |
|
110 | - $c = file_get_contents($fileCache); |
|
111 | - $c = unserialize($c); |
|
112 | - |
|
113 | - if (\is_array($c) && isset($c['value'])) { |
|
114 | - return $c['value']; |
|
115 | - } |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - return null; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Put in cache the result of $operation on $what, |
|
124 | - * which is known as dependant from the content of $options |
|
125 | - * |
|
126 | - * @param string $operation |
|
127 | - * @param mixed $what |
|
128 | - * @param mixed $value |
|
129 | - * @param array $options |
|
130 | - */ |
|
131 | - public function setCache($operation, $what, $value, $options = []) |
|
132 | - { |
|
133 | - $fileCache = self::$cacheDir . self::cacheName($operation, $what, $options); |
|
134 | - |
|
135 | - $c = ['value' => $value]; |
|
136 | - $c = serialize($c); |
|
137 | - |
|
138 | - file_put_contents($fileCache, $c); |
|
139 | - |
|
140 | - if (self::$forceRefresh === 'once') { |
|
141 | - self::$refreshed[$fileCache] = true; |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get the cache name for the caching of $operation on $what, |
|
147 | - * which is known as dependant from the content of $options |
|
148 | - * |
|
149 | - * @param string $operation |
|
150 | - * @param mixed $what |
|
151 | - * @param array $options |
|
152 | - * |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - private static function cacheName($operation, $what, $options = []) |
|
156 | - { |
|
157 | - $t = [ |
|
158 | - 'version' => self::CACHE_VERSION, |
|
159 | - 'operation' => $operation, |
|
160 | - 'what' => $what, |
|
161 | - 'options' => $options |
|
162 | - ]; |
|
163 | - |
|
164 | - $t = self::$prefix |
|
165 | - . sha1(json_encode($t)) |
|
166 | - . ".$operation" |
|
167 | - . ".scsscache"; |
|
168 | - |
|
169 | - return $t; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Check that the cache dir exists and is writeable |
|
174 | - * |
|
175 | - * @throws \Exception |
|
176 | - */ |
|
177 | - public static function checkCacheDir() |
|
178 | - { |
|
179 | - self::$cacheDir = str_replace('\\', '/', self::$cacheDir); |
|
180 | - self::$cacheDir = rtrim(self::$cacheDir, '/') . '/'; |
|
181 | - |
|
182 | - if (! is_dir(self::$cacheDir)) { |
|
183 | - throw new Exception('Cache directory doesn\'t exist: ' . self::$cacheDir); |
|
184 | - } |
|
185 | - |
|
186 | - if (! is_writable(self::$cacheDir)) { |
|
187 | - throw new Exception('Cache directory isn\'t writable: ' . self::$cacheDir); |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Delete unused cached files |
|
193 | - */ |
|
194 | - public static function cleanCache() |
|
195 | - { |
|
196 | - static $clean = false; |
|
197 | - |
|
198 | - if ($clean || empty(self::$cacheDir)) { |
|
199 | - return; |
|
200 | - } |
|
201 | - |
|
202 | - $clean = true; |
|
203 | - |
|
204 | - // only remove files with extensions created by SCSSPHP Cache |
|
205 | - // css files removed based on the list files |
|
206 | - $removeTypes = ['scsscache' => 1]; |
|
207 | - |
|
208 | - $files = scandir(self::$cacheDir); |
|
209 | - |
|
210 | - if (! $files) { |
|
211 | - return; |
|
212 | - } |
|
213 | - |
|
214 | - $checkTime = time() - self::$gcLifetime; |
|
215 | - |
|
216 | - foreach ($files as $file) { |
|
217 | - // don't delete if the file wasn't created with SCSSPHP Cache |
|
218 | - if (strpos($file, self::$prefix) !== 0) { |
|
219 | - continue; |
|
220 | - } |
|
221 | - |
|
222 | - $parts = explode('.', $file); |
|
223 | - $type = array_pop($parts); |
|
224 | - |
|
225 | - if (! isset($removeTypes[$type])) { |
|
226 | - continue; |
|
227 | - } |
|
228 | - |
|
229 | - $fullPath = self::$cacheDir . $file; |
|
230 | - $mtime = filemtime($fullPath); |
|
231 | - |
|
232 | - // don't delete if it's a relatively new file |
|
233 | - if ($mtime > $checkTime) { |
|
234 | - continue; |
|
235 | - } |
|
236 | - |
|
237 | - unlink($fullPath); |
|
238 | - } |
|
239 | - } |
|
35 | + const CACHE_VERSION = 1; |
|
36 | + |
|
37 | + // directory used for storing data |
|
38 | + public static $cacheDir = false; |
|
39 | + |
|
40 | + // prefix for the storing data |
|
41 | + public static $prefix = 'scssphp_'; |
|
42 | + |
|
43 | + // force a refresh : 'once' for refreshing the first hit on a cache only, true to never use the cache in this hit |
|
44 | + public static $forceRefresh = false; |
|
45 | + |
|
46 | + // specifies the number of seconds after which data cached will be seen as 'garbage' and potentially cleaned up |
|
47 | + public static $gcLifetime = 604800; |
|
48 | + |
|
49 | + // array of already refreshed cache if $forceRefresh==='once' |
|
50 | + protected static $refreshed = []; |
|
51 | + |
|
52 | + /** |
|
53 | + * Constructor |
|
54 | + * |
|
55 | + * @param array $options |
|
56 | + */ |
|
57 | + public function __construct($options) |
|
58 | + { |
|
59 | + // check $cacheDir |
|
60 | + if (isset($options['cacheDir'])) { |
|
61 | + self::$cacheDir = $options['cacheDir']; |
|
62 | + } |
|
63 | + |
|
64 | + if (empty(self::$cacheDir)) { |
|
65 | + throw new Exception('cacheDir not set'); |
|
66 | + } |
|
67 | + |
|
68 | + if (isset($options['prefix'])) { |
|
69 | + self::$prefix = $options['prefix']; |
|
70 | + } |
|
71 | + |
|
72 | + if (empty(self::$prefix)) { |
|
73 | + throw new Exception('prefix not set'); |
|
74 | + } |
|
75 | + |
|
76 | + if (isset($options['forceRefresh'])) { |
|
77 | + self::$forceRefresh = $options['forceRefresh']; |
|
78 | + } |
|
79 | + |
|
80 | + self::checkCacheDir(); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get the cached result of $operation on $what, |
|
85 | + * which is known as dependant from the content of $options |
|
86 | + * |
|
87 | + * @param string $operation parse, compile... |
|
88 | + * @param mixed $what content key (e.g., filename to be treated) |
|
89 | + * @param array $options any option that affect the operation result on the content |
|
90 | + * @param integer $lastModified last modified timestamp |
|
91 | + * |
|
92 | + * @return mixed |
|
93 | + * |
|
94 | + * @throws \Exception |
|
95 | + */ |
|
96 | + public function getCache($operation, $what, $options = [], $lastModified = null) |
|
97 | + { |
|
98 | + $fileCache = self::$cacheDir . self::cacheName($operation, $what, $options); |
|
99 | + |
|
100 | + if ( |
|
101 | + ((self::$forceRefresh === false) || (self::$forceRefresh === 'once' && |
|
102 | + isset(self::$refreshed[$fileCache]))) && file_exists($fileCache) |
|
103 | + ) { |
|
104 | + $cacheTime = filemtime($fileCache); |
|
105 | + |
|
106 | + if ( |
|
107 | + (\is_null($lastModified) || $cacheTime > $lastModified) && |
|
108 | + $cacheTime + self::$gcLifetime > time() |
|
109 | + ) { |
|
110 | + $c = file_get_contents($fileCache); |
|
111 | + $c = unserialize($c); |
|
112 | + |
|
113 | + if (\is_array($c) && isset($c['value'])) { |
|
114 | + return $c['value']; |
|
115 | + } |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + return null; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Put in cache the result of $operation on $what, |
|
124 | + * which is known as dependant from the content of $options |
|
125 | + * |
|
126 | + * @param string $operation |
|
127 | + * @param mixed $what |
|
128 | + * @param mixed $value |
|
129 | + * @param array $options |
|
130 | + */ |
|
131 | + public function setCache($operation, $what, $value, $options = []) |
|
132 | + { |
|
133 | + $fileCache = self::$cacheDir . self::cacheName($operation, $what, $options); |
|
134 | + |
|
135 | + $c = ['value' => $value]; |
|
136 | + $c = serialize($c); |
|
137 | + |
|
138 | + file_put_contents($fileCache, $c); |
|
139 | + |
|
140 | + if (self::$forceRefresh === 'once') { |
|
141 | + self::$refreshed[$fileCache] = true; |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get the cache name for the caching of $operation on $what, |
|
147 | + * which is known as dependant from the content of $options |
|
148 | + * |
|
149 | + * @param string $operation |
|
150 | + * @param mixed $what |
|
151 | + * @param array $options |
|
152 | + * |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + private static function cacheName($operation, $what, $options = []) |
|
156 | + { |
|
157 | + $t = [ |
|
158 | + 'version' => self::CACHE_VERSION, |
|
159 | + 'operation' => $operation, |
|
160 | + 'what' => $what, |
|
161 | + 'options' => $options |
|
162 | + ]; |
|
163 | + |
|
164 | + $t = self::$prefix |
|
165 | + . sha1(json_encode($t)) |
|
166 | + . ".$operation" |
|
167 | + . ".scsscache"; |
|
168 | + |
|
169 | + return $t; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Check that the cache dir exists and is writeable |
|
174 | + * |
|
175 | + * @throws \Exception |
|
176 | + */ |
|
177 | + public static function checkCacheDir() |
|
178 | + { |
|
179 | + self::$cacheDir = str_replace('\\', '/', self::$cacheDir); |
|
180 | + self::$cacheDir = rtrim(self::$cacheDir, '/') . '/'; |
|
181 | + |
|
182 | + if (! is_dir(self::$cacheDir)) { |
|
183 | + throw new Exception('Cache directory doesn\'t exist: ' . self::$cacheDir); |
|
184 | + } |
|
185 | + |
|
186 | + if (! is_writable(self::$cacheDir)) { |
|
187 | + throw new Exception('Cache directory isn\'t writable: ' . self::$cacheDir); |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Delete unused cached files |
|
193 | + */ |
|
194 | + public static function cleanCache() |
|
195 | + { |
|
196 | + static $clean = false; |
|
197 | + |
|
198 | + if ($clean || empty(self::$cacheDir)) { |
|
199 | + return; |
|
200 | + } |
|
201 | + |
|
202 | + $clean = true; |
|
203 | + |
|
204 | + // only remove files with extensions created by SCSSPHP Cache |
|
205 | + // css files removed based on the list files |
|
206 | + $removeTypes = ['scsscache' => 1]; |
|
207 | + |
|
208 | + $files = scandir(self::$cacheDir); |
|
209 | + |
|
210 | + if (! $files) { |
|
211 | + return; |
|
212 | + } |
|
213 | + |
|
214 | + $checkTime = time() - self::$gcLifetime; |
|
215 | + |
|
216 | + foreach ($files as $file) { |
|
217 | + // don't delete if the file wasn't created with SCSSPHP Cache |
|
218 | + if (strpos($file, self::$prefix) !== 0) { |
|
219 | + continue; |
|
220 | + } |
|
221 | + |
|
222 | + $parts = explode('.', $file); |
|
223 | + $type = array_pop($parts); |
|
224 | + |
|
225 | + if (! isset($removeTypes[$type])) { |
|
226 | + continue; |
|
227 | + } |
|
228 | + |
|
229 | + $fullPath = self::$cacheDir . $file; |
|
230 | + $mtime = filemtime($fullPath); |
|
231 | + |
|
232 | + // don't delete if it's a relatively new file |
|
233 | + if ($mtime > $checkTime) { |
|
234 | + continue; |
|
235 | + } |
|
236 | + |
|
237 | + unlink($fullPath); |
|
238 | + } |
|
239 | + } |
|
240 | 240 | } |
@@ -179,11 +179,11 @@ discard block |
||
179 | 179 | self::$cacheDir = str_replace('\\', '/', self::$cacheDir); |
180 | 180 | self::$cacheDir = rtrim(self::$cacheDir, '/') . '/'; |
181 | 181 | |
182 | - if (! is_dir(self::$cacheDir)) { |
|
182 | + if (!is_dir(self::$cacheDir)) { |
|
183 | 183 | throw new Exception('Cache directory doesn\'t exist: ' . self::$cacheDir); |
184 | 184 | } |
185 | 185 | |
186 | - if (! is_writable(self::$cacheDir)) { |
|
186 | + if (!is_writable(self::$cacheDir)) { |
|
187 | 187 | throw new Exception('Cache directory isn\'t writable: ' . self::$cacheDir); |
188 | 188 | } |
189 | 189 | } |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | |
208 | 208 | $files = scandir(self::$cacheDir); |
209 | 209 | |
210 | - if (! $files) { |
|
210 | + if (!$files) { |
|
211 | 211 | return; |
212 | 212 | } |
213 | 213 | |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | $parts = explode('.', $file); |
223 | 223 | $type = array_pop($parts); |
224 | 224 | |
225 | - if (! isset($removeTypes[$type])) { |
|
225 | + if (!isset($removeTypes[$type])) { |
|
226 | 226 | continue; |
227 | 227 | } |
228 | 228 |
@@ -19,54 +19,54 @@ |
||
19 | 19 | */ |
20 | 20 | class Type |
21 | 21 | { |
22 | - const T_ASSIGN = 'assign'; |
|
23 | - const T_AT_ROOT = 'at-root'; |
|
24 | - const T_BLOCK = 'block'; |
|
25 | - const T_BREAK = 'break'; |
|
26 | - const T_CHARSET = 'charset'; |
|
27 | - const T_COLOR = 'color'; |
|
28 | - const T_COMMENT = 'comment'; |
|
29 | - const T_CONTINUE = 'continue'; |
|
30 | - const T_CONTROL = 'control'; |
|
31 | - const T_CUSTOM_PROPERTY = 'custom'; |
|
32 | - const T_DEBUG = 'debug'; |
|
33 | - const T_DIRECTIVE = 'directive'; |
|
34 | - const T_EACH = 'each'; |
|
35 | - const T_ELSE = 'else'; |
|
36 | - const T_ELSEIF = 'elseif'; |
|
37 | - const T_ERROR = 'error'; |
|
38 | - const T_EXPRESSION = 'exp'; |
|
39 | - const T_EXTEND = 'extend'; |
|
40 | - const T_FOR = 'for'; |
|
41 | - const T_FUNCTION = 'function'; |
|
42 | - const T_FUNCTION_REFERENCE = 'function-reference'; |
|
43 | - const T_FUNCTION_CALL = 'fncall'; |
|
44 | - const T_HSL = 'hsl'; |
|
45 | - const T_IF = 'if'; |
|
46 | - const T_IMPORT = 'import'; |
|
47 | - const T_INCLUDE = 'include'; |
|
48 | - const T_INTERPOLATE = 'interpolate'; |
|
49 | - const T_INTERPOLATED = 'interpolated'; |
|
50 | - const T_KEYWORD = 'keyword'; |
|
51 | - const T_LIST = 'list'; |
|
52 | - const T_MAP = 'map'; |
|
53 | - const T_MEDIA = 'media'; |
|
54 | - const T_MEDIA_EXPRESSION = 'mediaExp'; |
|
55 | - const T_MEDIA_TYPE = 'mediaType'; |
|
56 | - const T_MEDIA_VALUE = 'mediaValue'; |
|
57 | - const T_MIXIN = 'mixin'; |
|
58 | - const T_MIXIN_CONTENT = 'mixin_content'; |
|
59 | - const T_NESTED_PROPERTY = 'nestedprop'; |
|
60 | - const T_NOT = 'not'; |
|
61 | - const T_NULL = 'null'; |
|
62 | - const T_NUMBER = 'number'; |
|
63 | - const T_RETURN = 'return'; |
|
64 | - const T_ROOT = 'root'; |
|
65 | - const T_SCSSPHP_IMPORT_ONCE = 'scssphp-import-once'; |
|
66 | - const T_SELF = 'self'; |
|
67 | - const T_STRING = 'string'; |
|
68 | - const T_UNARY = 'unary'; |
|
69 | - const T_VARIABLE = 'var'; |
|
70 | - const T_WARN = 'warn'; |
|
71 | - const T_WHILE = 'while'; |
|
22 | + const T_ASSIGN = 'assign'; |
|
23 | + const T_AT_ROOT = 'at-root'; |
|
24 | + const T_BLOCK = 'block'; |
|
25 | + const T_BREAK = 'break'; |
|
26 | + const T_CHARSET = 'charset'; |
|
27 | + const T_COLOR = 'color'; |
|
28 | + const T_COMMENT = 'comment'; |
|
29 | + const T_CONTINUE = 'continue'; |
|
30 | + const T_CONTROL = 'control'; |
|
31 | + const T_CUSTOM_PROPERTY = 'custom'; |
|
32 | + const T_DEBUG = 'debug'; |
|
33 | + const T_DIRECTIVE = 'directive'; |
|
34 | + const T_EACH = 'each'; |
|
35 | + const T_ELSE = 'else'; |
|
36 | + const T_ELSEIF = 'elseif'; |
|
37 | + const T_ERROR = 'error'; |
|
38 | + const T_EXPRESSION = 'exp'; |
|
39 | + const T_EXTEND = 'extend'; |
|
40 | + const T_FOR = 'for'; |
|
41 | + const T_FUNCTION = 'function'; |
|
42 | + const T_FUNCTION_REFERENCE = 'function-reference'; |
|
43 | + const T_FUNCTION_CALL = 'fncall'; |
|
44 | + const T_HSL = 'hsl'; |
|
45 | + const T_IF = 'if'; |
|
46 | + const T_IMPORT = 'import'; |
|
47 | + const T_INCLUDE = 'include'; |
|
48 | + const T_INTERPOLATE = 'interpolate'; |
|
49 | + const T_INTERPOLATED = 'interpolated'; |
|
50 | + const T_KEYWORD = 'keyword'; |
|
51 | + const T_LIST = 'list'; |
|
52 | + const T_MAP = 'map'; |
|
53 | + const T_MEDIA = 'media'; |
|
54 | + const T_MEDIA_EXPRESSION = 'mediaExp'; |
|
55 | + const T_MEDIA_TYPE = 'mediaType'; |
|
56 | + const T_MEDIA_VALUE = 'mediaValue'; |
|
57 | + const T_MIXIN = 'mixin'; |
|
58 | + const T_MIXIN_CONTENT = 'mixin_content'; |
|
59 | + const T_NESTED_PROPERTY = 'nestedprop'; |
|
60 | + const T_NOT = 'not'; |
|
61 | + const T_NULL = 'null'; |
|
62 | + const T_NUMBER = 'number'; |
|
63 | + const T_RETURN = 'return'; |
|
64 | + const T_ROOT = 'root'; |
|
65 | + const T_SCSSPHP_IMPORT_ONCE = 'scssphp-import-once'; |
|
66 | + const T_SELF = 'self'; |
|
67 | + const T_STRING = 'string'; |
|
68 | + const T_UNARY = 'unary'; |
|
69 | + const T_VARIABLE = 'var'; |
|
70 | + const T_WARN = 'warn'; |
|
71 | + const T_WHILE = 'while'; |
|
72 | 72 | } |
@@ -19,5 +19,5 @@ |
||
19 | 19 | */ |
20 | 20 | class Version |
21 | 21 | { |
22 | - const VERSION = '1.2'; |
|
22 | + const VERSION = '1.2'; |
|
23 | 23 | } |
@@ -22,82 +22,82 @@ |
||
22 | 22 | */ |
23 | 23 | class Util |
24 | 24 | { |
25 | - /** |
|
26 | - * Asserts that `value` falls within `range` (inclusive), leaving |
|
27 | - * room for slight floating-point errors. |
|
28 | - * |
|
29 | - * @param string $name The name of the value. Used in the error message. |
|
30 | - * @param \ScssPhp\ScssPhp\Base\Range $range Range of values. |
|
31 | - * @param array $value The value to check. |
|
32 | - * @param string $unit The unit of the value. Used in error reporting. |
|
33 | - * |
|
34 | - * @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
|
35 | - * |
|
36 | - * @throws \ScssPhp\ScssPhp\Exception\RangeException |
|
37 | - */ |
|
38 | - public static function checkRange($name, Range $range, $value, $unit = '') |
|
39 | - { |
|
40 | - $val = $value[1]; |
|
41 | - $grace = new Range(-0.00001, 0.00001); |
|
25 | + /** |
|
26 | + * Asserts that `value` falls within `range` (inclusive), leaving |
|
27 | + * room for slight floating-point errors. |
|
28 | + * |
|
29 | + * @param string $name The name of the value. Used in the error message. |
|
30 | + * @param \ScssPhp\ScssPhp\Base\Range $range Range of values. |
|
31 | + * @param array $value The value to check. |
|
32 | + * @param string $unit The unit of the value. Used in error reporting. |
|
33 | + * |
|
34 | + * @return mixed `value` adjusted to fall within range, if it was outside by a floating-point margin. |
|
35 | + * |
|
36 | + * @throws \ScssPhp\ScssPhp\Exception\RangeException |
|
37 | + */ |
|
38 | + public static function checkRange($name, Range $range, $value, $unit = '') |
|
39 | + { |
|
40 | + $val = $value[1]; |
|
41 | + $grace = new Range(-0.00001, 0.00001); |
|
42 | 42 | |
43 | - if (! \is_numeric($val)) { |
|
44 | - throw new RangeException("$name {$val} is not a number."); |
|
45 | - } |
|
43 | + if (! \is_numeric($val)) { |
|
44 | + throw new RangeException("$name {$val} is not a number."); |
|
45 | + } |
|
46 | 46 | |
47 | - if ($range->includes($val)) { |
|
48 | - return $val; |
|
49 | - } |
|
47 | + if ($range->includes($val)) { |
|
48 | + return $val; |
|
49 | + } |
|
50 | 50 | |
51 | - if ($grace->includes($val - $range->first)) { |
|
52 | - return $range->first; |
|
53 | - } |
|
51 | + if ($grace->includes($val - $range->first)) { |
|
52 | + return $range->first; |
|
53 | + } |
|
54 | 54 | |
55 | - if ($grace->includes($val - $range->last)) { |
|
56 | - return $range->last; |
|
57 | - } |
|
55 | + if ($grace->includes($val - $range->last)) { |
|
56 | + return $range->last; |
|
57 | + } |
|
58 | 58 | |
59 | - throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
|
60 | - } |
|
59 | + throw new RangeException("$name {$val} must be between {$range->first} and {$range->last}$unit"); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Encode URI component |
|
64 | - * |
|
65 | - * @param string $string |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public static function encodeURIComponent($string) |
|
70 | - { |
|
71 | - $revert = ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')']; |
|
62 | + /** |
|
63 | + * Encode URI component |
|
64 | + * |
|
65 | + * @param string $string |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public static function encodeURIComponent($string) |
|
70 | + { |
|
71 | + $revert = ['%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')']; |
|
72 | 72 | |
73 | - return strtr(rawurlencode($string), $revert); |
|
74 | - } |
|
73 | + return strtr(rawurlencode($string), $revert); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * mb_chr() wrapper |
|
78 | - * |
|
79 | - * @param integer $code |
|
80 | - * |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public static function mbChr($code) |
|
84 | - { |
|
85 | - // Use the native implementation if available. |
|
86 | - if (\function_exists('mb_chr')) { |
|
87 | - return mb_chr($code, 'UTF-8'); |
|
88 | - } |
|
76 | + /** |
|
77 | + * mb_chr() wrapper |
|
78 | + * |
|
79 | + * @param integer $code |
|
80 | + * |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public static function mbChr($code) |
|
84 | + { |
|
85 | + // Use the native implementation if available. |
|
86 | + if (\function_exists('mb_chr')) { |
|
87 | + return mb_chr($code, 'UTF-8'); |
|
88 | + } |
|
89 | 89 | |
90 | - if (0x80 > $code %= 0x200000) { |
|
91 | - $s = \chr($code); |
|
92 | - } elseif (0x800 > $code) { |
|
93 | - $s = \chr(0xC0 | $code >> 6) . \chr(0x80 | $code & 0x3F); |
|
94 | - } elseif (0x10000 > $code) { |
|
95 | - $s = \chr(0xE0 | $code >> 12) . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
96 | - } else { |
|
97 | - $s = \chr(0xF0 | $code >> 18) . \chr(0x80 | $code >> 12 & 0x3F) |
|
98 | - . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
99 | - } |
|
90 | + if (0x80 > $code %= 0x200000) { |
|
91 | + $s = \chr($code); |
|
92 | + } elseif (0x800 > $code) { |
|
93 | + $s = \chr(0xC0 | $code >> 6) . \chr(0x80 | $code & 0x3F); |
|
94 | + } elseif (0x10000 > $code) { |
|
95 | + $s = \chr(0xE0 | $code >> 12) . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
96 | + } else { |
|
97 | + $s = \chr(0xF0 | $code >> 18) . \chr(0x80 | $code >> 12 & 0x3F) |
|
98 | + . \chr(0x80 | $code >> 6 & 0x3F) . \chr(0x80 | $code & 0x3F); |
|
99 | + } |
|
100 | 100 | |
101 | - return $s; |
|
102 | - } |
|
101 | + return $s; |
|
102 | + } |
|
103 | 103 | } |
@@ -40,7 +40,7 @@ |
||
40 | 40 | $val = $value[1]; |
41 | 41 | $grace = new Range(-0.00001, 0.00001); |
42 | 42 | |
43 | - if (! \is_numeric($val)) { |
|
43 | + if (!\is_numeric($val)) { |
|
44 | 44 | throw new RangeException("$name {$val} is not a number."); |
45 | 45 | } |
46 | 46 |
@@ -39,113 +39,113 @@ |
||
39 | 39 | */ |
40 | 40 | class Base64VLQ |
41 | 41 | { |
42 | - // A Base64 VLQ digit can represent 5 bits, so it is base-32. |
|
43 | - const VLQ_BASE_SHIFT = 5; |
|
44 | - |
|
45 | - // A mask of bits for a VLQ digit (11111), 31 decimal. |
|
46 | - const VLQ_BASE_MASK = 31; |
|
47 | - |
|
48 | - // The continuation bit is the 6th bit. |
|
49 | - const VLQ_CONTINUATION_BIT = 32; |
|
50 | - |
|
51 | - /** |
|
52 | - * Returns the VLQ encoded value. |
|
53 | - * |
|
54 | - * @param integer $value |
|
55 | - * |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public static function encode($value) |
|
59 | - { |
|
60 | - $encoded = ''; |
|
61 | - $vlq = self::toVLQSigned($value); |
|
62 | - |
|
63 | - do { |
|
64 | - $digit = $vlq & self::VLQ_BASE_MASK; |
|
65 | - |
|
66 | - //$vlq >>>= self::VLQ_BASE_SHIFT; // unsigned right shift |
|
67 | - $vlq = (($vlq >> 1) & PHP_INT_MAX) >> (self::VLQ_BASE_SHIFT - 1); |
|
68 | - |
|
69 | - if ($vlq > 0) { |
|
70 | - $digit |= self::VLQ_CONTINUATION_BIT; |
|
71 | - } |
|
72 | - |
|
73 | - $encoded .= Base64::encode($digit); |
|
74 | - } while ($vlq > 0); |
|
75 | - |
|
76 | - return $encoded; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Decodes VLQValue. |
|
81 | - * |
|
82 | - * @param string $str |
|
83 | - * @param integer $index |
|
84 | - * |
|
85 | - * @return integer |
|
86 | - */ |
|
87 | - public static function decode($str, &$index) |
|
88 | - { |
|
89 | - $result = 0; |
|
90 | - $shift = 0; |
|
91 | - |
|
92 | - do { |
|
93 | - $c = $str[$index++]; |
|
94 | - $digit = Base64::decode($c); |
|
95 | - $continuation = ($digit & self::VLQ_CONTINUATION_BIT) != 0; |
|
96 | - $digit &= self::VLQ_BASE_MASK; |
|
97 | - $result = $result + ($digit << $shift); |
|
98 | - $shift = $shift + self::VLQ_BASE_SHIFT; |
|
99 | - } while ($continuation); |
|
100 | - |
|
101 | - return self::fromVLQSigned($result); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Converts from a two-complement value to a value where the sign bit is |
|
106 | - * is placed in the least significant bit. For example, as decimals: |
|
107 | - * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) |
|
108 | - * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) |
|
109 | - * |
|
110 | - * @param integer $value |
|
111 | - * |
|
112 | - * @return integer |
|
113 | - */ |
|
114 | - private static function toVLQSigned($value) |
|
115 | - { |
|
116 | - if ($value < 0) { |
|
117 | - return ((-$value) << 1) + 1; |
|
118 | - } |
|
119 | - |
|
120 | - return ($value << 1) + 0; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Converts to a two-complement value from a value where the sign bit is |
|
125 | - * is placed in the least significant bit. For example, as decimals: |
|
126 | - * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 |
|
127 | - * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 |
|
128 | - * |
|
129 | - * @param integer $value |
|
130 | - * |
|
131 | - * @return integer |
|
132 | - */ |
|
133 | - private static function fromVLQSigned($value) |
|
134 | - { |
|
135 | - $negate = ($value & 1) === 1; |
|
136 | - |
|
137 | - //$value >>>= 1; // unsigned right shift |
|
138 | - $value = ($value >> 1) & PHP_INT_MAX; |
|
139 | - |
|
140 | - if (! $negate) { |
|
141 | - return $value; |
|
142 | - } |
|
143 | - |
|
144 | - // We need to OR 0x80000000 here to ensure the 32nd bit (the sign bit) is |
|
145 | - // always set for negative numbers. If `value` were 1, (meaning `negate` is |
|
146 | - // true and all other bits were zeros), `value` would now be 0. -0 is just |
|
147 | - // 0, and doesn't flip the 32nd bit as intended. All positive numbers will |
|
148 | - // successfully flip the 32nd bit without issue, so it's a noop for them. |
|
149 | - return -$value | 0x80000000; |
|
150 | - } |
|
42 | + // A Base64 VLQ digit can represent 5 bits, so it is base-32. |
|
43 | + const VLQ_BASE_SHIFT = 5; |
|
44 | + |
|
45 | + // A mask of bits for a VLQ digit (11111), 31 decimal. |
|
46 | + const VLQ_BASE_MASK = 31; |
|
47 | + |
|
48 | + // The continuation bit is the 6th bit. |
|
49 | + const VLQ_CONTINUATION_BIT = 32; |
|
50 | + |
|
51 | + /** |
|
52 | + * Returns the VLQ encoded value. |
|
53 | + * |
|
54 | + * @param integer $value |
|
55 | + * |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public static function encode($value) |
|
59 | + { |
|
60 | + $encoded = ''; |
|
61 | + $vlq = self::toVLQSigned($value); |
|
62 | + |
|
63 | + do { |
|
64 | + $digit = $vlq & self::VLQ_BASE_MASK; |
|
65 | + |
|
66 | + //$vlq >>>= self::VLQ_BASE_SHIFT; // unsigned right shift |
|
67 | + $vlq = (($vlq >> 1) & PHP_INT_MAX) >> (self::VLQ_BASE_SHIFT - 1); |
|
68 | + |
|
69 | + if ($vlq > 0) { |
|
70 | + $digit |= self::VLQ_CONTINUATION_BIT; |
|
71 | + } |
|
72 | + |
|
73 | + $encoded .= Base64::encode($digit); |
|
74 | + } while ($vlq > 0); |
|
75 | + |
|
76 | + return $encoded; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Decodes VLQValue. |
|
81 | + * |
|
82 | + * @param string $str |
|
83 | + * @param integer $index |
|
84 | + * |
|
85 | + * @return integer |
|
86 | + */ |
|
87 | + public static function decode($str, &$index) |
|
88 | + { |
|
89 | + $result = 0; |
|
90 | + $shift = 0; |
|
91 | + |
|
92 | + do { |
|
93 | + $c = $str[$index++]; |
|
94 | + $digit = Base64::decode($c); |
|
95 | + $continuation = ($digit & self::VLQ_CONTINUATION_BIT) != 0; |
|
96 | + $digit &= self::VLQ_BASE_MASK; |
|
97 | + $result = $result + ($digit << $shift); |
|
98 | + $shift = $shift + self::VLQ_BASE_SHIFT; |
|
99 | + } while ($continuation); |
|
100 | + |
|
101 | + return self::fromVLQSigned($result); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Converts from a two-complement value to a value where the sign bit is |
|
106 | + * is placed in the least significant bit. For example, as decimals: |
|
107 | + * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) |
|
108 | + * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) |
|
109 | + * |
|
110 | + * @param integer $value |
|
111 | + * |
|
112 | + * @return integer |
|
113 | + */ |
|
114 | + private static function toVLQSigned($value) |
|
115 | + { |
|
116 | + if ($value < 0) { |
|
117 | + return ((-$value) << 1) + 1; |
|
118 | + } |
|
119 | + |
|
120 | + return ($value << 1) + 0; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Converts to a two-complement value from a value where the sign bit is |
|
125 | + * is placed in the least significant bit. For example, as decimals: |
|
126 | + * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 |
|
127 | + * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 |
|
128 | + * |
|
129 | + * @param integer $value |
|
130 | + * |
|
131 | + * @return integer |
|
132 | + */ |
|
133 | + private static function fromVLQSigned($value) |
|
134 | + { |
|
135 | + $negate = ($value & 1) === 1; |
|
136 | + |
|
137 | + //$value >>>= 1; // unsigned right shift |
|
138 | + $value = ($value >> 1) & PHP_INT_MAX; |
|
139 | + |
|
140 | + if (! $negate) { |
|
141 | + return $value; |
|
142 | + } |
|
143 | + |
|
144 | + // We need to OR 0x80000000 here to ensure the 32nd bit (the sign bit) is |
|
145 | + // always set for negative numbers. If `value` were 1, (meaning `negate` is |
|
146 | + // true and all other bits were zeros), `value` would now be 0. -0 is just |
|
147 | + // 0, and doesn't flip the 32nd bit as intended. All positive numbers will |
|
148 | + // successfully flip the 32nd bit without issue, so it's a noop for them. |
|
149 | + return -$value | 0x80000000; |
|
150 | + } |
|
151 | 151 | } |
@@ -137,7 +137,7 @@ |
||
137 | 137 | //$value >>>= 1; // unsigned right shift |
138 | 138 | $value = ($value >> 1) & PHP_INT_MAX; |
139 | 139 | |
140 | - if (! $negate) { |
|
140 | + if (!$negate) { |
|
141 | 141 | return $value; |
142 | 142 | } |
143 | 143 |
@@ -19,167 +19,167 @@ |
||
19 | 19 | */ |
20 | 20 | class Base64 |
21 | 21 | { |
22 | - /** |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - private static $encodingMap = [ |
|
26 | - 0 => 'A', |
|
27 | - 1 => 'B', |
|
28 | - 2 => 'C', |
|
29 | - 3 => 'D', |
|
30 | - 4 => 'E', |
|
31 | - 5 => 'F', |
|
32 | - 6 => 'G', |
|
33 | - 7 => 'H', |
|
34 | - 8 => 'I', |
|
35 | - 9 => 'J', |
|
36 | - 10 => 'K', |
|
37 | - 11 => 'L', |
|
38 | - 12 => 'M', |
|
39 | - 13 => 'N', |
|
40 | - 14 => 'O', |
|
41 | - 15 => 'P', |
|
42 | - 16 => 'Q', |
|
43 | - 17 => 'R', |
|
44 | - 18 => 'S', |
|
45 | - 19 => 'T', |
|
46 | - 20 => 'U', |
|
47 | - 21 => 'V', |
|
48 | - 22 => 'W', |
|
49 | - 23 => 'X', |
|
50 | - 24 => 'Y', |
|
51 | - 25 => 'Z', |
|
52 | - 26 => 'a', |
|
53 | - 27 => 'b', |
|
54 | - 28 => 'c', |
|
55 | - 29 => 'd', |
|
56 | - 30 => 'e', |
|
57 | - 31 => 'f', |
|
58 | - 32 => 'g', |
|
59 | - 33 => 'h', |
|
60 | - 34 => 'i', |
|
61 | - 35 => 'j', |
|
62 | - 36 => 'k', |
|
63 | - 37 => 'l', |
|
64 | - 38 => 'm', |
|
65 | - 39 => 'n', |
|
66 | - 40 => 'o', |
|
67 | - 41 => 'p', |
|
68 | - 42 => 'q', |
|
69 | - 43 => 'r', |
|
70 | - 44 => 's', |
|
71 | - 45 => 't', |
|
72 | - 46 => 'u', |
|
73 | - 47 => 'v', |
|
74 | - 48 => 'w', |
|
75 | - 49 => 'x', |
|
76 | - 50 => 'y', |
|
77 | - 51 => 'z', |
|
78 | - 52 => '0', |
|
79 | - 53 => '1', |
|
80 | - 54 => '2', |
|
81 | - 55 => '3', |
|
82 | - 56 => '4', |
|
83 | - 57 => '5', |
|
84 | - 58 => '6', |
|
85 | - 59 => '7', |
|
86 | - 60 => '8', |
|
87 | - 61 => '9', |
|
88 | - 62 => '+', |
|
89 | - 63 => '/', |
|
90 | - ]; |
|
22 | + /** |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + private static $encodingMap = [ |
|
26 | + 0 => 'A', |
|
27 | + 1 => 'B', |
|
28 | + 2 => 'C', |
|
29 | + 3 => 'D', |
|
30 | + 4 => 'E', |
|
31 | + 5 => 'F', |
|
32 | + 6 => 'G', |
|
33 | + 7 => 'H', |
|
34 | + 8 => 'I', |
|
35 | + 9 => 'J', |
|
36 | + 10 => 'K', |
|
37 | + 11 => 'L', |
|
38 | + 12 => 'M', |
|
39 | + 13 => 'N', |
|
40 | + 14 => 'O', |
|
41 | + 15 => 'P', |
|
42 | + 16 => 'Q', |
|
43 | + 17 => 'R', |
|
44 | + 18 => 'S', |
|
45 | + 19 => 'T', |
|
46 | + 20 => 'U', |
|
47 | + 21 => 'V', |
|
48 | + 22 => 'W', |
|
49 | + 23 => 'X', |
|
50 | + 24 => 'Y', |
|
51 | + 25 => 'Z', |
|
52 | + 26 => 'a', |
|
53 | + 27 => 'b', |
|
54 | + 28 => 'c', |
|
55 | + 29 => 'd', |
|
56 | + 30 => 'e', |
|
57 | + 31 => 'f', |
|
58 | + 32 => 'g', |
|
59 | + 33 => 'h', |
|
60 | + 34 => 'i', |
|
61 | + 35 => 'j', |
|
62 | + 36 => 'k', |
|
63 | + 37 => 'l', |
|
64 | + 38 => 'm', |
|
65 | + 39 => 'n', |
|
66 | + 40 => 'o', |
|
67 | + 41 => 'p', |
|
68 | + 42 => 'q', |
|
69 | + 43 => 'r', |
|
70 | + 44 => 's', |
|
71 | + 45 => 't', |
|
72 | + 46 => 'u', |
|
73 | + 47 => 'v', |
|
74 | + 48 => 'w', |
|
75 | + 49 => 'x', |
|
76 | + 50 => 'y', |
|
77 | + 51 => 'z', |
|
78 | + 52 => '0', |
|
79 | + 53 => '1', |
|
80 | + 54 => '2', |
|
81 | + 55 => '3', |
|
82 | + 56 => '4', |
|
83 | + 57 => '5', |
|
84 | + 58 => '6', |
|
85 | + 59 => '7', |
|
86 | + 60 => '8', |
|
87 | + 61 => '9', |
|
88 | + 62 => '+', |
|
89 | + 63 => '/', |
|
90 | + ]; |
|
91 | 91 | |
92 | - /** |
|
93 | - * @var array |
|
94 | - */ |
|
95 | - private static $decodingMap = [ |
|
96 | - 'A' => 0, |
|
97 | - 'B' => 1, |
|
98 | - 'C' => 2, |
|
99 | - 'D' => 3, |
|
100 | - 'E' => 4, |
|
101 | - 'F' => 5, |
|
102 | - 'G' => 6, |
|
103 | - 'H' => 7, |
|
104 | - 'I' => 8, |
|
105 | - 'J' => 9, |
|
106 | - 'K' => 10, |
|
107 | - 'L' => 11, |
|
108 | - 'M' => 12, |
|
109 | - 'N' => 13, |
|
110 | - 'O' => 14, |
|
111 | - 'P' => 15, |
|
112 | - 'Q' => 16, |
|
113 | - 'R' => 17, |
|
114 | - 'S' => 18, |
|
115 | - 'T' => 19, |
|
116 | - 'U' => 20, |
|
117 | - 'V' => 21, |
|
118 | - 'W' => 22, |
|
119 | - 'X' => 23, |
|
120 | - 'Y' => 24, |
|
121 | - 'Z' => 25, |
|
122 | - 'a' => 26, |
|
123 | - 'b' => 27, |
|
124 | - 'c' => 28, |
|
125 | - 'd' => 29, |
|
126 | - 'e' => 30, |
|
127 | - 'f' => 31, |
|
128 | - 'g' => 32, |
|
129 | - 'h' => 33, |
|
130 | - 'i' => 34, |
|
131 | - 'j' => 35, |
|
132 | - 'k' => 36, |
|
133 | - 'l' => 37, |
|
134 | - 'm' => 38, |
|
135 | - 'n' => 39, |
|
136 | - 'o' => 40, |
|
137 | - 'p' => 41, |
|
138 | - 'q' => 42, |
|
139 | - 'r' => 43, |
|
140 | - 's' => 44, |
|
141 | - 't' => 45, |
|
142 | - 'u' => 46, |
|
143 | - 'v' => 47, |
|
144 | - 'w' => 48, |
|
145 | - 'x' => 49, |
|
146 | - 'y' => 50, |
|
147 | - 'z' => 51, |
|
148 | - 0 => 52, |
|
149 | - 1 => 53, |
|
150 | - 2 => 54, |
|
151 | - 3 => 55, |
|
152 | - 4 => 56, |
|
153 | - 5 => 57, |
|
154 | - 6 => 58, |
|
155 | - 7 => 59, |
|
156 | - 8 => 60, |
|
157 | - 9 => 61, |
|
158 | - '+' => 62, |
|
159 | - '/' => 63, |
|
160 | - ]; |
|
92 | + /** |
|
93 | + * @var array |
|
94 | + */ |
|
95 | + private static $decodingMap = [ |
|
96 | + 'A' => 0, |
|
97 | + 'B' => 1, |
|
98 | + 'C' => 2, |
|
99 | + 'D' => 3, |
|
100 | + 'E' => 4, |
|
101 | + 'F' => 5, |
|
102 | + 'G' => 6, |
|
103 | + 'H' => 7, |
|
104 | + 'I' => 8, |
|
105 | + 'J' => 9, |
|
106 | + 'K' => 10, |
|
107 | + 'L' => 11, |
|
108 | + 'M' => 12, |
|
109 | + 'N' => 13, |
|
110 | + 'O' => 14, |
|
111 | + 'P' => 15, |
|
112 | + 'Q' => 16, |
|
113 | + 'R' => 17, |
|
114 | + 'S' => 18, |
|
115 | + 'T' => 19, |
|
116 | + 'U' => 20, |
|
117 | + 'V' => 21, |
|
118 | + 'W' => 22, |
|
119 | + 'X' => 23, |
|
120 | + 'Y' => 24, |
|
121 | + 'Z' => 25, |
|
122 | + 'a' => 26, |
|
123 | + 'b' => 27, |
|
124 | + 'c' => 28, |
|
125 | + 'd' => 29, |
|
126 | + 'e' => 30, |
|
127 | + 'f' => 31, |
|
128 | + 'g' => 32, |
|
129 | + 'h' => 33, |
|
130 | + 'i' => 34, |
|
131 | + 'j' => 35, |
|
132 | + 'k' => 36, |
|
133 | + 'l' => 37, |
|
134 | + 'm' => 38, |
|
135 | + 'n' => 39, |
|
136 | + 'o' => 40, |
|
137 | + 'p' => 41, |
|
138 | + 'q' => 42, |
|
139 | + 'r' => 43, |
|
140 | + 's' => 44, |
|
141 | + 't' => 45, |
|
142 | + 'u' => 46, |
|
143 | + 'v' => 47, |
|
144 | + 'w' => 48, |
|
145 | + 'x' => 49, |
|
146 | + 'y' => 50, |
|
147 | + 'z' => 51, |
|
148 | + 0 => 52, |
|
149 | + 1 => 53, |
|
150 | + 2 => 54, |
|
151 | + 3 => 55, |
|
152 | + 4 => 56, |
|
153 | + 5 => 57, |
|
154 | + 6 => 58, |
|
155 | + 7 => 59, |
|
156 | + 8 => 60, |
|
157 | + 9 => 61, |
|
158 | + '+' => 62, |
|
159 | + '/' => 63, |
|
160 | + ]; |
|
161 | 161 | |
162 | - /** |
|
163 | - * Convert to base64 |
|
164 | - * |
|
165 | - * @param integer $value |
|
166 | - * |
|
167 | - * @return string |
|
168 | - */ |
|
169 | - public static function encode($value) |
|
170 | - { |
|
171 | - return self::$encodingMap[$value]; |
|
172 | - } |
|
162 | + /** |
|
163 | + * Convert to base64 |
|
164 | + * |
|
165 | + * @param integer $value |
|
166 | + * |
|
167 | + * @return string |
|
168 | + */ |
|
169 | + public static function encode($value) |
|
170 | + { |
|
171 | + return self::$encodingMap[$value]; |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Convert from base64 |
|
176 | - * |
|
177 | - * @param string $value |
|
178 | - * |
|
179 | - * @return integer |
|
180 | - */ |
|
181 | - public static function decode($value) |
|
182 | - { |
|
183 | - return self::$decodingMap[$value]; |
|
184 | - } |
|
174 | + /** |
|
175 | + * Convert from base64 |
|
176 | + * |
|
177 | + * @param string $value |
|
178 | + * |
|
179 | + * @return integer |
|
180 | + */ |
|
181 | + public static function decode($value) |
|
182 | + { |
|
183 | + return self::$decodingMap[$value]; |
|
184 | + } |
|
185 | 185 | } |
@@ -24,326 +24,326 @@ |
||
24 | 24 | */ |
25 | 25 | class SourceMapGenerator |
26 | 26 | { |
27 | - /** |
|
28 | - * What version of source map does the generator generate? |
|
29 | - */ |
|
30 | - const VERSION = 3; |
|
31 | - |
|
32 | - /** |
|
33 | - * Array of default options |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $defaultOptions = [ |
|
38 | - // an optional source root, useful for relocating source files |
|
39 | - // on a server or removing repeated values in the 'sources' entry. |
|
40 | - // This value is prepended to the individual entries in the 'source' field. |
|
41 | - 'sourceRoot' => '', |
|
42 | - |
|
43 | - // an optional name of the generated code that this source map is associated with. |
|
44 | - 'sourceMapFilename' => null, |
|
45 | - |
|
46 | - // url of the map |
|
47 | - 'sourceMapURL' => null, |
|
48 | - |
|
49 | - // absolute path to a file to write the map to |
|
50 | - 'sourceMapWriteTo' => null, |
|
51 | - |
|
52 | - // output source contents? |
|
53 | - 'outputSourceFiles' => false, |
|
54 | - |
|
55 | - // base path for filename normalization |
|
56 | - 'sourceMapRootpath' => '', |
|
57 | - |
|
58 | - // base path for filename normalization |
|
59 | - 'sourceMapBasepath' => '' |
|
60 | - ]; |
|
61 | - |
|
62 | - /** |
|
63 | - * The base64 VLQ encoder |
|
64 | - * |
|
65 | - * @var \ScssPhp\ScssPhp\SourceMap\Base64VLQ |
|
66 | - */ |
|
67 | - protected $encoder; |
|
68 | - |
|
69 | - /** |
|
70 | - * Array of mappings |
|
71 | - * |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - protected $mappings = []; |
|
75 | - |
|
76 | - /** |
|
77 | - * Array of contents map |
|
78 | - * |
|
79 | - * @var array |
|
80 | - */ |
|
81 | - protected $contentsMap = []; |
|
82 | - |
|
83 | - /** |
|
84 | - * File to content map |
|
85 | - * |
|
86 | - * @var array |
|
87 | - */ |
|
88 | - protected $sources = []; |
|
89 | - protected $sourceKeys = []; |
|
90 | - |
|
91 | - /** |
|
92 | - * @var array |
|
93 | - */ |
|
94 | - private $options; |
|
95 | - |
|
96 | - public function __construct(array $options = []) |
|
97 | - { |
|
98 | - $this->options = array_merge($this->defaultOptions, $options); |
|
99 | - $this->encoder = new Base64VLQ(); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Adds a mapping |
|
104 | - * |
|
105 | - * @param integer $generatedLine The line number in generated file |
|
106 | - * @param integer $generatedColumn The column number in generated file |
|
107 | - * @param integer $originalLine The line number in original file |
|
108 | - * @param integer $originalColumn The column number in original file |
|
109 | - * @param string $sourceFile The original source file |
|
110 | - */ |
|
111 | - public function addMapping($generatedLine, $generatedColumn, $originalLine, $originalColumn, $sourceFile) |
|
112 | - { |
|
113 | - $this->mappings[] = [ |
|
114 | - 'generated_line' => $generatedLine, |
|
115 | - 'generated_column' => $generatedColumn, |
|
116 | - 'original_line' => $originalLine, |
|
117 | - 'original_column' => $originalColumn, |
|
118 | - 'source_file' => $sourceFile |
|
119 | - ]; |
|
120 | - |
|
121 | - $this->sources[$sourceFile] = $sourceFile; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Saves the source map to a file |
|
126 | - * |
|
127 | - * @param string $content The content to write |
|
128 | - * |
|
129 | - * @return string |
|
130 | - * |
|
131 | - * @throws \ScssPhp\ScssPhp\Exception\CompilerException If the file could not be saved |
|
132 | - */ |
|
133 | - public function saveMap($content) |
|
134 | - { |
|
135 | - $file = $this->options['sourceMapWriteTo']; |
|
136 | - $dir = \dirname($file); |
|
137 | - |
|
138 | - // directory does not exist |
|
139 | - if (! is_dir($dir)) { |
|
140 | - // FIXME: create the dir automatically? |
|
141 | - throw new CompilerException( |
|
142 | - sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir) |
|
143 | - ); |
|
144 | - } |
|
145 | - |
|
146 | - // FIXME: proper saving, with dir write check! |
|
147 | - if (file_put_contents($file, $content) === false) { |
|
148 | - throw new CompilerException(sprintf('Cannot save the source map to "%s"', $file)); |
|
149 | - } |
|
150 | - |
|
151 | - return $this->options['sourceMapURL']; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Generates the JSON source map |
|
156 | - * |
|
157 | - * @return string |
|
158 | - * |
|
159 | - * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# |
|
160 | - */ |
|
161 | - public function generateJson() |
|
162 | - { |
|
163 | - $sourceMap = []; |
|
164 | - $mappings = $this->generateMappings(); |
|
165 | - |
|
166 | - // File version (always the first entry in the object) and must be a positive integer. |
|
167 | - $sourceMap['version'] = self::VERSION; |
|
168 | - |
|
169 | - // An optional name of the generated code that this source map is associated with. |
|
170 | - $file = $this->options['sourceMapFilename']; |
|
171 | - |
|
172 | - if ($file) { |
|
173 | - $sourceMap['file'] = $file; |
|
174 | - } |
|
175 | - |
|
176 | - // An optional source root, useful for relocating source files on a server or removing repeated values in the |
|
177 | - // 'sources' entry. This value is prepended to the individual entries in the 'source' field. |
|
178 | - $root = $this->options['sourceRoot']; |
|
179 | - |
|
180 | - if ($root) { |
|
181 | - $sourceMap['sourceRoot'] = $root; |
|
182 | - } |
|
183 | - |
|
184 | - // A list of original sources used by the 'mappings' entry. |
|
185 | - $sourceMap['sources'] = []; |
|
186 | - |
|
187 | - foreach ($this->sources as $sourceUri => $sourceFilename) { |
|
188 | - $sourceMap['sources'][] = $this->normalizeFilename($sourceFilename); |
|
189 | - } |
|
190 | - |
|
191 | - // A list of symbol names used by the 'mappings' entry. |
|
192 | - $sourceMap['names'] = []; |
|
193 | - |
|
194 | - // A string with the encoded mapping data. |
|
195 | - $sourceMap['mappings'] = $mappings; |
|
196 | - |
|
197 | - if ($this->options['outputSourceFiles']) { |
|
198 | - // An optional list of source content, useful when the 'source' can't be hosted. |
|
199 | - // The contents are listed in the same order as the sources above. |
|
200 | - // 'null' may be used if some original sources should be retrieved by name. |
|
201 | - $sourceMap['sourcesContent'] = $this->getSourcesContent(); |
|
202 | - } |
|
203 | - |
|
204 | - // less.js compat fixes |
|
205 | - if (\count($sourceMap['sources']) && empty($sourceMap['sourceRoot'])) { |
|
206 | - unset($sourceMap['sourceRoot']); |
|
207 | - } |
|
208 | - |
|
209 | - return json_encode($sourceMap, JSON_UNESCAPED_SLASHES); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Returns the sources contents |
|
214 | - * |
|
215 | - * @return array|null |
|
216 | - */ |
|
217 | - protected function getSourcesContent() |
|
218 | - { |
|
219 | - if (empty($this->sources)) { |
|
220 | - return null; |
|
221 | - } |
|
222 | - |
|
223 | - $content = []; |
|
224 | - |
|
225 | - foreach ($this->sources as $sourceFile) { |
|
226 | - $content[] = file_get_contents($sourceFile); |
|
227 | - } |
|
228 | - |
|
229 | - return $content; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Generates the mappings string |
|
234 | - * |
|
235 | - * @return string |
|
236 | - */ |
|
237 | - public function generateMappings() |
|
238 | - { |
|
239 | - if (! \count($this->mappings)) { |
|
240 | - return ''; |
|
241 | - } |
|
242 | - |
|
243 | - $this->sourceKeys = array_flip(array_keys($this->sources)); |
|
244 | - |
|
245 | - // group mappings by generated line number. |
|
246 | - $groupedMap = $groupedMapEncoded = []; |
|
247 | - |
|
248 | - foreach ($this->mappings as $m) { |
|
249 | - $groupedMap[$m['generated_line']][] = $m; |
|
250 | - } |
|
251 | - |
|
252 | - ksort($groupedMap); |
|
253 | - |
|
254 | - $lastGeneratedLine = $lastOriginalIndex = $lastOriginalLine = $lastOriginalColumn = 0; |
|
255 | - |
|
256 | - foreach ($groupedMap as $lineNumber => $lineMap) { |
|
257 | - while (++$lastGeneratedLine < $lineNumber) { |
|
258 | - $groupedMapEncoded[] = ';'; |
|
259 | - } |
|
260 | - |
|
261 | - $lineMapEncoded = []; |
|
262 | - $lastGeneratedColumn = 0; |
|
263 | - |
|
264 | - foreach ($lineMap as $m) { |
|
265 | - $mapEncoded = $this->encoder->encode($m['generated_column'] - $lastGeneratedColumn); |
|
266 | - $lastGeneratedColumn = $m['generated_column']; |
|
267 | - |
|
268 | - // find the index |
|
269 | - if ($m['source_file']) { |
|
270 | - $index = $this->findFileIndex($m['source_file']); |
|
271 | - |
|
272 | - if ($index !== false) { |
|
273 | - $mapEncoded .= $this->encoder->encode($index - $lastOriginalIndex); |
|
274 | - $lastOriginalIndex = $index; |
|
275 | - // lines are stored 0-based in SourceMap spec version 3 |
|
276 | - $mapEncoded .= $this->encoder->encode($m['original_line'] - 1 - $lastOriginalLine); |
|
277 | - $lastOriginalLine = $m['original_line'] - 1; |
|
278 | - $mapEncoded .= $this->encoder->encode($m['original_column'] - $lastOriginalColumn); |
|
279 | - $lastOriginalColumn = $m['original_column']; |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - $lineMapEncoded[] = $mapEncoded; |
|
284 | - } |
|
285 | - |
|
286 | - $groupedMapEncoded[] = implode(',', $lineMapEncoded) . ';'; |
|
287 | - } |
|
288 | - |
|
289 | - return rtrim(implode($groupedMapEncoded), ';'); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Finds the index for the filename |
|
294 | - * |
|
295 | - * @param string $filename |
|
296 | - * |
|
297 | - * @return integer|false |
|
298 | - */ |
|
299 | - protected function findFileIndex($filename) |
|
300 | - { |
|
301 | - return $this->sourceKeys[$filename]; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Normalize filename |
|
306 | - * |
|
307 | - * @param string $filename |
|
308 | - * |
|
309 | - * @return string |
|
310 | - */ |
|
311 | - protected function normalizeFilename($filename) |
|
312 | - { |
|
313 | - $filename = $this->fixWindowsPath($filename); |
|
314 | - $rootpath = $this->options['sourceMapRootpath']; |
|
315 | - $basePath = $this->options['sourceMapBasepath']; |
|
316 | - |
|
317 | - // "Trim" the 'sourceMapBasepath' from the output filename. |
|
318 | - if (\strlen($basePath) && strpos($filename, $basePath) === 0) { |
|
319 | - $filename = substr($filename, \strlen($basePath)); |
|
320 | - } |
|
321 | - |
|
322 | - // Remove extra leading path separators. |
|
323 | - if (strpos($filename, '\\') === 0 || strpos($filename, '/') === 0) { |
|
324 | - $filename = substr($filename, 1); |
|
325 | - } |
|
326 | - |
|
327 | - return $rootpath . $filename; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Fix windows paths |
|
332 | - * |
|
333 | - * @param string $path |
|
334 | - * @param boolean $addEndSlash |
|
335 | - * |
|
336 | - * @return string |
|
337 | - */ |
|
338 | - public function fixWindowsPath($path, $addEndSlash = false) |
|
339 | - { |
|
340 | - $slash = ($addEndSlash) ? '/' : ''; |
|
341 | - |
|
342 | - if (! empty($path)) { |
|
343 | - $path = str_replace('\\', '/', $path); |
|
344 | - $path = rtrim($path, '/') . $slash; |
|
345 | - } |
|
346 | - |
|
347 | - return $path; |
|
348 | - } |
|
27 | + /** |
|
28 | + * What version of source map does the generator generate? |
|
29 | + */ |
|
30 | + const VERSION = 3; |
|
31 | + |
|
32 | + /** |
|
33 | + * Array of default options |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $defaultOptions = [ |
|
38 | + // an optional source root, useful for relocating source files |
|
39 | + // on a server or removing repeated values in the 'sources' entry. |
|
40 | + // This value is prepended to the individual entries in the 'source' field. |
|
41 | + 'sourceRoot' => '', |
|
42 | + |
|
43 | + // an optional name of the generated code that this source map is associated with. |
|
44 | + 'sourceMapFilename' => null, |
|
45 | + |
|
46 | + // url of the map |
|
47 | + 'sourceMapURL' => null, |
|
48 | + |
|
49 | + // absolute path to a file to write the map to |
|
50 | + 'sourceMapWriteTo' => null, |
|
51 | + |
|
52 | + // output source contents? |
|
53 | + 'outputSourceFiles' => false, |
|
54 | + |
|
55 | + // base path for filename normalization |
|
56 | + 'sourceMapRootpath' => '', |
|
57 | + |
|
58 | + // base path for filename normalization |
|
59 | + 'sourceMapBasepath' => '' |
|
60 | + ]; |
|
61 | + |
|
62 | + /** |
|
63 | + * The base64 VLQ encoder |
|
64 | + * |
|
65 | + * @var \ScssPhp\ScssPhp\SourceMap\Base64VLQ |
|
66 | + */ |
|
67 | + protected $encoder; |
|
68 | + |
|
69 | + /** |
|
70 | + * Array of mappings |
|
71 | + * |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + protected $mappings = []; |
|
75 | + |
|
76 | + /** |
|
77 | + * Array of contents map |
|
78 | + * |
|
79 | + * @var array |
|
80 | + */ |
|
81 | + protected $contentsMap = []; |
|
82 | + |
|
83 | + /** |
|
84 | + * File to content map |
|
85 | + * |
|
86 | + * @var array |
|
87 | + */ |
|
88 | + protected $sources = []; |
|
89 | + protected $sourceKeys = []; |
|
90 | + |
|
91 | + /** |
|
92 | + * @var array |
|
93 | + */ |
|
94 | + private $options; |
|
95 | + |
|
96 | + public function __construct(array $options = []) |
|
97 | + { |
|
98 | + $this->options = array_merge($this->defaultOptions, $options); |
|
99 | + $this->encoder = new Base64VLQ(); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Adds a mapping |
|
104 | + * |
|
105 | + * @param integer $generatedLine The line number in generated file |
|
106 | + * @param integer $generatedColumn The column number in generated file |
|
107 | + * @param integer $originalLine The line number in original file |
|
108 | + * @param integer $originalColumn The column number in original file |
|
109 | + * @param string $sourceFile The original source file |
|
110 | + */ |
|
111 | + public function addMapping($generatedLine, $generatedColumn, $originalLine, $originalColumn, $sourceFile) |
|
112 | + { |
|
113 | + $this->mappings[] = [ |
|
114 | + 'generated_line' => $generatedLine, |
|
115 | + 'generated_column' => $generatedColumn, |
|
116 | + 'original_line' => $originalLine, |
|
117 | + 'original_column' => $originalColumn, |
|
118 | + 'source_file' => $sourceFile |
|
119 | + ]; |
|
120 | + |
|
121 | + $this->sources[$sourceFile] = $sourceFile; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Saves the source map to a file |
|
126 | + * |
|
127 | + * @param string $content The content to write |
|
128 | + * |
|
129 | + * @return string |
|
130 | + * |
|
131 | + * @throws \ScssPhp\ScssPhp\Exception\CompilerException If the file could not be saved |
|
132 | + */ |
|
133 | + public function saveMap($content) |
|
134 | + { |
|
135 | + $file = $this->options['sourceMapWriteTo']; |
|
136 | + $dir = \dirname($file); |
|
137 | + |
|
138 | + // directory does not exist |
|
139 | + if (! is_dir($dir)) { |
|
140 | + // FIXME: create the dir automatically? |
|
141 | + throw new CompilerException( |
|
142 | + sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir) |
|
143 | + ); |
|
144 | + } |
|
145 | + |
|
146 | + // FIXME: proper saving, with dir write check! |
|
147 | + if (file_put_contents($file, $content) === false) { |
|
148 | + throw new CompilerException(sprintf('Cannot save the source map to "%s"', $file)); |
|
149 | + } |
|
150 | + |
|
151 | + return $this->options['sourceMapURL']; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Generates the JSON source map |
|
156 | + * |
|
157 | + * @return string |
|
158 | + * |
|
159 | + * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# |
|
160 | + */ |
|
161 | + public function generateJson() |
|
162 | + { |
|
163 | + $sourceMap = []; |
|
164 | + $mappings = $this->generateMappings(); |
|
165 | + |
|
166 | + // File version (always the first entry in the object) and must be a positive integer. |
|
167 | + $sourceMap['version'] = self::VERSION; |
|
168 | + |
|
169 | + // An optional name of the generated code that this source map is associated with. |
|
170 | + $file = $this->options['sourceMapFilename']; |
|
171 | + |
|
172 | + if ($file) { |
|
173 | + $sourceMap['file'] = $file; |
|
174 | + } |
|
175 | + |
|
176 | + // An optional source root, useful for relocating source files on a server or removing repeated values in the |
|
177 | + // 'sources' entry. This value is prepended to the individual entries in the 'source' field. |
|
178 | + $root = $this->options['sourceRoot']; |
|
179 | + |
|
180 | + if ($root) { |
|
181 | + $sourceMap['sourceRoot'] = $root; |
|
182 | + } |
|
183 | + |
|
184 | + // A list of original sources used by the 'mappings' entry. |
|
185 | + $sourceMap['sources'] = []; |
|
186 | + |
|
187 | + foreach ($this->sources as $sourceUri => $sourceFilename) { |
|
188 | + $sourceMap['sources'][] = $this->normalizeFilename($sourceFilename); |
|
189 | + } |
|
190 | + |
|
191 | + // A list of symbol names used by the 'mappings' entry. |
|
192 | + $sourceMap['names'] = []; |
|
193 | + |
|
194 | + // A string with the encoded mapping data. |
|
195 | + $sourceMap['mappings'] = $mappings; |
|
196 | + |
|
197 | + if ($this->options['outputSourceFiles']) { |
|
198 | + // An optional list of source content, useful when the 'source' can't be hosted. |
|
199 | + // The contents are listed in the same order as the sources above. |
|
200 | + // 'null' may be used if some original sources should be retrieved by name. |
|
201 | + $sourceMap['sourcesContent'] = $this->getSourcesContent(); |
|
202 | + } |
|
203 | + |
|
204 | + // less.js compat fixes |
|
205 | + if (\count($sourceMap['sources']) && empty($sourceMap['sourceRoot'])) { |
|
206 | + unset($sourceMap['sourceRoot']); |
|
207 | + } |
|
208 | + |
|
209 | + return json_encode($sourceMap, JSON_UNESCAPED_SLASHES); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Returns the sources contents |
|
214 | + * |
|
215 | + * @return array|null |
|
216 | + */ |
|
217 | + protected function getSourcesContent() |
|
218 | + { |
|
219 | + if (empty($this->sources)) { |
|
220 | + return null; |
|
221 | + } |
|
222 | + |
|
223 | + $content = []; |
|
224 | + |
|
225 | + foreach ($this->sources as $sourceFile) { |
|
226 | + $content[] = file_get_contents($sourceFile); |
|
227 | + } |
|
228 | + |
|
229 | + return $content; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Generates the mappings string |
|
234 | + * |
|
235 | + * @return string |
|
236 | + */ |
|
237 | + public function generateMappings() |
|
238 | + { |
|
239 | + if (! \count($this->mappings)) { |
|
240 | + return ''; |
|
241 | + } |
|
242 | + |
|
243 | + $this->sourceKeys = array_flip(array_keys($this->sources)); |
|
244 | + |
|
245 | + // group mappings by generated line number. |
|
246 | + $groupedMap = $groupedMapEncoded = []; |
|
247 | + |
|
248 | + foreach ($this->mappings as $m) { |
|
249 | + $groupedMap[$m['generated_line']][] = $m; |
|
250 | + } |
|
251 | + |
|
252 | + ksort($groupedMap); |
|
253 | + |
|
254 | + $lastGeneratedLine = $lastOriginalIndex = $lastOriginalLine = $lastOriginalColumn = 0; |
|
255 | + |
|
256 | + foreach ($groupedMap as $lineNumber => $lineMap) { |
|
257 | + while (++$lastGeneratedLine < $lineNumber) { |
|
258 | + $groupedMapEncoded[] = ';'; |
|
259 | + } |
|
260 | + |
|
261 | + $lineMapEncoded = []; |
|
262 | + $lastGeneratedColumn = 0; |
|
263 | + |
|
264 | + foreach ($lineMap as $m) { |
|
265 | + $mapEncoded = $this->encoder->encode($m['generated_column'] - $lastGeneratedColumn); |
|
266 | + $lastGeneratedColumn = $m['generated_column']; |
|
267 | + |
|
268 | + // find the index |
|
269 | + if ($m['source_file']) { |
|
270 | + $index = $this->findFileIndex($m['source_file']); |
|
271 | + |
|
272 | + if ($index !== false) { |
|
273 | + $mapEncoded .= $this->encoder->encode($index - $lastOriginalIndex); |
|
274 | + $lastOriginalIndex = $index; |
|
275 | + // lines are stored 0-based in SourceMap spec version 3 |
|
276 | + $mapEncoded .= $this->encoder->encode($m['original_line'] - 1 - $lastOriginalLine); |
|
277 | + $lastOriginalLine = $m['original_line'] - 1; |
|
278 | + $mapEncoded .= $this->encoder->encode($m['original_column'] - $lastOriginalColumn); |
|
279 | + $lastOriginalColumn = $m['original_column']; |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + $lineMapEncoded[] = $mapEncoded; |
|
284 | + } |
|
285 | + |
|
286 | + $groupedMapEncoded[] = implode(',', $lineMapEncoded) . ';'; |
|
287 | + } |
|
288 | + |
|
289 | + return rtrim(implode($groupedMapEncoded), ';'); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Finds the index for the filename |
|
294 | + * |
|
295 | + * @param string $filename |
|
296 | + * |
|
297 | + * @return integer|false |
|
298 | + */ |
|
299 | + protected function findFileIndex($filename) |
|
300 | + { |
|
301 | + return $this->sourceKeys[$filename]; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Normalize filename |
|
306 | + * |
|
307 | + * @param string $filename |
|
308 | + * |
|
309 | + * @return string |
|
310 | + */ |
|
311 | + protected function normalizeFilename($filename) |
|
312 | + { |
|
313 | + $filename = $this->fixWindowsPath($filename); |
|
314 | + $rootpath = $this->options['sourceMapRootpath']; |
|
315 | + $basePath = $this->options['sourceMapBasepath']; |
|
316 | + |
|
317 | + // "Trim" the 'sourceMapBasepath' from the output filename. |
|
318 | + if (\strlen($basePath) && strpos($filename, $basePath) === 0) { |
|
319 | + $filename = substr($filename, \strlen($basePath)); |
|
320 | + } |
|
321 | + |
|
322 | + // Remove extra leading path separators. |
|
323 | + if (strpos($filename, '\\') === 0 || strpos($filename, '/') === 0) { |
|
324 | + $filename = substr($filename, 1); |
|
325 | + } |
|
326 | + |
|
327 | + return $rootpath . $filename; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Fix windows paths |
|
332 | + * |
|
333 | + * @param string $path |
|
334 | + * @param boolean $addEndSlash |
|
335 | + * |
|
336 | + * @return string |
|
337 | + */ |
|
338 | + public function fixWindowsPath($path, $addEndSlash = false) |
|
339 | + { |
|
340 | + $slash = ($addEndSlash) ? '/' : ''; |
|
341 | + |
|
342 | + if (! empty($path)) { |
|
343 | + $path = str_replace('\\', '/', $path); |
|
344 | + $path = rtrim($path, '/') . $slash; |
|
345 | + } |
|
346 | + |
|
347 | + return $path; |
|
348 | + } |
|
349 | 349 | } |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | $dir = \dirname($file); |
137 | 137 | |
138 | 138 | // directory does not exist |
139 | - if (! is_dir($dir)) { |
|
139 | + if (!is_dir($dir)) { |
|
140 | 140 | // FIXME: create the dir automatically? |
141 | 141 | throw new CompilerException( |
142 | 142 | sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir) |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | */ |
237 | 237 | public function generateMappings() |
238 | 238 | { |
239 | - if (! \count($this->mappings)) { |
|
239 | + if (!\count($this->mappings)) { |
|
240 | 240 | return ''; |
241 | 241 | } |
242 | 242 | |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | { |
340 | 340 | $slash = ($addEndSlash) ? '/' : ''; |
341 | 341 | |
342 | - if (! empty($path)) { |
|
342 | + if (!empty($path)) { |
|
343 | 343 | $path = str_replace('\\', '/', $path); |
344 | 344 | $path = rtrim($path, '/') . $slash; |
345 | 345 | } |