-
Notifications
You must be signed in to change notification settings - Fork 0
/
minifier.php
105 lines (69 loc) · 1.97 KB
/
minifier.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
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
<?php
/*
@package: Magma PHP Minifier for JS and CSS
@author: Sören Meier <[email protected]>
@version: 0.1 <2019-07-10>
@docs: minifier.magma-lang.com/php/docs/
*/
namespace MagmaMinifier;
class Minifier {
protected $debug = false;
protected $tmpPath = '';
// METHODS
public function js( array $paths, string $v = '' ) {
$k = md5( implode( $paths ). $v );
$filename = $k. '.js';
$outFile = $this->tmpPath. $filename;
if ( is_file( $outFile ) && !$this->debug )
return $filename;
$str = '';
foreach ( $paths as $p )
$str .= file_get_contents( $p );
$str = $this->minifyJs( $str );
file_put_contents( $outFile, $str );
return $filename;
}
public function minifyJs( string $str ) {
$minifier = new JsMinifier;
return $minifier->go( $str );
}
public function css( array $paths, string $v = '' ) {
$k = md5( implode( $paths ). $v );
$filename = $k. '.css';
$outFile = $this->tmpPath. $filename;
if ( is_file( $outFile ) && !$this->debug )
return $filename;
$str = '';
foreach ( $paths as $p )
$str .= file_get_contents( $p );
$str = $this->minifyCss( $str );
file_put_contents( $outFile, $str );
return $filename;
}
public function minifyCss( string $str ) {
$minifier = new CssMinifier;
return $minifier->go( $str );
}
public function cleanTmp() {
self::deleteDir( $this->tmpPath );
}
// INIT
public function __construct( string $tmpPath, bool $debug = false ) {
$this->tmpPath = $tmpPath;
$this->debug = $debug;
if ( !is_dir( $this->tmpPath ) )
mkdir( $this->tmpPath );
}
// PROTECTED
// $v to change the filename for new versions
// returns the relative path
// to the tmp folder
protected static function deleteDir( string $dir ) {
foreach ( glob( $dir. '*', GLOB_MARK ) as $path )
if ( is_file( $path ) )
unlink( $path );
else
self::deleteDir( $path );
rmdir( $dir );
}
}