|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace allejo\stakx\AssetEngine\Sass; |
|
4
|
|
|
|
|
5
|
|
|
use allejo\stakx\RuntimeStatus; |
|
6
|
|
|
use allejo\stakx\Service; |
|
7
|
|
|
use Leafo\ScssPhp\Compiler as BaseCompiler; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A modified version of our Sass compiler. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Compiler extends BaseCompiler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $lastTouched; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Overridden method for handling Sass import statements. |
|
21
|
|
|
* |
|
22
|
|
|
* Our compiler will need to be able to refresh the Sass output if an import |
|
23
|
|
|
* has changed since it was cached. This is used solely for the purpose of |
|
24
|
|
|
* supporting our built-in dev server so that it can recompile Sass with the |
|
25
|
|
|
* latest changes made in imports. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $path |
|
28
|
|
|
* @param array $out |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function importFile($path, $out) |
|
31
|
|
|
{ |
|
32
|
|
|
if (!Service::hasRunTimeFlag(RuntimeStatus::IN_SERVE_MODE)) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::importFile($path, $out); |
|
35
|
|
|
|
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$realPath = realpath($path); |
|
40
|
|
|
|
|
41
|
|
|
// If we've compiled our Sass once already, then the last modified time |
|
42
|
|
|
// will exist in our listing. At this point, we already have Sass trees |
|
43
|
|
|
// cached, so we should only clear the outdated ones. |
|
44
|
|
|
if (isset($this->lastTouched[$realPath])) |
|
45
|
|
|
{ |
|
46
|
|
|
// If the imported file has been modified since our last |
|
47
|
|
|
// compilation, then we will need to clear the cached tree to allow |
|
48
|
|
|
// our engine to build a new tree. |
|
49
|
|
|
if (($t = filemtime($realPath)) > $this->lastTouched[$realPath]) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->clearImportCache($realPath); |
|
52
|
|
|
$this->lastTouched[$realPath] = $t; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
else |
|
56
|
|
|
{ |
|
57
|
|
|
$this->lastTouched[$realPath] = filemtime($realPath); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
parent::importFile($path, $out); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Remove an import from the Sass cache. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string|null $file The absolute path to an imported file that will |
|
67
|
|
|
* be cleared from the cache. When given null, the |
|
68
|
|
|
* entire cache will be cleared. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function clearImportCache($file = null) |
|
71
|
|
|
{ |
|
72
|
|
|
if ($file === null) { |
|
73
|
|
|
$this->importCache = []; |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
unset($this->importCache[$file]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|