Completed
Push — master ( 13910f...5ac23a )
by Denis
03:36
created

Leafo::_compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * JBZoo Less
4
 *
5
 * This file is part of the JBZoo CCK package.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Less
10
 * @license   MIT
11
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
12
 * @link      https://github.com/JBZoo/Less
13
 * @author    Denis Smetannikov <[email protected]>
14
 */
15
16
namespace JBZoo\Less\Driver;
17
18
use JBZoo\Less\Exception;
19
use JBZoo\Utils\FS;
20
21
/**
22
 * Class Leafo
23
 * @package JBZoo\Less
24
 */
25
class Leafo extends Driver
26
{
27
    /**
28
     * @var \lessc
29
     */
30
    protected $_compiler;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function _compile($fullPath, $relPath)
36
    {
37
        return $this->_compiler->compileFile($fullPath);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function _initCompiler()
44
    {
45
        if ($this->_compiler) {
46
            return $this->_compiler;
47
        }
48
49
        $this->_compiler = new \lessc();
50
51
        if (class_exists('\lessc_formatter_lessjs')) {
52
            $formatter = new \lessc_formatter_lessjs();
53
            // configurate css view
54
            $formatter->openSingle        = ' { ';
55
            $formatter->closeSingle       = "}\n";
56
            $formatter->close             = "}\n";
57
            $formatter->indentChar        = '    ';
58
            $formatter->disableSingle     = true;
59
            $formatter->breakSelectors    = true;
60
            $formatter->assignSeparator   = ': ';
61
            $formatter->selectorSeparator = ', ';
62
            $this->_compiler->setFormatter($formatter);
63
        }
64
65
        $this->_compiler->setPreserveComments(false);
66
67
        // Set paths
68
        $importPaths = (array)$this->_options->get('import_paths', []);
69
        foreach ($importPaths as $fullPath => $relPath) {
70
            $this->setImportPath($fullPath, $relPath);
71
        }
72
73
        // Set paths
74
        $this->_compiler->setVariables((array)$this->_options->get('global_vars', []));
75
76
        return $this->_compiler;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function setImportPath($fullPath, $relPath = null)
83
    {
84
        $this->_initCompiler();
85
86
        if (!FS::isDir($fullPath)) {
87
            throw new Exception('Undefined import path: ' . $fullPath);
88
        }
89
90
        $fullPath = FS::getRelative($fullPath, $this->_options->get('root_path'));
91
92
        $this->_compiler->addImportDir($fullPath);
93
    }
94
}
95