|
1
|
|
|
<?php |
|
2
|
|
|
namespace AssetManager\View\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use AssetManager\Core\Resolver\ResolverInterface; |
|
5
|
|
|
use Zend\Cache\Storage\Adapter\AbstractAdapter as AbstractCacheAdapter; |
|
6
|
|
|
use Zend\View\Helper\AbstractHelper; |
|
7
|
|
|
|
|
8
|
|
|
class Asset extends AbstractHelper |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
private $config; |
|
14
|
|
|
|
|
15
|
|
|
/** @var null|AbstractCacheAdapter */ |
|
16
|
|
|
private $cache; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ResolverInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $assetManagerResolver; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param ResolverInterface $assetManagerResolver |
|
25
|
|
|
* @param AbstractCacheAdapter|null $cache |
|
26
|
|
|
* @param array $config |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function __construct(ResolverInterface $assetManagerResolver, $cache, $config) |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->assetManagerResolver = $assetManagerResolver; |
|
31
|
4 |
|
$this->cache = $cache; |
|
32
|
4 |
|
$this->config = $config; |
|
33
|
4 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Append timestamp as query param to the filename |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $filename |
|
39
|
|
|
* @param string $queryString |
|
40
|
|
|
* @param int|null $timestamp |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
2 |
|
private function appendTimestamp($filename, $queryString, $timestamp = null) |
|
45
|
|
|
{ |
|
46
|
|
|
// current timestamp as default |
|
47
|
2 |
|
$timestamp = $timestamp === null ? time() : $timestamp; |
|
48
|
|
|
|
|
49
|
2 |
|
return $filename . '?' . urlencode($queryString) . '=' . $timestamp; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* find the file and if it exists, append its unix modification time to the filename |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $filename |
|
56
|
|
|
* @param string $queryString |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
1 |
|
private function elaborateFilePath($filename, $queryString) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$asset = $this->assetManagerResolver->resolve($filename); |
|
62
|
1 |
|
if ($asset !== null) { |
|
63
|
|
|
// append last modified date to the filepath and use a custom query string |
|
64
|
1 |
|
return $this->appendTimestamp($filename, $queryString, $asset->getLastModified()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $filename; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Use the cache to get the filePath |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $filename |
|
74
|
|
|
* @param string $queryString |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed|string |
|
77
|
|
|
*/ |
|
78
|
1 |
|
private function getFilePathFromCache($filename, $queryString) |
|
79
|
|
|
{ |
|
80
|
|
|
// return if cache not found |
|
81
|
1 |
|
if ($this->cache == null) { |
|
82
|
1 |
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// cache key based on the filename |
|
86
|
|
|
$cacheKey = md5($filename); |
|
87
|
|
|
$itemIsFoundInCache = false; |
|
88
|
|
|
$filePath = $this->cache->getItem($cacheKey, $itemIsFoundInCache); |
|
89
|
|
|
|
|
90
|
|
|
// if there is no element in the cache, elaborate and cache it |
|
91
|
|
|
if ($itemIsFoundInCache === false || $filePath === null) { |
|
92
|
|
|
$filePath = $this->elaborateFilePath($filename, $queryString); |
|
93
|
|
|
$this->cache->setItem($cacheKey, $filePath); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $filePath; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Output the filepath with its unix modification time as query param |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $filename |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function __invoke($filename) |
|
106
|
|
|
{ |
|
107
|
|
|
// nothing to append |
|
108
|
3 |
|
if (empty($this->config['view_helper']['append_timestamp'])) { |
|
109
|
1 |
|
return $filename; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// search the cache config for the specific file requested (if none, use the default one) |
|
113
|
2 |
|
if (isset($this->config['caching'][$filename])) { |
|
114
|
|
|
$cacheConfig = $this->config['caching'][$filename]; |
|
115
|
2 |
|
} elseif (isset($this->config['caching']['default'])) { |
|
116
|
1 |
|
$cacheConfig = $this->config['caching']['default']; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// query string params |
|
120
|
2 |
|
$queryString = isset($this->config['view_helper']['query_string']) |
|
121
|
2 |
|
? $this->config['view_helper']['query_string'] |
|
122
|
2 |
|
: '_'; |
|
123
|
|
|
|
|
124
|
|
|
// no cache dir is defined |
|
125
|
2 |
|
if (!isset($cacheConfig['options']['dir'])) { |
|
126
|
|
|
// append current timestamp to the filepath and use a custom query string |
|
127
|
2 |
|
return $this->appendTimestamp($filename, $queryString); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// get the filePath from the cache (if available) |
|
131
|
1 |
|
$filePath = $this->getFilePathFromCache($filename, $queryString); |
|
132
|
1 |
|
if ($filePath !== null) { |
|
133
|
|
|
return $filePath; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
return $this->elaborateFilePath($filename, $queryString); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|