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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2017 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/
require '/usr/share/php/natxet/CssMin/autoload.php';
require '/usr/share/php/Patchwork/autoload-jsqueeze.php';
class Task
{
private function taskMinifyCSS($srce, $dest) {
$l = strlen(dirname(__DIR__))+1;
$in = file_get_contents($srce);
$out = \CssMin::minify($in);
if ($out && strlen($out) < strlen($in)) {
file_put_contents($dest, $out);
printf("+ minify from %-40s to %-40s: %2d%% of %6d\n", substr($srce, $l), substr($dest, $l), round(strlen($out)*100/strlen($in)), strlen($in));
} else {
printf("- minify from %40s to %40s: skipped", substr($srce, $l), substr($dest, $l));
}
}
private function taskMinifyJS($srce, $dest) {
$l = strlen(dirname(__DIR__))+1;
$in = file_get_contents($srce);
$jsqueeze = new \Patchwork\JSqueeze();
$out = $jsqueeze->squeeze($in);
if ($out && strlen($out) < strlen($in)) {
file_put_contents($dest, $out);
printf("+ minify from %-40s to %-40s: %2d%% of %6d\n", substr($srce, $l), substr($dest, $l), round(strlen($out)*100/strlen($in)), strlen($in));
} else {
printf("- minify from %40s to %40s: skipped", substr($srce, $l), substr($dest, $l));
}
}
/**
* Minify CSS stylesheets
*
* @param string $css_dir path of CSS files
*
* @return this
*/
public function minifyCSS($css_dir) {
if (is_dir($css_dir)) {
$it = new RegexIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($css_dir)
),
"/\\.css\$/i"
);
foreach ($it as $css_file) {
if (!$this->endsWith($css_file->getFilename(), 'min.css')) {
$this->taskMinifyCSS($css_file->getRealpath(), str_replace('.css', '.min.css', $css_file->getRealpath()));
}
}
}
return $this;
}
/**
* Minify JavaScript files stylesheets
*
* @param string $js_dir path of CSS files
*
* @return this
*/
public function minifyJS($js_dir) {
if (is_dir($js_dir)) {
$it = new RegexIterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($js_dir)
),
"/\\.js\$/i"
);
foreach ($it as $js_file) {
if (!$this->endsWith($js_file->getFilename(), 'min.js')) {
$this->taskMinifyJS($js_file->getRealpath(), str_replace('.js', '.min.js', $js_file->getRealpath()));
}
}
}
return $this;
}
/**
* Checks if a string ends with another string
*
* @param string $haystack Full string
* @param string $needle Ends string
*
* @return boolean
* @see http://stackoverflow.com/a/834355
*/
private function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
}
$task = new Task();
$task->minifyCSS( __DIR__ . '/../css')->minifyJS( __DIR__ . '/../js');
|