-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsfCombineUtility.class.php
318 lines (281 loc) · 6.99 KB
/
sfCombineUtility.class.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Utility class of static methods for sfCombine
*
* @package sfCombine
* @subpackage sfCombineUtility
* @author Alexandre Mogère
* @author Kevin Dew <kev@dewsolutions.co.uk>
*/
class sfCombineUtility
{
/**
* Check whether or not a file is combinable. Reasons for files not being
* combinable are, a url with a protocol (likely to be a different server),
* a file path in full or everything before the question mark is in the
* $doNotCombine array, or if the file does not exist (could be a dynamic
* file)
*
* @param string $file File name
* @param array $doNotCombine (Optional) Array of files not to combine.
* Default empty array.
* @return bool
*/
static public function combinableFile($file, array $doNotCombine = array())
{
// check for a remote or file we've specified not to combine
if (
strpos($file, '://')
||
self::skipAsset($file, $doNotCombine)
)
{
return false;
}
// remove anything past the question mark
$fileParts = explode('?', $file);
$file = $fileParts[0];
if (self::skipAsset($file, $doNotCombine))
{
return false;
}
// check absolute file exists
if (
(0 === strpos($file, '/'))
&& !self::getFilePath($file)
)
{
return false;
}
return true;
}
/**
* Get the last modified timestamp from a file
*
* @param string $file
* @param mixed Method used to retrieve the asset path
* @return int
*/
static public function getModifiedTimestamp($file, $assetPathMethod)
{
// prefix asset path (if applicable)
if ($assetPathMethod && is_callable($assetPathMethod))
{
$file = call_user_func($assetPathMethod, $file);
}
if (!self::combinableFile($file, array()))
{
return 0;
}
$fileParts = explode('?', $file);
$file = $fileParts[0];
$filePath = self::getFilePath($file);
if ($filePath)
{
$lastModified = filemtime($filePath);
if ($lastModified)
{
return $lastModified;
}
}
return 0;
}
/**
* Get the path to a file as long as the file exists.
*
* @param string $file
* @return string|false False if file doesn't exist
*/
static public function getFilePath($file)
{
$paths = array(
sfConfig::get('sf_web_dir') . $file,
sfConfig::get('sf_symfony_data_dir') . '/web' . $file
);
foreach ($paths as $path)
{
if (file_exists($path))
{
return $path;
}
}
return false;
}
/**
* Normalize a path (get rid of relatives)
*
* based on: http://stackoverflow.com/questions/4049856/replace-phps-realpath
*
* @static
* @param string $path
* @return mixed
*/
static public function normalizePath($path)
{
if (strlen($path) == 0) {
return $path;
}
$normalizedPath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $normalizedPath), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
$normalizedPath = implode(DIRECTORY_SEPARATOR, $absolutes);
return $path{0} == '/' ? '/' . $normalizedPath : $normalizedPath;
}
/**
* Whether or not this is a file that should be skipped
*
* @param string $file
* @param array $doNotCombine
* @return bool
*/
static public function skipAsset($file, array $doNotCombine = array())
{
return
in_array($file, $doNotCombine)
||
in_array(basename($file), $doNotCombine)
;
}
/**
* Get the cache directory for sfCombine
*
* @return string
*/
static public function getCacheDir()
{
return sfConfig::get('sf_cache_dir') . '/'
. sfConfig::get('app_sfCombinePlugin_cache_dir','sfCombine')
;
}
/**
* Send GZip headers if possible
*
* @author Alexandre Mogère
* @return void
*/
static public function setGzip()
{
// gzip compression
if (
sfConfig::get('app_sfCombinePlugin_gzip', true)
&& !self::_checkGzipFail()
)
{
ob_start("ob_gzhandler");
}
}
/**
* Send cache headers if possible.
*
* @author Alexandre Mogère
* @param sfResponse $response
*/
static public function setCacheHeaders($response)
{
$max_age = sfConfig::get('app_sfCombinePlugin_client_cache_max_age', false);
if ($max_age !== false)
{
$lifetime = $max_age * 86400; // 24*60*60
$response->addCacheControlHttpHeader('max-age', $lifetime);
$response->setHttpHeader(
'Pragma',
sfConfig::get('app_sfCombinePlugin_pragma_header', 'public')
);
$response->setHttpHeader(
'Expires', $response->getDate(time() + $lifetime)
);
}
}
/**
* Check whether we can send gzip
*
* @author Alexandre Mogère
* @return bool
*/
static protected function _checkGzipFail()
{
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if (
strpos($userAgent, 'Mozilla/4.0 (compatible; MSIE ') !== 0
||
strpos($userAgent, 'Opera') !== false
)
{
return false;
}
$version = floatval(substr($userAgent, 30));
return $version < 6 || ($version == 6 && strpos($userAgent, 'SV1') === false);
}
/**
*
* @param string $js
* @return string
*/
static public function minifyInlineJs($js)
{
if (!sfConfig::get('app_sfCombinePlugin_enabled', false))
{
return $js;
}
// minify content
$config = sfConfig::get('app_sfCombinePlugin_js', array());
$combinerClass = isset($config['combiner_class'])
? $config['combiner_class']
: 'sfCombineCombinerJs'
;
$combiner = new $combinerClass(
$config
);
$js = $combiner->minify(
$js,
(isset($config['inline_minify_method'])
? $config['inline_minify_method']
: false
),
(isset($config['inline_minify_method_options'])
? $config['inline_minify_method_options']
: array()
)
);
return $js;
}
/**
*
* @param string $css
* @return string
*/
static public function minifyInlineCss($css)
{
if (!sfConfig::get('app_sfCombinePlugin_enabled', false))
{
return $css;
}
$config = sfConfig::get('app_sfCombinePlugin_css', array());
$combinerClass = isset($config['combiner_class'])
? $config['combiner_class']
: 'sfCombineCombinerCss';
$combiner = new $combinerClass(
$config
);
$css = $combiner->minify(
$css,
(isset($config['inline_minify_method'])
? $config['inline_minify_method']
: false
),
(isset($config['inline_minify_method_options'])
? $config['inline_minify_method_options']
: array()
)
);
return $css;
}
}