-
Notifications
You must be signed in to change notification settings - Fork 10
/
cached_compilation.php
55 lines (44 loc) · 1.57 KB
/
cached_compilation.php
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
<?php
use ILess\Parser;
use ILess\Cache\FileSystemCache;
require_once '_bootstrap.php';
// create the parser
$parser = new Parser([], new FileSystemCache([
'cache_dir' => dirname(__FILE__) . '/cache',
]));
$file = dirname(__FILE__) . '/less/test.less';
// create your cache key
$cacheKey = md5($file);
$importer = $parser->getImporter();
$cache = $parser->getCache();
$rebuild = true;
$cssLastModified = -1;
if ($cache->has($cacheKey)) {
$rebuild = false;
list($css, $importedFiles) = $cache->get($cacheKey);
// we need to check if the file has been modified
foreach ($importedFiles as $importedFileArray) {
list($lastModifiedBefore, $path, $currentFileInfo) = $importedFileArray;
$lastModified = $importer->getLastModified($path, $currentFileInfo);
$cssLastModified = max($lastModified, $cssLastModified);
if ($lastModifiedBefore != $lastModified) {
$rebuild = true;
// no need to continue, we will rebuild the CSS
break;
}
}
}
if ($rebuild) {
$parser->parseFile($file);
$css = $parser->getCSS();
// what have been imported?
$importedFiles = [];
foreach ($importer->getImportedFiles() as $importedFile) {
$importedFiles[] = [$importedFile[0]->getLastModified(), $importedFile[1], $importedFile[2]];
$cssLastModified = max($cssLastModified, $importedFile[0]->getLastModified());
}
$cache->set($cacheKey, [$css, $importedFiles]);
}
header('Content-Type: text/css');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s ', $cssLastModified) . 'GMT');
echo $css;