summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/lib/Minify/LessCssSource.php
blob: 389932c0b3bee6b6c913dbd1fbcb2067c0a74278 (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
<?php

class Minify_LessCssSource extends Minify_Source
{
    /**
     * @var Minify_CacheInterface
     */
    private $cache;

    /**
     * Parsed lessphp 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['lastModified'];
    }

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

        return $cache['compiled'];
    }

    /**
     * Get lessphp cache object
     *
     * @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);
            }
        }

        $less = $this->getCompiler();
        $input = $cache ? $cache : $this->filepath;
        $cache = $less->cachedCompile($input);

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

        return $this->parsed = $cache;
    }

    /**
     * Calculate maximum last modified of all files,
     * as the 'updated' timestamp in cache is not the same as file last modified timestamp:
     * @link https://github.com/leafo/lessphp/blob/v0.4.0/lessc.inc.php#L1904
     * @return int
     */
    private function getMaxLastModified($cache)
    {
        $lastModified = 0;
        foreach ($cache['files'] as $mtime) {
            $lastModified = max($lastModified, $mtime);
        }

        return $lastModified;
    }

    /**
     * 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}_less2_{$md5}";
    }

    /**
     * Get instance of less compiler
     *
     * @return lessc
     */
    private function getCompiler()
    {
        $less = new lessc();
        // do not spend CPU time letting less doing minify
        $less->setPreserveComments(true);

        return $less;
    }
}