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
|
<?php
class Minify {
private static function _compileFiles($files, $directory, $compressor, $cacheFile) {
$lastmtime = self::_getMTime($files, $directory);
if (!file_exists($cacheFile) || filemtime($cacheFile) < $lastmtime) {
$out = '';
foreach ($files as $file) {
$fullPath = implode(
DIRECTORY_SEPARATOR,
[
BASEPATH, 'src', $directory, $file
]
);
$content = file_get_contents($fullPath) . PHP_EOL;
if (!preg_match('/\.min\./', $file)) {
$content = call_user_func($compressor, $content);
}
$out .= $content;
}
file_put_contents($cacheFile, $out);
}
return $lastmtime;
}
public static function minifyJS($scripts, $cacheFile) {
require_once(implode(
DIRECTORY_SEPARATOR,
[BASEPATH, 'lib', 'JShrink.php']
));
return self::_compileFiles(
$scripts, '_js', array('Minifier', 'minify'), $cacheFile
);
}
public static function minifyCSS($sheets, $cacheFile) {
require_once(implode(
DIRECTORY_SEPARATOR,
[BASEPATH, 'lib', 'CssMin.php']
));
return self::_compileFiles(
$sheets, '_css', array('CssMin', 'minify'), $cacheFile
);
}
private static function _getMTime($files, $directory) {
$timestamp = max(
array_map(
function($file) use($directory) {
$path = implode(
DIRECTORY_SEPARATOR,
[BASEPATH, 'src', $directory, $file]
);
$mtime = filemtime($path);
return $mtime;
},
$files
)
);
$resourceConfig = implode(
DIRECTORY_SEPARATOR,
[BASEPATH, 'config', 'resources.json']
);
if (file_exists($resourceConfig)) {
return max([filemtime($resourceConfig), $timestamp]);
}
return $timestamp;
}
public static function getMTime($scripts, $sheets) {
return max(self::_getMTime($scripts, '_js'),
self::_getMTime($sheets, '_css'));
}
}
?>
|