PluginProbe
WP Super Minify • Minify, Compress and Cache HTML, CSS & JavaScript / trunk
WP Super Minify • Minify, Compress and Cache HTML, CSS & JavaScript vtrunk
trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.4 1.5 1.5.1 1.6 2.0 2.0.1
wp-super-minify / includes / min / lib / Minify / LessCssSource.php

LessCssSource.php in WP Super Minify • Minify, Compress and Cache HTML, CSS & JavaScript trunk, at includes/min/lib/Minify/LessCssSource.php

129 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Minify_LessCssSource extends Minify_Source
4 {
5 /**
6 * @var Minify_CacheInterface
7 */
8 private $cache;
9
10 /**
11 * Parsed lessphp cache object
12 *
13 * @var array
14 */
15 private $parsed;
16
17 /**
18 * @inheritdoc
19 */
20 public function __construct(array $spec, Minify_CacheInterface $cache)
21 {
22 parent::__construct($spec);
23
24 $this->cache = $cache;
25 }
26
27 /**
28 * Get last modified of all parsed files
29 *
30 * @return int
31 */
32 public function getLastModified()
33 {
34 $cache = $this->getCache();
35
36 return $cache['lastModified'];
37 }
38
39 /**
40 * Get content
41 *
42 * @return string
43 */
44 public function getContent()
45 {
46 $cache = $this->getCache();
47
48 return $cache['compiled'];
49 }
50
51 /**
52 * Get lessphp cache object
53 *
54 * @return array
55 */
56 private function getCache()
57 {
58 // cache for single run
59 // so that getLastModified and getContent in single request do not add additional cache roundtrips (i.e memcache)
60 if (isset($this->parsed)) {
61 return $this->parsed;
62 }
63
64 // check from cache first
65 $cache = null;
66 $cacheId = $this->getCacheId();
67 if ($this->cache->isValid($cacheId, 0)) {
68 if ($cache = $this->cache->fetch($cacheId)) {
69 $cache = unserialize($cache);
70 }
71 }
72
73 $less = $this->getCompiler();
74 $input = $cache ? $cache : $this->filepath;
75 $cache = $less->cachedCompile($input);
76
77 if (!is_array($input) || $cache['updated'] > $input['updated']) {
78 $cache['lastModified'] = $this->getMaxLastModified($cache);
79 $this->cache->store($cacheId, serialize($cache));
80 }
81
82 return $this->parsed = $cache;
83 }
84
85 /**
86 * Calculate maximum last modified of all files,
87 * as the 'updated' timestamp in cache is not the same as file last modified timestamp:
88 * @link https://github.com/leafo/lessphp/blob/v0.4.0/lessc.inc.php#L1904
89 * @return int
90 */
91 private function getMaxLastModified($cache)
92 {
93 $lastModified = 0;
94 foreach ($cache['files'] as $mtime) {
95 $lastModified = max($lastModified, $mtime);
96 }
97
98 return $lastModified;
99 }
100
101 /**
102 * Make a unique cache id for for this source.
103 *
104 * @param string $prefix
105 *
106 * @return string
107 */
108 private function getCacheId($prefix = 'minify')
109 {
110 $md5 = md5($this->filepath);
111
112 return "{$prefix}_less2_{$md5}";
113 }
114
115 /**
116 * Get instance of less compiler
117 *
118 * @return lessc
119 */
120 private function getCompiler()
121 {
122 $less = new lessc();
123 // do not spend CPU time letting less doing minify
124 $less->setPreserveComments(true);
125
126 return $less;
127 }
128 }
129