1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Extension; |
4
|
|
|
|
5
|
|
|
use League\Plates\Engine; |
6
|
|
|
use League\Plates\Template\Template; |
7
|
|
|
use LogicException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Extension that adds the ability to create "cache busted" asset URLs. |
11
|
|
|
*/ |
12
|
|
|
class Asset implements ExtensionInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Instance of the current template. |
16
|
|
|
* @var Template |
17
|
|
|
*/ |
18
|
|
|
public $template; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Path to asset directory. |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Enables the filename method. |
28
|
|
|
* @var boolean |
29
|
|
|
*/ |
30
|
|
|
public $filenameMethod; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Enables hashing for last updated value in filename. |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
public $hashLastUpdatedValue; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create new Asset instance. |
40
|
|
|
* @param string $path |
41
|
|
|
* @param boolean $filenameMethod |
42
|
|
|
* @param boolean $hashLastUpdatedValue |
43
|
|
|
*/ |
44
|
16 |
|
public function __construct($path, $filenameMethod = false, $hashLastUpdatedValue = false) |
45
|
|
|
{ |
46
|
16 |
|
$this->path = rtrim($path, '/'); |
47
|
16 |
|
$this->filenameMethod = $filenameMethod; |
48
|
16 |
|
$this->hashLastUpdatedValue = $hashLastUpdatedValue; |
49
|
16 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Register extension function. |
53
|
|
|
* @param Engine $engine |
54
|
|
|
* @return null |
55
|
|
|
*/ |
56
|
4 |
|
public function register(Engine $engine) |
57
|
|
|
{ |
58
|
4 |
|
$engine->registerFunction('asset', array($this, 'cachedAssetUrl')); |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create "cache busted" asset URL. |
63
|
|
|
* @param string $url |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
10 |
|
public function cachedAssetUrl($url) |
67
|
|
|
{ |
68
|
10 |
|
$filePath = $this->path . '/' . ltrim($url, '/'); |
69
|
|
|
|
70
|
10 |
|
if (!file_exists($filePath)) { |
71
|
2 |
|
throw new LogicException( |
72
|
2 |
|
'Unable to locate the asset "' . $url . '" in the "' . $this->path . '" directory.' |
73
|
2 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
$lastUpdated = filemtime($filePath); |
77
|
8 |
|
$pathInfo = pathinfo($url); |
78
|
|
|
|
79
|
8 |
|
if ($this->hashLastUpdatedValue) { |
80
|
2 |
|
$lastUpdated = hash('crc32b', $lastUpdated); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
8 |
|
if ($pathInfo['dirname'] === '.') { |
84
|
6 |
|
$directory = ''; |
85
|
8 |
|
} elseif ($pathInfo['dirname'] === '/') { |
86
|
2 |
|
$directory = '/'; |
87
|
2 |
|
} else { |
88
|
2 |
|
$directory = $pathInfo['dirname'] . '/'; |
89
|
|
|
} |
90
|
|
|
|
91
|
8 |
|
if ($this->filenameMethod) { |
92
|
2 |
|
return $directory . $pathInfo['filename'] . '.' . $lastUpdated . '.' . $pathInfo['extension']; |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
return $directory . $pathInfo['filename'] . '.' . $pathInfo['extension'] . '?v=' . $lastUpdated; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|