Completed
Pull Request — master (#86)
by Vladimir
08:24
created

Compiler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A importFile() 0 32 4
A clearImportCache() 0 9 2
1
<?php
2
3
namespace allejo\stakx\AssetEngine\Sass;
4
5
use allejo\stakx\RuntimeStatus;
6
use allejo\stakx\Service;
7
use Leafo\ScssPhp\Compiler as BaseCompiler;
8
9
/**
10
 * A modified version of our Sass compiler.
11
 */
12
class Compiler extends BaseCompiler
13
{
14
    /**
15
     * @var int
16
     */
17
    protected $lastTouched;
18
19
    /**
20
     * Overridden method for handling Sass import statements.
21
     *
22
     * Our compiler will need to be able to refresh the Sass output if an import
23
     * has changed since it was cached. This is used solely for the purpose of
24
     * supporting our built-in dev server so that it can recompile Sass with the
25
     * latest changes made in imports.
26
     *
27
     * @param string $path
28
     * @param array  $out
29
     */
30
    protected function importFile($path, $out)
31
    {
32
        if (!Service::hasRunTimeFlag(RuntimeStatus::IN_SERVE_MODE))
33
        {
34
            parent::importFile($path, $out);
35
36
            return;
37
        }
38
39
        $realPath = realpath($path);
40
41
        // If we've compiled our Sass once already, then the last modified time
42
        // will exist in our listing. At this point, we already have Sass trees
43
        // cached, so we should only clear the outdated ones.
44
        if (isset($this->lastTouched[$realPath]))
45
        {
46
            // If the imported file has been modified since our last
47
            // compilation, then we will need to clear the cached tree to allow
48
            // our engine to build a new tree.
49
            if (($t = filemtime($realPath)) > $this->lastTouched[$realPath])
50
            {
51
                $this->clearImportCache($realPath);
52
                $this->lastTouched[$realPath] = $t;
53
            }
54
        }
55
        else
56
        {
57
            $this->lastTouched[$realPath] = filemtime($realPath);
58
        }
59
60
        parent::importFile($path, $out);
61
    }
62
63
    /**
64
     * Remove an import from the Sass cache.
65
     *
66
     * @param string|null $file The absolute path to an imported file that will
67
     *                          be cleared from the cache. When given null, the
68
     *                          entire cache will be cleared.
69
     */
70
    public function clearImportCache($file = null)
71
    {
72
        if ($file === null) {
73
            $this->importCache = [];
74
            return;
75
        }
76
77
        unset($this->importCache[$file]);
78
    }
79
}
80