1
|
|
|
<?php namespace Anomaly\Streams\Platform\Asset; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Theme\ThemeCollection; |
4
|
|
|
use Anomaly\Streams\Platform\Application\Application; |
5
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\CoffeeFilter; |
6
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\CssMinFilter; |
7
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\JsMinFilter; |
8
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\LessFilter; |
9
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\NodeLessFilter; |
10
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\ParseFilter; |
11
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\RubyScssFilter; |
12
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\ScssFilter; |
13
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\SeparatorFilter; |
14
|
|
|
use Anomaly\Streams\Platform\Asset\Filter\StylusFilter; |
15
|
|
|
use Anomaly\Streams\Platform\Routing\UrlGenerator; |
16
|
|
|
use Assetic\Asset\AssetCollection; |
17
|
|
|
use Assetic\Asset\FileAsset; |
18
|
|
|
use Assetic\Asset\GlobAsset; |
19
|
|
|
use Assetic\Filter\PhpCssEmbedFilter; |
20
|
|
|
use Collective\Html\HtmlBuilder; |
21
|
|
|
use Illuminate\Config\Repository; |
22
|
|
|
use Illuminate\Filesystem\Filesystem; |
23
|
|
|
use Illuminate\Http\Request; |
24
|
|
|
use League\Flysystem\MountManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Asset |
28
|
|
|
* |
29
|
|
|
* This is the asset management class. It handles front |
30
|
|
|
* and backend asset's for everything. |
31
|
|
|
* |
32
|
|
|
* @link http://anomaly.is/streams-platform |
33
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
34
|
|
|
* @author Ryan Thompson <[email protected]> |
35
|
|
|
* @package Anomaly\Streams\Platform\Asset |
36
|
|
|
*/ |
37
|
|
|
class Asset |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The public base directory. |
42
|
|
|
* |
43
|
|
|
* @var null |
44
|
|
|
*/ |
45
|
|
|
protected $directory = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Groups of assets. Groups can |
49
|
|
|
* be single files as well. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $collections = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The URL generator. |
57
|
|
|
* |
58
|
|
|
* @var UrlGenerator |
59
|
|
|
*/ |
60
|
|
|
protected $url; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The HTML utility. |
64
|
|
|
* |
65
|
|
|
* @var HtmlBuilder |
66
|
|
|
*/ |
67
|
|
|
protected $html; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The files system. |
71
|
|
|
* |
72
|
|
|
* @var Filesystem |
73
|
|
|
*/ |
74
|
|
|
protected $files; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Asset path hints by namespace. |
78
|
|
|
* |
79
|
|
|
* 'module.users' => 'the/resources/path' |
80
|
|
|
* |
81
|
|
|
* @var AssetPaths |
82
|
|
|
*/ |
83
|
|
|
protected $paths; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The asset parser utility. |
87
|
|
|
* |
88
|
|
|
* @var AssetParser |
89
|
|
|
*/ |
90
|
|
|
protected $parser; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The theme collection. |
94
|
|
|
* |
95
|
|
|
* @var ThemeCollection |
96
|
|
|
*/ |
97
|
|
|
protected $themes; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The mount manager. |
101
|
|
|
* |
102
|
|
|
* @var MountManager |
103
|
|
|
*/ |
104
|
|
|
protected $manager; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The HTTP request. |
108
|
|
|
* |
109
|
|
|
* @var Request |
110
|
|
|
*/ |
111
|
|
|
protected $request; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The stream application. |
115
|
|
|
* |
116
|
|
|
* @var Application |
117
|
|
|
*/ |
118
|
|
|
protected $application; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* The config repository. |
122
|
|
|
* |
123
|
|
|
* @var array |
124
|
|
|
*/ |
125
|
|
|
protected $config; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Create a new Application instance. |
129
|
|
|
* |
130
|
|
|
* @param Application $application |
131
|
|
|
* @param ThemeCollection $themes |
132
|
|
|
* @param MountManager $manager |
133
|
|
|
* @param AssetParser $parser |
134
|
|
|
* @param Repository $config |
135
|
|
|
* @param Filesystem $files |
136
|
|
|
* @param AssetPaths $paths |
137
|
|
|
* @param Request $request |
138
|
|
|
* @param HtmlBuilder $html |
139
|
|
|
* @param UrlGenerator $url |
140
|
|
|
*/ |
141
|
|
|
public function __construct( |
142
|
|
|
Application $application, |
143
|
|
|
ThemeCollection $themes, |
144
|
|
|
MountManager $manager, |
145
|
|
|
AssetParser $parser, |
146
|
|
|
Repository $config, |
147
|
|
|
Filesystem $files, |
148
|
|
|
AssetPaths $paths, |
149
|
|
|
Request $request, |
|
|
|
|
150
|
|
|
HtmlBuilder $html, |
151
|
|
|
UrlGenerator $url |
152
|
|
|
) { |
153
|
|
|
$this->url = $url; |
154
|
|
|
$this->html = $html; |
155
|
|
|
$this->files = $files; |
156
|
|
|
$this->paths = $paths; |
157
|
|
|
$this->config = $config; |
|
|
|
|
158
|
|
|
$this->themes = $themes; |
159
|
|
|
$this->parser = $parser; |
160
|
|
|
$this->manager = $manager; |
161
|
|
|
$this->request = $request; |
162
|
|
|
$this->application = $application; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Add an asset or glob pattern to an asset collection. |
167
|
|
|
* |
168
|
|
|
* This should support the asset being the collection |
169
|
|
|
* and the asset (for single files) internally |
170
|
|
|
* so asset.links / asset.scripts will work. |
171
|
|
|
* |
172
|
|
|
* @param $collection |
173
|
|
|
* @param $file |
174
|
|
|
* @param array $filters |
175
|
|
|
* @return $this |
176
|
|
|
* @throws \Exception |
177
|
|
|
*/ |
178
|
|
|
public function add($collection, $file, array $filters = []) |
179
|
|
|
{ |
180
|
|
|
if (!isset($this->collections[$collection])) { |
181
|
|
|
$this->collections[$collection] = []; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$filters = $this->addConvenientFilters($file, $filters); |
185
|
|
|
|
186
|
|
|
$file = $this->paths->realPath($file); |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* If this is a remote or single existing |
190
|
|
|
* file then add it normally. |
191
|
|
|
*/ |
192
|
|
|
if (starts_with($file, ['http', '//']) || file_exists($file)) { |
193
|
|
|
|
194
|
|
|
$this->collections[$collection][$file] = $filters; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* If this is a valid glob pattern then add |
201
|
|
|
* it to the collection and add the glob filter. |
202
|
|
|
*/ |
203
|
|
|
if (count(glob($file)) > 0) { |
204
|
|
|
|
205
|
|
|
$this->collections[$collection][$file] = array_merge($filters, ['glob']); |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($this->config->get('app.debug')) { |
|
|
|
|
211
|
|
|
throw new \Exception("Asset [{$file}] does not exist!"); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Download a file and return it's path. |
217
|
|
|
* |
218
|
|
|
* @param $url |
219
|
|
|
* @param null $path |
220
|
|
|
* @return null|string |
221
|
|
|
*/ |
222
|
|
|
public function download($url, $path = null) |
223
|
|
|
{ |
224
|
|
|
if (!$this->files->exists($path = $this->paths->downloadPath($url, $path))) { |
225
|
|
|
|
226
|
|
|
if (!$this->files->isDirectory($directory = dirname($path = public_path(ltrim($path, '/\\'))))) { |
227
|
|
|
$this->files->makeDirectory($directory, 0777, true); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (!$this->files->exists($path)) { |
231
|
|
|
$this->files->put($path, file_get_contents($url)); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return str_replace(public_path(), '', $path); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Return the contents of a collection. |
240
|
|
|
* |
241
|
|
|
* @param $collection |
242
|
|
|
* @param array $filters |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function inline($collection, array $filters = []) |
246
|
|
|
{ |
247
|
|
|
return file_get_contents($this->paths->realPath('public::' . ltrim($this->path($collection, $filters), '/\\'))); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Return the URL to a compiled asset collection. |
252
|
|
|
* |
253
|
|
|
* @param $collection |
254
|
|
|
* @param array $filters |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function url($collection, array $filters = [], array $parameters = [], $secure = null) |
258
|
|
|
{ |
259
|
|
|
if (!isset($this->collections[$collection])) { |
260
|
|
|
$this->add($collection, $collection, $filters); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if (!$path = $this->getPath($collection, $filters)) { |
264
|
|
|
return null; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $this->url->asset($this->getPath($collection, $filters), $parameters, $secure); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Return the path to a compiled asset collection. |
272
|
|
|
* |
273
|
|
|
* @param $collection |
274
|
|
|
* @param array $filters |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function path($collection, array $filters = []) |
278
|
|
|
{ |
279
|
|
|
if (!isset($this->collections[$collection])) { |
280
|
|
|
$this->add($collection, $collection, $filters); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $this->getPath($collection, $filters); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Return the script tag for a collection. |
288
|
|
|
* |
289
|
|
|
* @param $collection |
290
|
|
|
* @param array $filters |
291
|
|
|
* @param array $attributes |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
public function script($collection, array $filters = [], array $attributes = []) |
295
|
|
|
{ |
296
|
|
|
$attributes['src'] = $this->path($collection, $filters); |
297
|
|
|
|
298
|
|
|
return '<script' . $this->html->attributes($attributes) . '></script>'; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Return the style tag for a collection. |
303
|
|
|
* |
304
|
|
|
* @param $collection |
305
|
|
|
* @param array $filters |
306
|
|
|
* @param array $attributes |
307
|
|
|
* @return string |
308
|
|
|
*/ |
309
|
|
View Code Duplication |
public function style($collection, array $filters = [], array $attributes = []) |
|
|
|
|
310
|
|
|
{ |
311
|
|
|
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet']; |
312
|
|
|
|
313
|
|
|
$attributes = $attributes + $defaults; |
314
|
|
|
|
315
|
|
|
$attributes['href'] = $this->path($collection, $filters); |
316
|
|
|
|
317
|
|
|
return '<link' . $this->html->attributes($attributes) . '>'; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Return an array of script tags. |
322
|
|
|
* |
323
|
|
|
* @param $collection |
324
|
|
|
* @param array $filters |
325
|
|
|
* @param array $attributes |
326
|
|
|
* @return array |
327
|
|
|
*/ |
328
|
|
|
public function scripts($collection, array $filters = [], array $attributes = []) |
329
|
|
|
{ |
330
|
|
|
return array_map( |
331
|
|
|
function ($path) use ($attributes) { |
332
|
|
|
|
333
|
|
|
$attributes['src'] = $path; |
334
|
|
|
|
335
|
|
|
return '<script' . $this->html->attributes($attributes) . '></script>'; |
336
|
|
|
}, |
337
|
|
|
$this->paths($collection, $filters) |
338
|
|
|
); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Return an array of style tags. |
343
|
|
|
* |
344
|
|
|
* @param $collection |
345
|
|
|
* @param array $filters |
346
|
|
|
* @param array $attributes |
347
|
|
|
* @return array |
348
|
|
|
*/ |
349
|
|
View Code Duplication |
public function styles($collection, array $filters = [], array $attributes = []) |
|
|
|
|
350
|
|
|
{ |
351
|
|
|
return array_map( |
352
|
|
|
function ($path) use ($attributes) { |
353
|
|
|
|
354
|
|
|
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet']; |
355
|
|
|
|
356
|
|
|
$attributes = $attributes + $defaults; |
|
|
|
|
357
|
|
|
|
358
|
|
|
$attributes['href'] = $path; |
359
|
|
|
|
360
|
|
|
return '<link' . $this->html->attributes($attributes) . '>'; |
361
|
|
|
}, |
362
|
|
|
$this->paths($collection, $filters) |
363
|
|
|
); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Return an array of paths to an asset collection. |
368
|
|
|
* |
369
|
|
|
* This instead of combining the collection contents |
370
|
|
|
* just returns an array of individual processed |
371
|
|
|
* paths instead. |
372
|
|
|
* |
373
|
|
|
* @param $collection |
374
|
|
|
* @param array $additionalFilters |
375
|
|
|
* @return array |
376
|
|
|
*/ |
377
|
|
|
public function paths($collection, array $additionalFilters = []) |
378
|
|
|
{ |
379
|
|
|
if (!isset($this->collections[$collection])) { |
380
|
|
|
return []; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return array_filter( |
384
|
|
|
array_map( |
385
|
|
|
function ($file, $filters) use ($additionalFilters) { |
386
|
|
|
|
387
|
|
|
$filters = array_filter(array_unique(array_merge($filters, $additionalFilters))); |
388
|
|
|
|
389
|
|
|
return $this->path($file, $filters); |
390
|
|
|
}, |
391
|
|
|
array_keys($this->collections[$collection]), |
392
|
|
|
array_values($this->collections[$collection]) |
393
|
|
|
) |
394
|
|
|
); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Return an array of style URLs. |
399
|
|
|
* |
400
|
|
|
* @param $collection |
401
|
|
|
* @param array $filters |
402
|
|
|
* @param array $attributes |
403
|
|
|
* @return array |
404
|
|
|
*/ |
405
|
|
|
public function urls($collection, array $filters = [], array $attributes = []) |
406
|
|
|
{ |
407
|
|
|
return array_map( |
408
|
|
|
function ($path) use ($attributes) { |
409
|
|
|
return $this->url($path); |
410
|
|
|
}, |
411
|
|
|
$this->paths($collection, $filters) |
412
|
|
|
); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param $collection |
417
|
|
|
* @param $filters |
418
|
|
|
* @return string |
419
|
|
|
*/ |
420
|
|
|
protected function getPath($collection, $filters) |
421
|
|
|
{ |
422
|
|
|
/** |
423
|
|
|
* If the asset is remote just return it. |
424
|
|
|
*/ |
425
|
|
|
if (starts_with($collection, ['http', '//'])) { |
426
|
|
|
return $collection; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$path = $this->paths->outputPath($collection); |
430
|
|
|
|
431
|
|
|
if ($this->shouldPublish($path, $collection, $filters)) { |
432
|
|
|
$this->publish($path, $collection, $filters); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/*if (file_exists($path) && filesize($path) == 0) { |
|
|
|
|
436
|
|
|
return null; |
437
|
|
|
}*/ |
438
|
|
|
|
439
|
|
|
return $this->paths->prefix() . $path; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Return the collection path. This |
444
|
|
|
* is primarily used to determine paths |
445
|
|
|
* to single assets. |
446
|
|
|
* |
447
|
|
|
* @param $collection |
448
|
|
|
* @return string |
449
|
|
|
*/ |
450
|
|
|
public function getCollectionPath($collection) |
451
|
|
|
{ |
452
|
|
|
return ($this->request->segment(1) == 'admin' ? 'admin' : 'public') . '/' . ltrim( |
453
|
|
|
str_replace(base_path(), '', $this->paths->realPath($collection)), |
454
|
|
|
'/\\' |
455
|
|
|
); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Publish the collection of assets to the path. |
460
|
|
|
* |
461
|
|
|
* @param $path |
462
|
|
|
* @param $collection |
463
|
|
|
* @param $additionalFilters |
464
|
|
|
*/ |
465
|
|
|
protected function publish($path, $collection, $additionalFilters) |
466
|
|
|
{ |
467
|
|
|
$path = ltrim($path, '/\\'); |
468
|
|
|
|
469
|
|
|
if (str_contains($collection, public_path())) { |
470
|
|
|
return; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$assets = $this->getAssetCollection($collection, $additionalFilters); |
474
|
|
|
|
475
|
|
|
$path = $this->directory . $path; |
476
|
|
|
|
477
|
|
|
$this->files->makeDirectory((new \SplFileInfo($path))->getPath(), 0777, true, true); |
478
|
|
|
|
479
|
|
|
$this->files->put($path, $assets->dump()); |
480
|
|
|
|
481
|
|
|
if ($this->paths->extension($path) == 'css') { |
482
|
|
|
$this->files->put($path, app('twig')->render(str_replace($this->directory, 'assets::', $path))); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Transform an array of filters to |
488
|
|
|
* an array of Assetic filters. |
489
|
|
|
* |
490
|
|
|
* @param $filters |
491
|
|
|
* @param $hint |
492
|
|
|
* @return mixed |
493
|
|
|
*/ |
494
|
|
|
protected function transformFilters($filters, $hint) |
495
|
|
|
{ |
496
|
|
|
foreach ($filters as $k => &$filter) { |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Parse Twg tags in the asset content. |
500
|
|
|
*/ |
501
|
|
|
if ($filter == 'parse') { |
502
|
|
|
|
503
|
|
|
$filter = new ParseFilter($this->parser); |
504
|
|
|
|
505
|
|
|
continue; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Compile LESS to CSS with PHP. |
510
|
|
|
*/ |
511
|
|
View Code Duplication |
if ($filter == 'less' && $this->config->get('streams::assets.filters.less', 'php') == 'php') { |
|
|
|
|
512
|
|
|
|
513
|
|
|
$filter = new LessFilter($this->parser); |
514
|
|
|
|
515
|
|
|
continue; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Compile LESS to CSS with Node. |
520
|
|
|
*/ |
521
|
|
View Code Duplication |
if ($filter == 'less' && $this->config->get('streams::assets.filters.less', 'php') == 'node') { |
|
|
|
|
522
|
|
|
|
523
|
|
|
$filter = new NodeLessFilter($this->parser); |
524
|
|
|
|
525
|
|
|
continue; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Compile Stylus to CSS. |
530
|
|
|
*/ |
531
|
|
|
if ($filter == 'styl') { |
532
|
|
|
|
533
|
|
|
$filter = new StylusFilter($this->parser); |
534
|
|
|
|
535
|
|
|
continue; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Compile SCSS to CSS with PHP. |
540
|
|
|
*/ |
541
|
|
View Code Duplication |
if ($filter == 'scss' && $this->config->get('streams::assets.filters.scss', 'php') == 'php') { |
|
|
|
|
542
|
|
|
|
543
|
|
|
$filter = new ScssFilter($this->parser); |
544
|
|
|
|
545
|
|
|
continue; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Compile SCSS to CSS with Ruby. |
550
|
|
|
*/ |
551
|
|
View Code Duplication |
if ($filter == 'scss' && $this->config->get('streams::assets.filters.scss', 'php') == 'ruby') { |
|
|
|
|
552
|
|
|
|
553
|
|
|
$filter = new RubyScssFilter($this->parser); |
554
|
|
|
|
555
|
|
|
continue; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Compile CoffeeScript to JS |
560
|
|
|
*/ |
561
|
|
|
if ($filter == 'coffee') { |
562
|
|
|
|
563
|
|
|
$filter = new CoffeeFilter($this->parser); |
564
|
|
|
|
565
|
|
|
continue; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Look for and embed CSS images. |
570
|
|
|
*/ |
571
|
|
|
if ($filter == 'embed') { |
572
|
|
|
|
573
|
|
|
$filter = new PhpCssEmbedFilter(); |
574
|
|
|
|
575
|
|
|
continue; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Minify JS |
580
|
|
|
*/ |
581
|
|
|
if ($filter == 'min' && $hint == 'js') { |
582
|
|
|
|
583
|
|
|
$filter = new JsMinFilter(); |
584
|
|
|
|
585
|
|
|
continue; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* Minify CSS |
590
|
|
|
*/ |
591
|
|
|
if ($filter == 'min' && $hint == 'css') { |
592
|
|
|
|
593
|
|
|
$filter = new CssMinFilter(); |
594
|
|
|
|
595
|
|
|
continue; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Glob is a flag that's used later. |
600
|
|
|
*/ |
601
|
|
|
if ($filter == 'glob') { |
602
|
|
|
continue; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* No filter class could be determined! |
607
|
|
|
*/ |
608
|
|
|
$filter = null; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Be sure to separate JS concatenations. |
613
|
|
|
*/ |
614
|
|
|
if ($hint == 'js') { |
615
|
|
|
$filters[] = new SeparatorFilter(); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
return array_filter($filters); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Add filters that we can assume based |
623
|
|
|
* on the asset's file name. |
624
|
|
|
* |
625
|
|
|
* @param $file |
626
|
|
|
* @param $filters |
627
|
|
|
* @return array |
628
|
|
|
*/ |
629
|
|
|
protected function addConvenientFilters($file, $filters) |
630
|
|
|
{ |
631
|
|
|
if (ends_with($file, '.less')) { |
632
|
|
|
$filters[] = 'less'; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
if (ends_with($file, '.styl')) { |
636
|
|
|
$filters[] = 'styl'; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
if (ends_with($file, '.scss')) { |
640
|
|
|
$filters[] = 'scss'; |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
if (ends_with($file, '.coffee')) { |
644
|
|
|
$filters[] = 'coffee'; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
return array_unique($filters); |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* Decide whether we need to publish the file |
652
|
|
|
* to the path or not. |
653
|
|
|
* |
654
|
|
|
* @param $path |
655
|
|
|
* @param $collection |
656
|
|
|
* @param array $filters |
657
|
|
|
* @return bool |
658
|
|
|
*/ |
659
|
|
|
protected function shouldPublish($path, $collection, array $filters = []) |
660
|
|
|
{ |
661
|
|
|
$path = ltrim($path, '/\\'); |
662
|
|
|
|
663
|
|
|
if (starts_with($path, 'http')) { |
664
|
|
|
return false; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
if (!$this->files->exists($path)) { |
668
|
|
|
return true; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
if (in_array('force', $this->collectionFilters($collection, $filters))) { |
672
|
|
|
return true; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
$debug = $this->config->get('streams::assets.live', false); |
|
|
|
|
676
|
|
|
|
677
|
|
|
$live = in_array('live', $this->collectionFilters($collection, $filters)); |
678
|
|
|
|
679
|
|
|
if ($debug === true && $live) { |
680
|
|
|
return true; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
View Code Duplication |
if ($debug == 'public' && $live && $this->request->segment(1) !== 'admin') { |
|
|
|
|
684
|
|
|
return true; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
View Code Duplication |
if ($debug == 'admin' && $live && $this->request->segment(1) === 'admin') { |
|
|
|
|
688
|
|
|
return true; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
// Merge filters from collection files. |
692
|
|
|
foreach ($this->collections[$collection] as $fileFilters) { |
693
|
|
|
$filters = array_filter(array_unique(array_merge($filters, $fileFilters))); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
$assets = $this->getAssetCollection($collection); |
697
|
|
|
|
698
|
|
|
// If any of the files are more recent than the cache file, publish, otherwise skip |
699
|
|
|
if ($assets->getLastModified() < filemtime($path)) { |
700
|
|
|
return false; |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
return true; |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* Add a namespace path hint. |
708
|
|
|
* |
709
|
|
|
* @param $namespace |
710
|
|
|
* @param $path |
711
|
|
|
* @return $this |
712
|
|
|
*/ |
713
|
|
|
public function addPath($namespace, $path) |
714
|
|
|
{ |
715
|
|
|
$this->paths->addPath($namespace, $path); |
716
|
|
|
|
717
|
|
|
return $this; |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
/** |
721
|
|
|
* Set the public base directory. |
722
|
|
|
* |
723
|
|
|
* @param $directory |
724
|
|
|
* @return $this |
725
|
|
|
*/ |
726
|
|
|
public function setDirectory($directory) |
727
|
|
|
{ |
728
|
|
|
$this->directory = $directory; |
729
|
|
|
|
730
|
|
|
return $this; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Create asset collection from collection array |
735
|
|
|
* |
736
|
|
|
* @param $collection |
737
|
|
|
* @param array $additionalFilters |
738
|
|
|
* @return AssetCollection |
739
|
|
|
*/ |
740
|
|
|
private function getAssetCollection($collection, $additionalFilters = array()) |
741
|
|
|
{ |
742
|
|
|
$assets = new AssetCollection(); |
743
|
|
|
|
744
|
|
|
$hint = $this->paths->hint($collection); |
745
|
|
|
|
746
|
|
|
foreach ($this->collections[$collection] as $file => $filters) { |
747
|
|
|
|
748
|
|
|
$filters = array_filter(array_unique(array_merge($filters, $additionalFilters))); |
749
|
|
|
|
750
|
|
|
$filters = $this->transformFilters($filters, $hint); |
751
|
|
|
|
752
|
|
|
if (in_array('glob', $filters)) { |
753
|
|
|
|
754
|
|
|
unset($filters[array_search('glob', $filters)]); |
755
|
|
|
|
756
|
|
|
$file = new GlobAsset($file, $filters); |
757
|
|
|
} else { |
758
|
|
|
$file = new FileAsset($file, $filters); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
$assets->add($file); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
return $assets; |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* Return the filters used in a collection. |
769
|
|
|
* |
770
|
|
|
* @param $collection |
771
|
|
|
* @param array $filters |
772
|
|
|
* @return array |
773
|
|
|
*/ |
774
|
|
|
protected function collectionFilters($collection, array $filters = []) |
775
|
|
|
{ |
776
|
|
|
return array_unique( |
777
|
|
|
array_merge($filters, call_user_func_array('array_merge', array_get($this->collections, $collection, []))) |
778
|
|
|
); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* Return nothing. |
783
|
|
|
* |
784
|
|
|
* @return string |
785
|
|
|
*/ |
786
|
|
|
function __toString() |
|
|
|
|
787
|
|
|
{ |
788
|
|
|
return ''; |
789
|
|
|
} |
790
|
|
|
} |
791
|
|
|
|