|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
use ScssPhp\ScssPhp\Compiler; |
|
6
|
|
|
|
|
7
|
|
|
class ScssPhpCompiler implements SassTaskCompiler |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var Compiler |
|
12
|
|
|
*/ |
|
13
|
|
|
private $scssCompiler; |
|
14
|
|
|
|
|
15
|
4 |
|
public function __construct(string $style, string $encoding, bool $lineNumbers, string $loadPath) |
|
16
|
|
|
{ |
|
17
|
4 |
|
$this->scssCompiler = new Compiler(); |
|
18
|
4 |
|
if ($style) { |
|
19
|
4 |
|
$ucStyle = ucfirst(strtolower($style)); |
|
20
|
4 |
|
$this->scssCompiler->setFormatter('ScssPhp\\ScssPhp\\Formatter\\' . $ucStyle); |
|
21
|
|
|
} |
|
22
|
4 |
|
if ($encoding) { |
|
23
|
4 |
|
$this->scssCompiler->setEncoding($encoding); |
|
24
|
|
|
} |
|
25
|
4 |
|
if ($lineNumbers) { |
|
26
|
|
|
$this->scssCompiler->setLineNumberStyle(1); |
|
27
|
|
|
} |
|
28
|
4 |
|
if ($loadPath !== '') { |
|
29
|
|
|
$this->scssCompiler->setImportPaths(explode(PATH_SEPARATOR, $loadPath)); |
|
30
|
|
|
} |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
public function compile(string $inputFilePath, string $outputFilePath, bool $failOnError): void |
|
34
|
|
|
{ |
|
35
|
4 |
|
if (!$this->checkInputFile($inputFilePath, $failOnError)) { |
|
36
|
1 |
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
$input = file_get_contents($inputFilePath); |
|
40
|
|
|
try { |
|
41
|
2 |
|
$out = $this->scssCompiler->compile($input); |
|
42
|
2 |
|
if ($out !== '') { |
|
43
|
2 |
|
$success = file_put_contents($outputFilePath, $out); |
|
44
|
2 |
|
if (!$success && $failOnError) { |
|
|
|
|
|
|
45
|
|
|
throw new BuildException( |
|
46
|
|
|
"Cannot write to output file " . var_export($outputFilePath, true), |
|
47
|
|
|
Project::MSG_INFO |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} catch (Exception $ex) { |
|
52
|
|
|
if ($failOnError) { |
|
|
|
|
|
|
53
|
|
|
throw new BuildException($ex->getMessage()); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
private function checkInputFile($inputFilePath, $failOnError): bool |
|
59
|
|
|
{ |
|
60
|
4 |
|
if (file_exists($inputFilePath) && is_readable($inputFilePath)) { |
|
61
|
2 |
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
if ($failOnError) { |
|
65
|
1 |
|
throw new BuildException( |
|
66
|
1 |
|
"Cannot read from input file " . var_export($inputFilePath, true), |
|
67
|
1 |
|
Project::MSG_INFO |
|
|
|
|
|
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
1 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|