forked from janmarek/WebLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebLoader.php
454 lines (348 loc) · 8.09 KB
/
WebLoader.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
namespace WebLoader;
use Nette\SafeStream;
/**
* Web loader
*
* @author Jan Marek
* @license MIT
*/
abstract class WebLoader extends \Nette\Application\Control {
// <editor-fold defaultstate="collapsed" desc="variables">
/** @var string */
private $sourcePath;
/** @var string */
private $tempPath;
/** @var string */
private $tempUri;
/** @var bool */
private $joinFiles = true;
/** @var string */
private $generatedFileNamePrefix = "webloader-";
/** @var string */
private $generatedFileNameSuffix = "";
/** @var bool */
private $throwExceptions = false;
/** @var array */
public $filters = array();
/** @var array */
public $fileFilters = array();
/** @var array */
private $files = array();
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="getters & setters">
/**
* Get source path
* @return string
*/
public function getSourcePath() {
return $this->sourcePath;
}
/**
* Set source path
* @param string source path
* @return WebLoader
*/
public function setSourcePath($sourcePath) {
$sourcePath = realpath($sourcePath);
if ($sourcePath === false) {
throw new \FileNotFoundException("Source path does not exist.");
}
$this->sourcePath = $sourcePath;
return $this;
}
/**
* Get temp path
* @return string
*/
public function getTempPath() {
return $this->tempPath;
}
/**
* Set temp path
* @param string temp path
* @return WebLoader
*/
public function setTempPath($tempPath) {
$tempPath = realpath($tempPath);
if ($tempPath === false) {
throw new \FileNotFoundException("Temp path does not exist.");
}
if (!is_writable($tempPath)) {
throw new \InvalidStateException("Directory '$tempPath' is not writeable.");
}
$this->tempPath = $tempPath;
return $this;
}
/**
* Get temp uri
* @return string
*/
public function getTempUri() {
return $this->tempUri;
}
/**
* Set temp uri
* @param string temp uri
* @return string
*/
public function setTempUri($tempUri) {
$this->tempUri = (string) $tempUri;
return $this;
}
/**
* Get join files
* @return bool
*/
public function getJoinFiles() {
return $this->joinFiles;
}
/**
* Set join files
* @param bool join files
* @return WebLoader
*/
public function setJoinFiles($joinFiles) {
$this->joinFiles = (bool) $joinFiles;
return $this;
}
/**
* Get generated file name prefix
* @return string
*/
public function getGeneratedFileNamePrefix() {
return $this->generatedFileNamePrefix;
}
/**
* Set generated file name prefix
* @param string generated file name prefix
* @return WebLoader
*/
public function setGeneratedFileNamePrefix($generatedFileNamePrefix) {
$this->generatedFileNamePrefix = (string) $generatedFileNamePrefix;
return $this;
}
/**
* Get generated file name suffix
* @return string
*/
public function getGeneratedFileNameSuffix() {
return $this->generatedFileNameSuffix;
}
/**
* Set generated file name suffix
* @param string generated file name suffix
* @return WebLoader
*/
public function setGeneratedFileNameSuffix($generatedFileNameSuffix) {
$this->generatedFileNameSuffix = (string) $generatedFileNameSuffix;
return $this;
}
/**
* Throw exceptions?
* @return bool
*/
public function getThrowExceptions() {
return $this->throwExceptions;
}
/**
* Set throw exceptions
* @param bool throw exceptions
* @return WebLoader
*/
public function setThrowExceptions($throwExceptions) {
$this->throwExceptions = (bool) $throwExceptions;
return $this;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="files">
/**
* Get file list
* @return array
*/
public function getFiles() {
return $this->files;
}
/**
* Make path absolute
* @param string path
* @throws \FileNotFoundException
* @return string
*/
public function cannonicalizePath($path) {
$rel = realpath($this->sourcePath . "/" . $path);
if ($rel !== false) return $rel;
$abs = realpath($path);
if ($abs !== false) return $abs;
throw new \FileNotFoundException("File '$path' does not exist.");
}
/**
* Add file
* @param string filename
*/
public function addFile($file) {
try {
$file = $this->cannonicalizePath($file);
if (in_array($file, $this->files)) {
return;
}
$this->files[] = $file;
} catch (\FileNotFoundException $e) {
if ($this->throwExceptions) {
throw $e;
}
}
}
/**
* Add files
* @param array list of files
*/
public function addFiles(array $files) {
foreach ($files as $file) {
$this->addFile($file);
}
}
/**
* Remove file
* @param string filename
*/
public function removeFile($file) {
$this->removeFiles(array($file));
}
/**
* Remove files
* @param array list of files
*/
public function removeFiles(array $files) {
$files = array_map(array($this, "cannonicalizePath"), $files);
$this->files = array_diff($this->files, $files);
}
/**
* Remove all files
*/
public function clear() {
$this->files = array();
}
// </editor-fold>
/**
* Get html element including generated content
* @param string source
* @return Html
*/
abstract public function getElement($source);
/**
* Generate compiled file(s) and render link(s)
*/
public function render() {
$hasArgs = func_num_args() > 0;
if ($hasArgs) {
$backup = $this->files;
$this->clear();
$this->addFiles(func_get_args());
}
// joined files
if ($this->joinFiles) {
$file = $this->generate($this->files);
echo $this->getElement($this->tempUri . "/" . $file);
// separated files
} else {
foreach ($this->files as $file) {
$file = $this->generate(array($file));
echo $this->getElement($this->tempUri . "/" . $file);
}
}
if ($hasArgs) {
$this->files = array_merge($this->files, $backup);
// joined files
if($this->joinFiles) {
$file = $this->generate($backup);
echo $this->getElement($this->tempUri . "/" . $file);
// seperated files
} else {
foreach($backup as $file) {
$file = $this->generate(array($file));
echo $this->getElement($this->tempUri . "/" . $file);
}
}
}
}
/**
* Get last modified timestamp of newest file
* @param array files
* @return int
*/
public function getLastModified(array $files = null) {
if ($files === null) {
$files = $this->files;
}
$modified = 0;
foreach ($files as $file) {
$modified = max($modified, filemtime($file));
}
return $modified;
}
/**
* Filename of generated file
* @param array files
* @return string
*/
public function getGeneratedFilename(array $files = null) {
if ($files === null) {
$files = $this->files;
}
$name = substr(md5(implode("|", $files)), 0, 12);
if (count($files) === 1) {
$name .= "-" . pathinfo($files[0], PATHINFO_FILENAME);
}
return $this->generatedFileNamePrefix . $name . $this->generatedFileNameSuffix;
}
/**
* Get joined content of all files
* @param array files
* @return string
*/
public function getContent(array $files = null) {
if ($files === null) {
$files = $this->files;
}
// load content
$content = "";
foreach ($files as $file) {
$content .= $this->loadFile($file);
}
// apply filters
foreach ($this->filters as $filter) {
$content = call_user_func($filter, $content, $this);
}
return $content;
}
/**
* Load content and save file
* @param array files
* @return string filename of generated file
*/
protected function generate($files) {
$name = $this->getGeneratedFilename($files);
$path = $this->tempPath . "/" . $name;
$lastModified = $this->getLastModified($files);
if (!file_exists($path) || $lastModified > filemtime($path)) {
if (!in_array(SafeStream::PROTOCOL, stream_get_wrappers())) {
SafeStream::register();
}
file_put_contents("safe://" . $path, $this->getContent($files));
}
return $name . "?" . $lastModified;
}
/**
* Load file
* @param string file path
* @return string
*/
protected function loadFile($file) {
$content = file_get_contents($file);
foreach ($this->fileFilters as $filter) {
$content = call_user_func($filter, $content, $this, $file);
}
return $content;
}
}