Completed
Pull Request — master (#86)
by Vladimir
02:09
created

Compiler::clearImportCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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