1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Phing\Task\Ext\Sass; |
24
|
|
|
|
25
|
|
|
use Exception; |
26
|
|
|
use Phing\Exception\BuildException; |
27
|
|
|
use Phing\Project; |
28
|
|
|
use ScssPhp\ScssPhp\Compiler; |
29
|
|
|
use ScssPhp\ScssPhp\OutputStyle; |
30
|
|
|
|
31
|
|
|
class ScssPhpCompiler implements SassTaskCompiler |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Compiler |
35
|
|
|
*/ |
36
|
|
|
private $scssCompiler; |
37
|
|
|
|
38
|
4 |
|
public function __construct(string $style, string $loadPath) |
39
|
|
|
{ |
40
|
4 |
|
$this->scssCompiler = new Compiler(); |
41
|
4 |
|
if ($style) { |
42
|
4 |
|
$this->scssCompiler->setOutputStyle(OutputStyle::fromString(strtolower($style))); |
43
|
|
|
} |
44
|
4 |
|
if ($loadPath !== '') { |
45
|
|
|
$this->scssCompiler->setImportPaths(explode(PATH_SEPARATOR, $loadPath)); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
public function compile(string $inputFilePath, string $outputFilePath, bool $failOnError): void |
50
|
|
|
{ |
51
|
4 |
|
if (!$this->checkInputFile($inputFilePath, $failOnError)) { |
52
|
1 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
try { |
56
|
2 |
|
$out = $this->scssCompiler->compileFile($inputFilePath); |
57
|
2 |
|
if ($out !== '') { |
|
|
|
|
58
|
2 |
|
$success = file_put_contents($outputFilePath, $out->getCss()); |
59
|
2 |
|
if (!$success && $failOnError) { |
|
|
|
|
60
|
2 |
|
throw new BuildException( |
61
|
2 |
|
"Cannot write to output file " . var_export($outputFilePath, true), |
62
|
2 |
|
Project::MSG_INFO |
63
|
2 |
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} catch (Exception $ex) { |
67
|
|
|
if ($failOnError) { |
|
|
|
|
68
|
|
|
throw new BuildException($ex); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
private function checkInputFile($inputFilePath, $failOnError): bool |
74
|
|
|
{ |
75
|
4 |
|
if (file_exists($inputFilePath) && is_readable($inputFilePath)) { |
76
|
2 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
if ($failOnError) { |
80
|
1 |
|
throw new BuildException( |
81
|
1 |
|
"Cannot read from input file " . var_export($inputFilePath, true), |
82
|
1 |
|
Project::MSG_INFO |
|
|
|
|
83
|
1 |
|
); |
84
|
|
|
} |
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|