1
1
<?php
2
+
2
3
/**
3
4
* SCSSPHP
4
5
*
12
13
namespace ScssPhp \Server ;
13
14
14
15
use ScssPhp \ScssPhp \Compiler ;
15
- use ScssPhp \ScssPhp \Exception \ServerException ;
16
16
use ScssPhp \ScssPhp \Version ;
17
17
18
18
/**
@@ -130,7 +130,7 @@ protected function metadataName($out)
130
130
*/
131
131
protected function needsCompile ($ out , &$ etag )
132
132
{
133
- if (! is_file ($ out )) {
133
+ if (!is_file ($ out )) {
134
134
return true ;
135
135
}
136
136
@@ -210,7 +210,10 @@ protected function getIfNoneMatchHeader()
210
210
protected function compile ($ in , $ out )
211
211
{
212
212
$ start = microtime (true );
213
- $ css = $ this ->scss ->compile (file_get_contents ($ in ), $ in );
213
+ $ result = $ this ->scss ->compileString (file_get_contents ($ in ), $ in );
214
+
215
+ $ css = $ result ->getCss ();
216
+
214
217
$ elapsed = round ((microtime (true ) - $ start ), 4 );
215
218
216
219
$ v = Version::VERSION ;
@@ -223,14 +226,38 @@ protected function compile($in, $out)
223
226
$ this ->metadataName ($ out ),
224
227
serialize ([
225
228
'etag ' => $ etag ,
226
- 'imports ' => $ this ->scss -> getParsedFiles ( ),
229
+ 'imports ' => $ this ->makeParsedFilesFromIncludeFiles ( $ result -> getIncludedFiles () ),
227
230
'vars ' => crc32 (serialize ($ this ->scss ->getVariables ())),
228
231
])
229
232
);
230
233
231
234
return [$ css , $ etag ];
232
235
}
233
236
237
+
238
+ /**
239
+ * Adds to list of parsed files
240
+ *
241
+ * @internal
242
+ *
243
+ * @param array|null $paths
244
+ *
245
+ * @return array
246
+ */
247
+ protected function makeParsedFilesFromIncludeFiles ($ paths )
248
+ {
249
+ $ parsedFiles = array ();
250
+ if (!\is_null ($ paths ) && !empty ($ paths )) {
251
+ foreach ($ paths as $ path ) {
252
+ if (!\is_null ($ path ) && is_file ($ path )) {
253
+ $ parsedFiles [realpath ($ path )] = filemtime ($ path );
254
+ }
255
+ }
256
+ }
257
+ return $ parsedFiles ;
258
+ }
259
+
260
+
234
261
/**
235
262
* Format error as a pseudo-element in CSS
236
263
*
@@ -275,24 +302,26 @@ public function showErrorsAsCSS($show = true)
275
302
* @param string $in Input file (.scss)
276
303
* @param string $out Output file (.css) optional
277
304
*
278
- * @return string |bool
305
+ * @return array |bool
279
306
*
280
- * @throws \ScssPhp\ScssPhp\Exception \ServerException
307
+ * @throws \ScssPhp\Server \ServerException
281
308
*/
282
309
public function compileFile ($ in , $ out = null )
283
310
{
284
- if (! is_readable ($ in )) {
311
+ if (!is_readable ($ in )) {
285
312
throw new ServerException ('load error: failed to find ' . $ in );
286
313
}
287
314
288
315
$ pi = pathinfo ($ in );
289
316
290
317
$ this ->scss ->addImportPath ($ pi ['dirname ' ] . '/ ' );
291
318
292
- $ compiled = $ this ->scss ->compile (file_get_contents ($ in ), $ in );
319
+ $ result = $ this ->scss ->compileString (file_get_contents ($ in ), $ in );
320
+
321
+ $ compiled = $ result ->getCss ();
293
322
294
323
if (is_null ($ out )) {
295
- return $ compiled ;
324
+ return array ( ' compiled ' => $ compiled, ' files ' => $ this -> makeParsedFilesFromIncludeFiles ( $ result -> getIncludedFiles ()),) ;
296
325
}
297
326
298
327
return file_put_contents ($ out , $ compiled );
@@ -308,7 +337,7 @@ public function compileFile($in, $out = null)
308
337
*/
309
338
public function checkedCompile ($ in , $ out )
310
339
{
311
- if (! is_file ($ out ) || filemtime ($ in ) > filemtime ($ out )) {
340
+ if (!is_file ($ out ) || filemtime ($ in ) > filemtime ($ out )) {
312
341
$ this ->compileFile ($ in , $ out );
313
342
314
343
return true ;
@@ -406,11 +435,11 @@ public function serve($salt = '')
406
435
*/
407
436
public function checkedCachedCompile ($ in , $ out , $ force = false )
408
437
{
409
- if (! is_file ($ in ) || ! is_readable ($ in )) {
438
+ if (!is_file ($ in ) || !is_readable ($ in )) {
410
439
throw new ServerException ('Invalid or unreadable input file specified. ' );
411
440
}
412
441
413
- if (is_dir ($ out ) || ! is_writable (file_exists ($ out ) ? $ out : dirname ($ out ))) {
442
+ if (is_dir ($ out ) || !is_writable (file_exists ($ out ) ? $ out : dirname ($ out ))) {
414
443
throw new ServerException ('Invalid or unwritable output file specified. ' );
415
444
}
416
445
@@ -452,14 +481,14 @@ public function cachedCompile($in, $force = false)
452
481
if (is_string ($ in )) {
453
482
$ root = $ in ;
454
483
} elseif (is_array ($ in ) and isset ($ in ['root ' ])) {
455
- if ($ force or ! isset ($ in ['files ' ])) {
484
+ if ($ force or !isset ($ in ['files ' ])) {
456
485
// If we are forcing a recompile or if for some reason the
457
486
// structure does not contain any file information we should
458
487
// specify the root to trigger a rebuild.
459
488
$ root = $ in ['root ' ];
460
489
} elseif (isset ($ in ['files ' ]) and is_array ($ in ['files ' ])) {
461
490
foreach ($ in ['files ' ] as $ fname => $ ftime ) {
462
- if (! file_exists ($ fname ) or filemtime ($ fname ) > $ ftime ) {
491
+ if (!file_exists ($ fname ) or filemtime ($ fname ) > $ ftime ) {
463
492
// One of the files we knew about previously has changed
464
493
// so we should look at our incoming root again.
465
494
$ root = $ in ['root ' ];
@@ -480,10 +509,8 @@ public function cachedCompile($in, $force = false)
480
509
}
481
510
482
511
// If we have a root value which means we should rebuild.
483
- $ out = [] ;
512
+ $ out = $ this -> compileFile ( $ root ) ;
484
513
$ out ['root ' ] = $ root ;
485
- $ out ['compiled ' ] = $ this ->compileFile ($ root );
486
- $ out ['files ' ] = $ this ->scss ->getParsedFiles ();
487
514
$ out ['updated ' ] = time ();
488
515
489
516
return $ out ;
@@ -500,17 +527,17 @@ public function __construct($dir, $cacheDir = null, $scss = null)
500
527
{
501
528
$ this ->dir = $ dir ;
502
529
503
- if (! isset ($ cacheDir )) {
530
+ if (!isset ($ cacheDir )) {
504
531
$ cacheDir = $ this ->join ($ dir , 'scss_cache ' );
505
532
}
506
533
507
534
$ this ->cacheDir = $ cacheDir ;
508
535
509
- if (! is_dir ($ this ->cacheDir )) {
536
+ if (!is_dir ($ this ->cacheDir )) {
510
537
throw new ServerException ('Cache directory doesn \'t exist: ' . $ cacheDir );
511
538
}
512
539
513
- if (! isset ($ scss )) {
540
+ if (!isset ($ scss )) {
514
541
$ scss = new Compiler ();
515
542
$ scss ->setImportPaths ($ this ->dir );
516
543
}
0 commit comments