-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
387 lines (304 loc) · 11.3 KB
/
lib.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Copyright (c) 2012 Chris Van Patten.
*/
class QuickAsset {
private $showMethods = array();
private $bustMethods = array();
private $assetTypes = array();
private $hosts = array();
private $assetTypesToHosts = array(); // more efficient lookup table
public function __construct() {
// prepare the default show methods and bust methods
$this->addShowMethod('_default', function($host, $assetFile, $assetPath, $bustString) {
// get the position of the last dot (the file extension)
$lastDot = strrpos($assetFile, '.');
// get the substring before the last dot (beginning) and after (end)
$beginning = substr($assetFile, 0, $lastDot);
$end = substr($assetFile, $lastDot, strlen($assetFile));
// stitch together, putting the bust string in beteen $beginning and $end
return $host . $assetPath . $beginning . '.' . $bustString . $end;
});
$this->addBustMethod('_default', function($assetPath, $assetFile, $rootPath) {
return date('YmdHis', @filemtime($rootPath . $assetPath . $assetFile));
});
}
public function addShowMethod($methodName, $function) {
// sanity checking
if (!is_string($methodName) || strlen($methodName) < 1)
{
trigger_error('QuickAsset::addShowMethod expects parameter 1 to be a non-empty string, ' . gettype($methodName) .' given', E_USER_WARNING);
return false;
}
if (!is_callable($function))
{
trigger_error('QuickAsset::addShowMethod expects parameter 2 to be a callable function (anonymous function, string of callable function or array of strings to class/function)', E_USER_WARNING);
return false;
}
// add to our internal store of show methods
if (!in_array($methodName, array_keys($this->showMethods)))
{
$this->showMethods[$methodName] = $function;
return true;
}
else {
trigger_error('The show method name "' . htmlentities(strip_tags($methodName), ENT_QUOTES) . '" was already registered. Leaving the old one alone', E_USER_NOTICE);
return false;
}
}
public function addBustMethod($methodName, $function) {
// sanity checking
if (!is_string($methodName) || strlen($methodName) < 1)
{
trigger_error('QuickAsset::addBustMethod expects parameter 1 to be a non-empty string, ' . gettype($methodName) .' given', E_USER_WARNING);
return false;
}
if (!is_callable($function))
{
trigger_error('QuickAsset::addBustMethod expects parameter 2 to be a callable function (anonymous function, string of callable function or array of strings to class/function)', E_USER_WARNING);
return false;
}
// add to our internal store of bust methods
if (!in_array($methodName, array_keys($this->bustMethods)))
{
$this->bustMethods[$methodName] = $function;
return true;
}
else {
trigger_error('The bust method name "' . htmlentities(strip_tags($methodName), ENT_QUOTES) . '" was already registered. Leaving the old one alone', E_USER_NOTICE);
return false;
}
}
public function addAssetType($assetType, $parameters) {
// sanity checking
if (!is_string($assetType) || strlen($assetType) < 1)
{
trigger_error('QuickAsset::addAssetType expects parameter 1 to be a non-empty string, ' . gettype($assetType) .' given', E_USER_WARNING);
return false;
}
if (!is_array($parameters) || count($parameters) < 1)
{
trigger_error('QuickAsset::addAssetType expects parameter 2 to be a non-empty array, ' . gettype($parameters) . ' given.', E_USER_WARNING);
return false;
}
if (!array_key_exists('assetPath', $parameters))
{
trigger_error('QuickAsset::addAssetType expects the $parameters array to include \'assetPath\'', E_USER_WARNING);
return false;
}
if (strlen($parameters['assetPath']) < 1)
{
trigger_error('QuickAsset::addAssetType expects the \'assetPath\' parameter to not be empty', E_USER_WARNING);
return false;
}
// prepare the asset type for internal storage -- prepare custom callback parameters, or use defaults
if (array_key_exists('bustMethod', $parameters))
{
$bustMethod = $parameters['bustMethod'];
}
else {
$bustMethod = '_default';
}
if (array_key_exists('showMethod', $parameters))
{
$showMethod = $parameters['showMethod'];
}
else {
$showMethod = '_default';
}
if (array_key_exists('rootPath', $parameters))
{
$rootPath = $parameters['rootPath'];
}
else {
$rootPath = dirname(realpath($_SERVER['SCRIPT_FILENAME'])) . '/';
}
// add the new asset type
if (!in_array($assetType, $this->assetTypes))
{
$this->assetTypes[$assetType] = array(
'assetPath' => $parameters['assetPath'],
'bustMethod' => $bustMethod,
'showMethod' => $showMethod,
'rootPath' => $rootPath
);
return true;
}
else {
trigger_error('The asset type "' . htmlentities(strip_tags($assetType), ENT_QUOTES) . '" was already registered. Leaving the old one alone', E_USER_NOTICE);
return false;
}
}
public function addHost($host, $parameters) {
// sanity checking
if (!is_string($host))
{
trigger_error('QuickAsset::addHost expects parameter 1 to be a string, ' . gettype($host) .' given', E_USER_WARNING);
return false;
}
if (strlen($host) < 1)
{
$host = '_'; // default host needs a default key
}
if (!is_array($parameters) || count($parameters) < 1)
{
trigger_error('QuickAsset::addHost expects parameter 2 to be a non-empty array, ' . gettype($parameters) . ' given.', E_USER_WARNING);
return false;
}
if (!array_key_exists('assetTypes', $parameters))
{
trigger_error('QuickAsset::addHost expects the $parameters array to include \'assetTypes\'', E_USER_WARNING);
return false;
}
if (strlen($parameters['assetTypes']) < 1)
{
trigger_error('QuickAsset::addHost expects the \'assetTypes\' parameter to not be empty', E_USER_WARNING);
return false;
}
// bring $assetTypes to bind this host to into an array
$assetTypes = explode(',', str_replace(' ', '', $parameters['assetTypes']));
if (count($assetTypes) < 1)
{
trigger_error('QuickAsset::addHost expects \'assetTypes\' to have at least one type defined', E_USER_WARNING);
return false;
}
// handle domain cycling
//defaults
$shouldCycleHosts = false;
$maxHosts = 0;
if (strpos($host, '$') !== false)
{
// check for 'maxHosts' parameter key
if (!array_key_exists('maxHosts', $parameters))
{
trigger_error('Found a \'$\' in the hostname, but the \'maxHosts\' parameter was not set', E_USER_NOTICE);
}
else {
$maxHosts = filter_var($parameters['maxHosts'], FILTER_VALIDATE_INT);
if ($maxHosts === false)
{
trigger_error('QuickAsset::addHost expects the \'maxHosts\' parameter to be integer, '.gettype($parameters['maxHosts']).' given', E_USER_WARNING);
$maxHosts = 0;
}
$shouldCycleHosts = true;
}
}
// add host to internal store
if (!in_array($host, $this->hosts))
{
$this->hosts[$host] = array(
'shouldCycleHosts' => $shouldCycleHosts,
'maxHosts' => $maxHosts
);
// add to lookup table
foreach ($assetTypes as $at)
{
if (array_key_exists($at, $this->assetTypesToHosts))
{
trigger_error('QuickAsset::addHost expects that only one host is bound to a an asset type (trying to assign "'.htmlentities(strip_tags($at), ENT_QUOTES).'" to two hosts)', E_USER_WARNING);
}
else {
$this->assetTypesToHosts[$at] = $host;
}
}
}
return true;
}
protected function cycleHosts($host)
{
if (!array_key_exists('hostCounter', $this->hosts[$host]))
{
$this->hosts[$host]['hostCounter'] = 0;
}
$cycledHost = str_replace('$', $this->hosts[$host]['hostCounter'], $host);
if ( $this->hosts[$host]['hostCounter'] < ($this->hosts[$host]['maxHosts'] - 1) ) {
++$this->hosts[$host]['hostCounter'];
} else {
$this->hosts[$host]['hostCounter'] = 0;
}
return $cycledHost;
}
public function url($assetType, $assetFile) {
/*
* 1. Get $assetType
* 2. Determine which host should serve it
* 3. Determine which bustMethod and showMethod apply
* 4. Do magic
* 5. ...
* 6. Profit!
*/
/*
In slightly more technical detail...
Thinking...
1. Get $assetType
2. Go through class $hosts, find most specific match... or default
3. Choose bustMethod from the assetType's $parameters... or default
4. Choose showMethod from the assetType's $parameters... or default
5. Execute host handler to pull a processed host string (rotation, etc.)
(host handler uses $parameters to either return unfiltered,
or swap '$' for host number based on internal counter and max.
from $params)
6. Execute bustMethod with $assetFile, $filteredHost. Return $bustedString
7. Execute showMethod with $assetFile, $bustedString, $filteredHost. Return $assetURL.
8. Echo $assetURL.
Considering... does bustMethod need to know about the on-disk filename? Will this be a problem?
*/
// do we know about this asset type?
if (!array_key_exists($assetType, $this->assetTypes))
{
trigger_error('QuickAsset::url expects parameter 1 to be an $assetType that has already been registered', E_USER_WARNING);
return false;
}
// has it been bound to a host?
if (!array_key_exists($assetType, $this->assetTypesToHosts))
{
trigger_error('QuickAsset::url expects parameter 1 to be an $assetType that has been bound to a host (please use QuickAsset::addHost)', E_USER_WARNING);
return false;
}
$host = $this->assetTypesToHosts[$assetType];
// is the bust method callable?
$bustMethod = $this->assetTypes[$assetType]['bustMethod'];
if (!array_key_exists($bustMethod, $this->bustMethods))
{
trigger_error('QuickAsset::url expects the bustMethod "' . htmlentities(strip_tags($bustMethod), ENT_QUOTES) . '" to be registered before trying to use it', E_USER_WARNING);
return false;
}
// is the show method callable?
$showMethod = $this->assetTypes[$assetType]['showMethod'];
if (!array_key_exists($showMethod, $this->showMethods))
{
trigger_error('QuickAsset::url expects the showMethod "' . htmlentities(strip_tags($showMethod), ENT_QUOTES) . '" to be registered before trying to use it', E_USER_WARNING);
return false;
}
// does the host exist?
if (!array_key_exists($host, $this->hosts))
{
trigger_error('QuickAsset::url expects the host "' . htmlentities(strip_tags($host), ENT_QUOTES) . '" to be registered before trying to use it', E_USER_WARNING);
return false;
}
// cycle hosts if necessary
if ($this->hosts[$host]['shouldCycleHosts'])
{
$hostString = $this->cycleHosts($host);
}
else {
$hostString = $host;
}
if ($hostString == '_')
{
// deal with default host case
$hostString = '';
}
$assetPath = $this->assetTypes[$assetType]['assetPath'];
$rootPath = $this->assetTypes[$assetType]['rootPath'];
// execute bust method to get bust string
$bustString = call_user_func_array($this->bustMethods[$bustMethod], array($assetPath, $assetFile, $rootPath));
// execute show method to get finished string
$finishedAsset = call_user_func_array($this->showMethods[$showMethod], array($hostString, $assetFile, $assetPath, $bustString));
// post filtering
//eesca
// return/echo
return $finishedAsset;
}
}
?>