/
Asset.php
553 lines (500 loc) · 14.1 KB
/
Asset.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
namespace Concrete\Core\Asset;
use Concrete\Core\Filesystem\FileLocator;
use Concrete\Core\Http\Request;
use Concrete\Core\Package\Package;
use Concrete\Core\Package\PackageService;
use Concrete\Core\Routing\RouterInterface;
use Concrete\Core\Support\Facade\Application;
use Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RequestContext;
abstract class Asset implements AssetInterface
{
/**
* The handle of this asset (together with getAssetType, identifies this asset).
*
* @var string
*/
protected $assetHandle;
/**
* The position of this asset (\Concrete\Core\Asset\AssetInterface::ASSET_POSITION_HEADER or \Concrete\Core\Asset\AssetInterface::ASSET_POSITION_FOOTER).
*
* @var string
*/
protected $position;
/**
* Is this asset a locally available file (accessible with the getAssetPath method)?
*
* @var bool
*/
protected $local = true;
/**
* Does this asset support minification?
*
* @var bool
*/
protected $assetSupportsMinification = false;
/**
* Can this asset be combined with other assets?
*
* @var bool
*/
protected $assetSupportsCombination = false;
/**
* The location of the asset (used to build the path & URL).
*
* @var string
*/
protected $location;
/**
* The URL of this asset.
*
* @var string
*/
protected $assetURL;
/**
* The path to this asset.
*
* @var string
*/
protected $assetPath;
/**
* Does the URL/path have already been resolved (starting from the location) for this (local) assets?
*
* @var bool
*/
protected $assetHasBeenMapped = false;
/**
* The name of the file of this asset.
*
* @var string
*/
protected $filename;
/**
* The asset version.
*
* @var string
*/
protected $assetVersion = '0';
/**
* The package that defines this asset.
*
* @var \Concrete\Core\Package\Package|\Concrete\Core\Entity\Package|null
*/
protected $pkg;
/**
* The URL of the source files this asset has been built from (useful to understand the origin of this asset).
*
* @var array
*/
protected $combinedAssetSourceFiles = [];
/**
* Initialize the instance.
*
* @param string $assetHandle the handle of this asset (together with getAssetType, identifies this asset)
*/
public function __construct($assetHandle = '')
{
$this->assetHandle = (string) $assetHandle;
$this->position = $this->getAssetDefaultPosition();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetPosition()
*/
public function setAssetPosition($position)
{
$this->position = $position;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetPosition()
*/
public function getAssetPosition()
{
return $this->position;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetHandle()
*/
public function getAssetHandle()
{
return $this->assetHandle;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getOutputAssetType()
*/
public function getOutputAssetType()
{
return $this->getAssetType();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetIsLocal()
*/
public function setAssetIsLocal($isLocal)
{
$this->local = $isLocal;
}
/**
* {@inheritdoc}
*/
public function isAssetLocal()
{
return $this->local;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetSupportsMinification()
*/
public function setAssetSupportsMinification($minify)
{
$this->assetSupportsMinification = $minify;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::assetSupportsMinification()
*/
public function assetSupportsMinification()
{
return $this->local && $this->assetSupportsMinification;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetSupportsCombination()
*/
public function setAssetSupportsCombination($combine)
{
$this->assetSupportsCombination = $combine;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::assetSupportsCombination()
*/
public function assetSupportsCombination()
{
return $this->local && $this->assetSupportsCombination;
}
/**
* Set the location of this asset.
*
* @param string $location
*/
public function setAssetLocation($location)
{
$this->location = $location;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetURL()
*/
public function setAssetURL($url)
{
$this->assetHasBeenMapped = true;
$this->assetURL = $url;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetURL()
*/
public function getAssetURL()
{
if (!$this->assetHasBeenMapped) {
$this->mapAssetLocation($this->location);
}
$result = $this->assetURL;
if ($result && $this->isAssetLocal() && !preg_match('/\?(.*&)?' . preg_quote(static::OUTPUT_NOCACHE_PARAM, '/') . '=/', $result)) {
$config = app('config');
if ($this->pkg) {
$noCacheValue = $this->pkg->getPackageVersion();
} else {
$noCacheValue = $config->get('concrete.version_installed') . '-' . $config->get('concrete.version_db');
}
$noCacheValue .= '-' . $config->get('concrete.cache.last_cleared');
$assetVersion = $this->getAssetVersion();
$noCacheValue = !empty($assetVersion) ? $noCacheValue . '-' . $assetVersion : $noCacheValue;
$noCacheValue = $this->obfuscateNoCacheValue($noCacheValue);
$result .= (strpos($result, '?') === false ? '?' : '&') . static::OUTPUT_NOCACHE_PARAM . '=' . rawurlencode($noCacheValue);
}
return $result;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetPath()
*/
public function setAssetPath($path)
{
$this->assetHasBeenMapped = true;
$this->assetPath = $path;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetPath()
*/
public function getAssetPath()
{
if (!$this->assetHasBeenMapped) {
$this->mapAssetLocation($this->location);
}
return $this->assetPath;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::mapAssetLocation()
*/
public function mapAssetLocation($path)
{
if ($this->isAssetLocal()) {
$app = Application::getFacadeApplication();
$locator = $app->make(FileLocator::class);
if ($this->pkg) {
$locator->addLocation(new FileLocator\PackageLocation($this->pkg->getPackageHandle()));
}
$r = $locator->getRecord($path);
$this->setAssetPath($r->file);
$this->setAssetURL($r->url);
} else {
$this->setAssetURL($path);
}
}
/**
* Does the URL/path have already been resolved (starting from the location) for this (local) assets?
*
* @return bool
*/
public function hasAssetBeenMapped()
{
return $this->assetHasBeenMapped;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetURLPath()
*/
public function getAssetURLPath()
{
return substr($this->getAssetURL(), 0, strrpos($this->getAssetURL(), '/'));
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetFilename()
*/
public function getAssetFilename()
{
return $this->filename;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetHashKey()
*/
public function getAssetHashKey()
{
$result = $this->assetURL;
if ($this->isAssetLocal()) {
$filename = $this->getAssetPath();
if (is_file($filename)) {
$useFileContents = false;
if (is_readable($filename)) {
$app = Application::getFacadeApplication();
$config = $app->make('config');
if ($config->get('concrete.cache.full_contents_assets_hash')) {
$useFileContents = true;
}
}
if ($useFileContents) {
$sha1 = @sha1_file($filename);
if ($sha1 !== false) {
$result = $sha1;
}
} else {
$mtime = @filemtime($filename);
if ($mtime !== false) {
$result .= '@' . $mtime;
}
}
}
}
return $result;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setAssetVersion()
*/
public function setAssetVersion($version)
{
$this->assetVersion = $version;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetVersion()
*/
public function getAssetVersion()
{
return $this->assetVersion;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setPackageObject()
*/
public function setPackageObject($pkg)
{
$this->pkg = $pkg;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::getAssetContents()
*/
public function getAssetContents()
{
$result = @file_get_contents($this->getAssetPath());
return ($result === false) ? null : $result;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::setCombinedAssetSourceFiles()
*/
public function setCombinedAssetSourceFiles($paths)
{
$this->combinedAssetSourceFiles = $paths;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::register()
*/
public function register($filename, $args, $pkg = false)
{
$args += [
'local' => false,
'minify' => null,
'combine' => null,
'version' => null,
'position' => null,
];
if ($pkg) {
if (is_string($pkg)) {
$app = Application::getFacadeApplication();
$pkg = $app->make(PackageService::class)->getByHandle($pkg);
}
$this->setPackageObject($pkg ?: null);
}
$this->setAssetIsLocal($args['local']);
$this->setAssetLocation($filename);
if ($args['minify'] === true || $args['minify'] === false) {
$this->setAssetSupportsMinification($args['minify']);
}
if ($args['combine'] === true || $args['combine'] === false) {
$this->setAssetSupportsCombination($args['combine']);
}
if ($args['version']) {
$this->setAssetVersion($args['version']);
}
if ($args['position']) {
$this->setAssetPosition($args['position']);
}
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Asset\AssetInterface::process()
*/
public static function process($assets)
{
return $assets;
}
/**
* Get an AssetPointer instance that identifies this asset.
*
* @return \Concrete\Core\Asset\AssetPointer
*/
public function getAssetPointer()
{
$pointer = new AssetPointer($this->getAssetType(), $this->getAssetHandle());
return $pointer;
}
/**
* Get the contents of an asset given its route.
*
* @param string $route
*
* @return string|null
*/
protected static function getAssetContentsByRoute($route)
{
$result = null;
try {
$app = Application::getFacadeApplication();
$router = $app->make(RouterInterface::class);
$context = new RequestContext();
$context->fromRequest($app->make(Request::class));
$context->setMethod('GET');
$matched = $router->getRouteByPath($route, $context);
$controller = $matched->getAction();
$callable = null;
if (is_string($controller)) {
$chunks = explode('::', $controller, 2);
if (count($chunks) === 2) {
if (class_exists($chunks[0])) {
$array = [$app->make($chunks[0]), $chunks[1]];
if (is_callable($array)) {
$callable = $array;
}
}
} else {
if (class_exists($controller) && method_exists($controller, '__invoke')) {
$callable = $app->make($controller);
}
}
} elseif (is_callable($controller)) {
$callable = $controller;
}
if ($callable !== null) {
ob_start();
$r = call_user_func($callable, false);
if ($r instanceof Response) {
$result = $r->getContent();
} elseif ($r !== false) {
$result = ob_get_contents();
}
ob_end_clean();
}
} catch (Exception $x) {
}
return $result;
}
/**
* Obfuscate the value of the query-string parameter used to avoid browser cache problems.
*
* @param string $noCacheValue
*
* @return string
*/
protected function obfuscateNoCacheValue($noCacheValue)
{
return sha1($noCacheValue);
}
}