forked from dead23angel/smarty-combine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.combine.php
237 lines (190 loc) · 7.83 KB
/
function.combine.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty combine function plugin
*
* Type: function<br>
* Name: combine<br>
* Date: September 5, 2015
* Purpose: Combine content from several JS or CSS files into one
* Input: string to count
* Example: {combine input=$array_of_files_to_combine output=$path_to_output_file use_true_path=true age=$seconds_to_try_recombine_file}
*
* @author Gorochov Ivan <dead23angel at gmail dot com>
* @version 1.0
* @param array
* @param string
* @param int
* @return string
*/
function smarty_function_combine($params, &$smarty)
{
require_once dirname(__FILE__) . '/minify/JSmin.php';
require_once dirname(__FILE__) . '/minify/CSSmin.php';
/**
* Build combined file
*
* @param array $params
*/
if ( ! function_exists('smarty_build_combine')) {
function smarty_build_combine($params)
{
$filelist = array();
$lastest_mtime = 0;
foreach ($params['input'] as $item) {
$mtime = filemtime($params['file_path'] . $item);
$lastest_mtime = max($lastest_mtime, $mtime);
$filelist[] = array('name' => $item, 'time' => $mtime);
}
if ($params['debug'] == true) {
$output_filename = '';
foreach ($filelist as $file) {
if ($params['type'] == 'js') {
$output_filename .= '<script type="text/javascript" src="' . base_url() . $file['name'].'" charset="utf-8"></script>' . "\n";
} elseif ($params['type'] == 'css') {
$output_filename .= '<link type="text/css" rel="stylesheet" href="' . base_url() . $file['name'] . '" />' . "\n";
}
}
echo $output_filename;
return;
}
$last_cmtime = 0;
if (file_exists($params['file_path'] . $params['cache_file_name'])) {
$last_cmtime = file_get_contents($params['file_path'] . $params['cache_file_name']);
}
if ($lastest_mtime > $last_cmtime) {
$glob_mask = preg_replace('/\.(js|css)$/i', '_*.$1', $params['output']);
$files_to_cleanup = glob($params['file_path'] . $glob_mask);
foreach ($files_to_cleanup as $cfile) {
if (is_file($cfile) && file_exists($cfile)) {
unlink($cfile);
}
}
$output_filename = preg_replace('/\.(js|css)$/i', date('_YmdHis.', $lastest_mtime) . '$1', $params['output']);
$dirname = dirname($params['file_path'] . $output_filename);
if ( ! is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
$fh = fopen($params['file_path'] . $output_filename, 'w');
if (flock($fh, LOCK_EX)) {
foreach ($filelist as $file) {
$min = '';
if ($params['type'] == 'js') {
$min = JSMin::minify(file_get_contents($params['file_path'] . $file['name']));
} elseif ($params['type'] == 'css') {
$min = CSSMin::minify(file_get_contents($params['file_path'] . $file['name']));
} else {
fputs($fh, PHP_EOL . PHP_EOL . '/* ' . $file['name'] . ' @ ' . date('c', $file['time']) . ' */' . PHP_EOL . PHP_EOL);
$min = file_get_contents($params['file_path'] . $file['name']);
}
fputs($fh, $min);
}
flock($fh, LOCK_UN);
file_put_contents($params['file_path'] . $params['cache_file_name'], $lastest_mtime, LOCK_EX);
}
fclose($fh);
clearstatcache();
}
touch($params['file_path'] . $params['cache_file_name']);
smarty_print_out($params);
}
}
/**
* Print filename
*
* @param string $params
*/
if ( ! function_exists('smarty_print_out')) {
function smarty_print_out($params)
{
$last_mtime = 0;
if (file_exists($params['file_path'] . $params['cache_file_name'])) {
$last_mtime = file_get_contents($params['file_path'] . $params['cache_file_name']);
}
$output_filename = preg_replace('/\.(js|css)$/i', date('_YmdHis.', $last_mtime) . '$1', $params['output']);
if ($params['type'] == 'js') {
echo '<script type="text/javascript" src="' . $output_filename . '" charset="utf-8"></script>';
} elseif ($params['type'] == 'css') {
echo '<link type="text/css" rel="stylesheet" href="' . $output_filename . '" />';
} else {
echo $output_filename;
}
}
}
/**
* This function gets the base url for the project where this plugin is used
* If this plugin is used within Code Igniter, the base_url() would have already been defined
*/
if ( ! function_exists('base_url')) {
function base_url(){
return sprintf(
"%s://%s%s/",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
rtrim(dirname($_SERVER['PHP_SELF']), '/\\')
);
}
}
// The new 'use_true_path' option that tells this plugin to use the path to the files as it is
if ( isset($params['use_true_path']) && !is_bool($params['use_true_path'])) {
trigger_error('use_true_path must be boolean', E_USER_NOTICE);
return;
}
if ( ! isset($params['use_true_path'])) {
$params['use_true_path'] = false;
}
// use the relative path or the true path of the file based on the 'use_true_path' option passed in
$params['file_path'] = ($params['use_true_path']) ? '' : getenv('DOCUMENT_ROOT');
if ( ! isset($params['input'])) {
trigger_error('input cannot be empty', E_USER_NOTICE);
return;
}
if ( ! is_array($params['input']) || count($params['input']) < 1) {
trigger_error('input must be array and have one item at least', E_USER_NOTICE);
return;
}
foreach ($params['input'] as $file) {
if ( ! file_exists($params['file_path'] . $file)) {
trigger_error('File ' . $params['file_path'] . $file . ' does not exist!', E_USER_WARNING);
return;
}
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( ! in_array($ext, array('js', 'css'))) {
trigger_error('all input files must have js or css extension', E_USER_NOTICE);
return;
}
$files_extensions[] = $ext;
}
if (count(array_unique($files_extensions)) > 1) {
trigger_error('all input files must have the same extension', E_USER_NOTICE);
return;
}
$params['type'] = $ext;
if ( ! isset($params['output'])) {
$params['output'] = dirname($params['input'][0]) . '/combined.' . $ext;
}
if ( ! isset($params['age'])) {
$params['age'] = 3600;
}
if ( ! isset($params['cache_file_name'])) {
$params['cache_file_name'] = $params['output'] . '.cache';
}
if ( ! isset($params['debug'])) {
$params['debug'] = false;
}
$cache_file_name = $params['cache_file_name'];
if ($params['debug'] == true || ! file_exists($params['file_path'] . $cache_file_name)) {
smarty_build_combine($params);
return;
}
$cache_mtime = filemtime($params['file_path'] . $cache_file_name);
if ($cache_mtime + $params['age'] < time()) {
smarty_build_combine($params);
} else {
smarty_print_out($params);
}
}