1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
declare(strict_types=1);
21

22
class SassTaskCompilerFactory
23
{
24

25
    /**
26
     * @var FileSystem
27
     */
28
    private $fs;
29

30 1
    public function __construct(FileSystem $fs)
31
    {
32 1
        $this->fs = $fs;
33
    }
34

35 1
    public function prepareCompiler(SassTask $sassTask): SassTaskCompiler
36
    {
37 1
        $this->assertCompilerIsSet($sassTask);
38

39
        // If both are set to be used, prefer sass over scssphp.
40 1
        if ($sassTask->getUseSass() && $sassTask->getUseScssPhp()) {
41 1
            if ($this->fs->which($sassTask->getExecutable()) === false) {
42 0
                $this->assertScssPhpIsAvailable();
43 0
                return new ScssPhpCompiler(
44 0
                    $sassTask->getStyle(),
45 0
                    $sassTask->getEncoding(),
46 0
                    $sassTask->getLineNumbers(),
47 0
                    $sassTask->getPath()
48
                );
49
            }
50 1
        } elseif ($sassTask->getUseSass()) {
51 1
            $this->assertSassIsAvailable($sassTask);
52 1
        } elseif ($sassTask->getUseScssPhp()) {
53 1
            $this->assertScssPhpIsAvailable();
54 1
            return new ScssPhpCompiler(
55 1
                $sassTask->getStyle(),
56 1
                $sassTask->getEncoding(),
57 1
                $sassTask->getLineNumbers(),
58 1
                $sassTask->getPath()
59
            );
60
        }
61

62 1
        return new SassCompiler($sassTask->getExecutable(), $sassTask->getFlags());
63
    }
64

65 1
    private function assertCompilerIsSet(SassTask $sassTask): void
66
    {
67 1
        if (!$sassTask->getUseSass() && !$sassTask->getUseScssPhp()) {
68 1
            throw new BuildException("Neither sass nor scssphp are to be used.");
69
        }
70
    }
71

72 1
    private function assertScssPhpIsAvailable(): void
73
    {
74 1
        if (!$this->isScssPhpLoaded()) {
75 0
            $msg = sprintf(
76 0
                "Install scssphp/scssphp."
77
            );
78 0
            throw new BuildException($msg);
79
        }
80
    }
81

82 1
    private function assertSassIsAvailable(SassTask $sassTask): void
83
    {
84 1
        if ($this->fs->which($sassTask->getExecutable()) === false) {
85 1
            $msg = sprintf(
86 1
                "%s not found. Install sass.",
87 1
                $sassTask->getExecutable()
88
            );
89 1
            throw new BuildException($msg);
90
        }
91
    }
92

93 1
    private function isScssPhpLoaded(): bool
94
    {
95 1
        return class_exists('\ScssPhp\ScssPhp\Compiler');
96
    }
97
}

Read our documentation on viewing source code .

Loading