| 1 |
<?php |
| 2 |
use Leafo\ScssPhp\Compiler; |
| 3 |
use Leafo\ScssPhp\Server; |
| 4 |
use Leafo\ScssPhp\Version; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class for using SCSS files |
| 8 |
* |
| 9 |
* @link https://github.com/leafo/scssphp/ |
| 10 |
*/ |
| 11 |
class Minify_ScssCssSource extends Minify_Source |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var Minify_CacheInterface |
| 15 |
*/ |
| 16 |
private $cache; |
| 17 |
|
| 18 |
/** |
| 19 |
* Parsed SCSS cache object |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private $parsed; |
| 24 |
|
| 25 |
/** |
| 26 |
* @inheritdoc |
| 27 |
*/ |
| 28 |
public function __construct(array $spec, Minify_CacheInterface $cache) |
| 29 |
{ |
| 30 |
parent::__construct($spec); |
| 31 |
|
| 32 |
$this->cache = $cache; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get last modified of all parsed files |
| 37 |
* |
| 38 |
* @return int |
| 39 |
*/ |
| 40 |
public function getLastModified() |
| 41 |
{ |
| 42 |
$cache = $this->getCache(); |
| 43 |
|
| 44 |
return $cache['updated']; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get content |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function getContent() |
| 53 |
{ |
| 54 |
$cache = $this->getCache(); |
| 55 |
|
| 56 |
return $cache['content']; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Make a unique cache id for for this source. |
| 61 |
* |
| 62 |
* @param string $prefix |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
private function getCacheId($prefix = 'minify') |
| 67 |
{ |
| 68 |
$md5 = md5($this->filepath); |
| 69 |
|
| 70 |
return "{$prefix}_scss_{$md5}"; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get SCSS cache object |
| 75 |
* |
| 76 |
* Runs the compilation if needed |
| 77 |
* |
| 78 |
* Implements Leafo\ScssPhp\Server logic because we need to get parsed files without parsing actual content |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
private function getCache() |
| 83 |
{ |
| 84 |
// cache for single run |
| 85 |
// so that getLastModified and getContent in single request do not add additional cache roundtrips (i.e memcache) |
| 86 |
if (isset($this->parsed)) { |
| 87 |
return $this->parsed; |
| 88 |
} |
| 89 |
|
| 90 |
// check from cache first |
| 91 |
$cache = null; |
| 92 |
$cacheId = $this->getCacheId(); |
| 93 |
if ($this->cache->isValid($cacheId, 0)) { |
| 94 |
if ($cache = $this->cache->fetch($cacheId)) { |
| 95 |
$cache = unserialize($cache); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$input = $cache ? $cache : $this->filepath; |
| 100 |
|
| 101 |
if ($this->cacheIsStale($cache)) { |
| 102 |
$cache = $this->compile($this->filepath); |
| 103 |
} |
| 104 |
|
| 105 |
if (!is_array($input) || $cache['updated'] > $input['updated']) { |
| 106 |
$this->cache->store($cacheId, serialize($cache)); |
| 107 |
} |
| 108 |
|
| 109 |
return $this->parsed = $cache; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Determine whether .scss file needs to be re-compiled. |
| 114 |
* |
| 115 |
* @param array $cache Cache object |
| 116 |
* |
| 117 |
* @return boolean True if compile required. |
| 118 |
*/ |
| 119 |
private function cacheIsStale($cache) |
| 120 |
{ |
| 121 |
if (!$cache) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
$updated = $cache['updated']; |
| 126 |
foreach ($cache['files'] as $import => $mtime) { |
| 127 |
$filemtime = filemtime($import); |
| 128 |
|
| 129 |
if ($filemtime !== $mtime || $filemtime > $updated) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Compile .scss file |
| 139 |
* |
| 140 |
* @param string $filename Input path (.scss) |
| 141 |
* |
| 142 |
* @see Server::compile() |
| 143 |
* @return array meta data result of the compile |
| 144 |
*/ |
| 145 |
private function compile($filename) |
| 146 |
{ |
| 147 |
$start = microtime(true); |
| 148 |
$scss = new Compiler(); |
| 149 |
|
| 150 |
// set import path directory the input filename resides |
| 151 |
// otherwise @import statements will not find the files |
| 152 |
// and will treat the @import line as css import |
| 153 |
$scss->setImportPaths(dirname($filename)); |
| 154 |
|
| 155 |
$css = $scss->compile(file_get_contents($filename), $filename); |
| 156 |
$elapsed = round((microtime(true) - $start), 4); |
| 157 |
|
| 158 |
$v = Version::VERSION; |
| 159 |
$ts = date('r', $start); |
| 160 |
$css = "/* compiled by scssphp $v on $ts (${elapsed}s) */\n\n" . $css; |
| 161 |
|
| 162 |
$imports = $scss->getParsedFiles(); |
| 163 |
|
| 164 |
$updated = 0; |
| 165 |
foreach ($imports as $mtime) { |
| 166 |
$updated = max($updated, $mtime); |
| 167 |
} |
| 168 |
|
| 169 |
return array( |
| 170 |
'elapsed' => $elapsed, // statistic, can be dropped |
| 171 |
'updated' => $updated, |
| 172 |
'content' => $css, |
| 173 |
'files' => $imports, |
| 174 |
); |
| 175 |
} |
| 176 |
} |
| 177 |
|