1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\AssetEngine; |
9
|
|
|
|
10
|
|
|
use __; |
11
|
|
|
use allejo\stakx\Service; |
12
|
|
|
use Leafo\ScssPhp\Compiler; |
13
|
|
|
use Leafo\ScssPhp\Formatter\Compact; |
14
|
|
|
use Leafo\ScssPhp\Formatter\Crunched; |
15
|
|
|
use Leafo\ScssPhp\Formatter\Expanded; |
16
|
|
|
use Leafo\ScssPhp\Formatter\Nested; |
17
|
|
|
|
18
|
|
|
class SassEngine implements AssetEngineInterface |
19
|
|
|
{ |
20
|
|
|
private $compiler; |
21
|
|
|
private $options = []; |
22
|
|
|
|
23
|
4 |
|
public function __construct() |
24
|
|
|
{ |
25
|
4 |
|
$this->compiler = new Compiler(); |
26
|
4 |
|
} |
27
|
|
|
|
28
|
4 |
|
public function getConfigurationNamespace() |
29
|
|
|
{ |
30
|
4 |
|
return 'scss'; |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
public function getDefaultConfiguration() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
4 |
|
'style' => 'compressed', |
37
|
|
|
'sourcemap' => false, |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
public function getFolder() |
42
|
|
|
{ |
43
|
4 |
|
return '_sass'; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
public function getExtensions() |
47
|
|
|
{ |
48
|
4 |
|
return ['scss']; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
public function parse($content) |
52
|
|
|
{ |
53
|
4 |
|
return $this->compiler->compile($content); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
public function setOptions(array $options) |
57
|
|
|
{ |
58
|
4 |
|
$this->options = $options; |
59
|
|
|
|
60
|
4 |
|
$this->configureImportPath(); |
61
|
4 |
|
$this->configureOutputStyle(); |
62
|
4 |
|
$this->configureSourceMap(); |
63
|
4 |
|
} |
64
|
|
|
|
65
|
4 |
|
private function configureImportPath() |
66
|
|
|
{ |
67
|
4 |
|
$this->compiler->setImportPaths(Service::getWorkingDirectory() . '/_sass/'); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
4 |
|
private function configureOutputStyle() |
71
|
|
|
{ |
72
|
4 |
|
$style = __::get($this->options, 'style', 'compressed'); |
73
|
|
|
|
74
|
4 |
|
$this->compiler->setFormatter(self::stringToFormatter($style)); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
4 |
|
private function configureSourceMap() |
78
|
|
|
{ |
79
|
4 |
|
$sourcemap = __::get($this->options, 'sourcemap'); |
80
|
|
|
|
81
|
4 |
|
if ($sourcemap === false || $sourcemap === null) { |
82
|
4 |
|
$this->compiler->setSourceMap(Compiler::SOURCE_MAP_NONE); |
83
|
|
|
} |
84
|
|
|
elseif ($sourcemap === 'inline') { |
85
|
|
|
$this->compiler->setSourceMap(Compiler::SOURCE_MAP_INLINE); |
86
|
|
|
} |
87
|
|
|
else { |
88
|
|
|
$this->compiler->setSourceMap(Compiler::SOURCE_MAP_FILE); |
89
|
|
|
$this->compiler->setSourceMapOptions([ |
90
|
|
|
'sourceMapRootpath' => Service::getWorkingDirectory(), |
91
|
|
|
]); |
92
|
|
|
} |
93
|
4 |
|
} |
94
|
|
|
|
95
|
4 |
|
public static function stringToFormatter($format) |
|
|
|
|
96
|
|
|
{ |
97
|
4 |
|
if ($format == 'nested') |
98
|
|
|
{ |
99
|
1 |
|
return Nested::class; |
100
|
|
|
} |
101
|
3 |
|
elseif ($format == 'expanded') |
102
|
|
|
{ |
103
|
1 |
|
return Expanded::class; |
104
|
|
|
} |
105
|
2 |
|
elseif ($format == 'compact') |
106
|
|
|
{ |
107
|
1 |
|
return Compact::class; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return Crunched::class; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.