-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinify.php
More file actions
102 lines (86 loc) · 2.71 KB
/
Minify.php
File metadata and controls
102 lines (86 loc) · 2.71 KB
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
<?php
class Minify {
private $validExtensions = ['css', 'js'];
private $path = '';
private $extension = '';
private $content = '';
private $contentMinify = '';
/**
* Minifier un fichier js ou css
* @param [string] $path : chemin du fichier
* @return boolean
*/
public function minify_file($path) {
$this->path = $path;
if ( ! $this->_isValidFile() ) {
return false;
}
$this->_loadContent();
$this->_minify();
return $this->contentMinify;
}
/**
* Indique si le fichier envoyé est valide
* @return boolean
*/
private function _isValidFile() {
$this->_loadExtension();
return in_array($this->extension, $this->validExtensions);
}
/**
* Récupère l'extension du fichier et la stocke en paramètre de la classe
* @return void
*/
private function _loadExtension() {
$SplFileInfo = new SplFileInfo($this->path);
$this->extension = $SplFileInfo->getExtension();
}
/**
* Récupère le contenu et le stocke en paramètre de la classe
* @return void
*/
private function _loadContent() {
$file = fopen($this->path, 'r');
$this->content = $this->contentMinify = fread($file, filesize($this->path));
}
/**
* Lance les opérations de minification des fichiers
* @return void
*/
private function _minify() {
$this->_remove_comments();
$this->_remove_tabsAndLinefeed();
$this->_remove_spaces();
}
/**
* Supprime les commentaires
* @return void
*/
private function _remove_comments() {
$this->contentMinify = preg_replace('/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/', '', $this->contentMinify);
}
/**
* Supprime les tabulations, retour à la ligne, retour chariot
* @return void
*/
private function _remove_tabsAndLinefeed() {
$this->contentMinify = str_replace(["\t", "\n", "\r"], '', $this->contentMinify);
}
/**
* Supprime les espaces inutile
* @return void
*/
private function _remove_spaces() {
$this->contentMinify = str_replace([' {', '{ '], '{', $this->contentMinify);
$this->contentMinify = str_replace([' }', '} '], '}', $this->contentMinify);
$this->contentMinify = str_replace([' ;', '; '], ';', $this->contentMinify);
$this->contentMinify = str_replace([' (', '( '], '(', $this->contentMinify);
$this->contentMinify = str_replace([' )', ') '], ')', $this->contentMinify);
$this->contentMinify = str_replace([' ,', ', '], ',', $this->contentMinify);
$this->contentMinify = str_replace([' :', ': '], ':', $this->contentMinify);
$this->contentMinify = str_replace([' =', '= '], '=', $this->contentMinify);
$this->contentMinify = str_replace([' ==', '== '], '==', $this->contentMinify);
$this->contentMinify = str_replace([' ===', '=== '], '===', $this->contentMinify);
}
}
?>