Completed
Pull Request — master (#69)
by Vladimir
05:36
created

SassEngine::configureSourceMap()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 4
nc 3
nop 0
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 AssetEngine
19
{
20
    private $compiler;
21
    private $options;
22
23
    public function __construct(array $options = [])
24
    {
25
        $this->compiler = new Compiler();
26
        $this->options = $options;
27
28
        $this->configureImportPath();
29
        $this->configureOutputStyle();
30
        $this->configureSourceMap();
31
    }
32
33
    public function getConfigurationNamespace()
34
    {
35
        return 'scss';
36
    }
37
38
    public function getDefaultConfiguration()
39
    {
40
        return [
41
            'style' => 'compressed',
42
            'sourcemap' => false,
43
        ];
44
    }
45
46
    public function getFolder()
47
    {
48
        return '_sass';
49
    }
50
51
    public function getExtensions()
52
    {
53
        return ['scss'];
54
    }
55
56
    public function parse($content)
57
    {
58
        return $this->compiler->compile($content);
59
    }
60
61
    private function configureImportPath()
62
    {
63
        $this->compiler->setImportPaths(Service::getWorkingDirectory() . '/_sass/');
64
    }
65
66
    private function configureOutputStyle()
67
    {
68
        $style = __::get($this->options, 'style', 'compressed');
69
70
        switch ($style)
71
        {
72
            case 'nested':
73
                $this->compiler->setFormatter(Nested::class);
74
                break;
75
76
            case 'expanded':
77
                $this->compiler->setFormatter(Expanded::class);
78
                break;
79
80
            case 'compact':
81
                $this->compiler->setFormatter(Compact::class);
82
                break;
83
84
            case 'compressed':
85
            default:
86
                $this->compiler->setFormatter(Crunched::class);
87
        }
88
    }
89
90
    private function configureSourceMap()
91
    {
92
        $sourcemap = __::get($this->options, 'sourcemap');
93
94
        if ($sourcemap === false || $sourcemap === null) {
95
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_NONE);
96
        }
97
        elseif ($sourcemap === 'inline') {
98
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_INLINE);
99
        }
100
        else {
101
            $this->compiler->setSourceMap(Compiler::SOURCE_MAP_FILE);
102
            $this->compiler->setSourceMapOptions([
103
                'sourceMapWriteTo' => $sourcemap
104
            ]);
105
        }
106
    }
107
}
108