summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/lib/Minify/ScssCssSource.php
blob: ebccbe8ead4d14a86bc6f8cf43a21042a644b4a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Server;
use Leafo\ScssPhp\Version;

/**
 * Class for using SCSS files
 *
 * @link https://github.com/leafo/scssphp/
 */
class Minify_ScssCssSource extends Minify_Source
{
    /**
     * @var Minify_CacheInterface
     */
    private $cache;

    /**
     * Parsed SCSS cache object
     *
     * @var array
     */
    private $parsed;

    /**
     * @inheritdoc
     */
    public function __construct(array $spec, Minify_CacheInterface $cache)
    {
        parent::__construct($spec);

        $this->cache = $cache;
    }

    /**
     * Get last modified of all parsed files
     *
     * @return int
     */
    public function getLastModified()
    {
        $cache = $this->getCache();

        return $cache['updated'];
    }

    /**
     * Get content
     *
     * @return string
     */
    public function getContent()
    {
        $cache = $this->getCache();

        return $cache['content'];
    }

    /**
     * Make a unique cache id for for this source.
     *
     * @param string $prefix
     *
     * @return string
     */
    private function getCacheId($prefix = 'minify')
    {
        $md5 = md5($this->filepath);

        return "{$prefix}_scss_{$md5}";
    }

    /**
     * Get SCSS cache object
     *
     * Runs the compilation if needed
     *
     * Implements Leafo\ScssPhp\Server logic because we need to get parsed files without parsing actual content
     *
     * @return array
     */
    private function getCache()
    {
        // cache for single run
        // so that getLastModified and getContent in single request do not add additional cache roundtrips (i.e memcache)
        if (isset($this->parsed)) {
            return $this->parsed;
        }

        // check from cache first
        $cache = null;
        $cacheId = $this->getCacheId();
        if ($this->cache->isValid($cacheId, 0)) {
            if ($cache = $this->cache->fetch($cacheId)) {
                $cache = unserialize($cache);
            }
        }

        $input = $cache ? $cache : $this->filepath;

        if ($this->cacheIsStale($cache)) {
            $cache = $this->compile($this->filepath);
        }

        if (!is_array($input) || $cache['updated'] > $input['updated']) {
            $this->cache->store($cacheId, serialize($cache));
        }

        return $this->parsed = $cache;
    }

    /**
     * Determine whether .scss file needs to be re-compiled.
     *
     * @param array $cache Cache object
     *
     * @return boolean True if compile required.
     */
    private function cacheIsStale($cache)
    {
        if (!$cache) {
            return true;
        }

        $updated = $cache['updated'];
        foreach ($cache['files'] as $import => $mtime) {
            $filemtime = filemtime($import);

            if ($filemtime !== $mtime || $filemtime > $updated) {
                return true;
            }
        }

        return false;
    }

    /**
     * Compile .scss file
     *
     * @param string $filename Input path (.scss)
     *
     * @see Server::compile()
     * @return array meta data result of the compile
     */
    private function compile($filename)
    {
        $start = microtime(true);
        $scss = new Compiler();

        // set import path directory the input filename resides
        // otherwise @import statements will not find the files
        // and will treat the @import line as css import
        $scss->setImportPaths(dirname($filename));

        $css = $scss->compile(file_get_contents($filename), $filename);
        $elapsed = round((microtime(true) - $start), 4);

        $v = Version::VERSION;
        $ts = date('r', $start);
        $css = "/* compiled by scssphp $v on $ts (${elapsed}s) */\n\n" . $css;

        $imports = $scss->getParsedFiles();

        $updated = 0;
        foreach ($imports as $mtime) {
            $updated = max($updated, $mtime);
        }

        return array(
            'elapsed' => $elapsed, // statistic, can be dropped
            'updated' => $updated,
            'content' => $css,
            'files' => $imports,
        );
    }
}