forked from albulescu/zf2-module-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetsManager.php
309 lines (227 loc) · 6.76 KB
/
AssetsManager.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
<?php
/**
*
* Albulescu Cosmin ( http://www.albulescu.com/ )
*
* @link http://www.albulescu.com/
* @copyright Copyright (c) 2012 Albulescu Cosmin. (http://www.albulescu.com)
* @license http://www.albulescu.ro/new-bsd New BSD License
* @autor Albulescu Cosmin <cosmin@albulescu.ro>
*
*/
namespace Assets;
use Zend\Mvc\Router\Http\Segment;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use Assets\Filter\FilterInterface;
use Zend\Cache\Storage\Adapter\AbstractAdapter;
use Zend\Server\Cache;
use Zend\Http\Header\Connection;
use Zend\Http\Headers;
use Zend\Http\PhpEnvironment\Response;
class AssetsManager {
/**
* The asset resolver class
* @var AssetsResolverInterface
*/
protected $resolver;
/**
* Enter description here ...
* @var AbstractAdapter
*/
protected $cache;
/**
* Keep the array with filters for each asset type
* @var array
*/
protected $filters = array();
/**
* @var AssetsOptions
*/
protected $options;
/**
* @var ServiceManager
*/
protected $serviceLocator;
public function __construct( $options, $serviceLocator )
{
$this->serviceLocator = $serviceLocator;
if($options) {
$this->setOptions($options);
}
}
/**
* @param AssetsOptions $options
* @throws \RuntimeException
*/
public function setOptions($options) {
if(!($options instanceof AssetsOptions) && ! ArrayUtils::hasStringKeys($options)) {
throw new \RuntimeException("Options must be an array with string keys");
}
if(is_array($options)) {
$options = new AssetsOptions($options);
}
//Set the controller resolver
$controllerLoader = $this->serviceLocator->get("ControllerLoader");
$controllerLoader->setInvokableClass("AssetsController", "Assets\AssetsController");
//Set the route
$router = $this->serviceLocator->get('Router');
$route = new Segment($options->getRoute(),array('asset'=>'.*'), array(
'controller'=>'AssetsController',
'action'=>'index'
));
$router->addRoute("assets", $route);
$filterManager = $this->serviceLocator->get('AssetsFilterManager');
foreach( $options->getFilters() as $asset => $filters ) {
foreach($filters as $filter) {
if(!$filterManager->has($filter)) {
throw new \RuntimeException("Filter $filter does not exist");
}
if(!isset($this->filters[$asset])) {
$this->filters[$asset] = array();
}
$this->filters[$asset][] = $filterManager->get($filter);
}
}
foreach($options->getPaths() as $path) {
$this->getResolver()->addPath($path);
}
}
/**
* Set the asset resolver
* @param AssetsResolverInterface $resolver
* @throws RuntimeException
*/
public function setResolver($resolver) {
if(!$resolver instanceof AssetsResolverInterface) {
throw new \RuntimeException("The resolver must implement the AssetsResolverInterface");
}
$this->resolver = $resolver;
}
/**
* Enter description here ...
* @return AssetsResolverInterface
*/
public function getResolver() {
if(!$this->resolver) {
$this->resolver = new AssetsResolver();
}
return $this->resolver;
}
/**
* @param string $request Request uri
* @throws \RuntimeException
* @return \Zend\Http\PhpEnvironment\Response
*/
public function dispatch( $request ) {
if(!$this->getResolver()) {
throw new \RuntimeException("No resolver setted");
}
$asset = $this->resolver->resolve( $request );
$content = null;
$responseCode = 404;
$headers = Headers::fromString("Content-Type: text/plain");
if($asset)
{
$headers = $this->getHeaders($asset->getFile(), $asset->getMime());
if( $this->browserCached($asset->getFile()) ) {
$responseCode = 304;
$headers->addHeader( Connection::fromString("Connection: close") );
}
else
{
$responseCode = 200;
$cacheKey = "assets-cache-" . md5($request);
if( $this->cache ) {
$content = $this->cache->getItem($cacheKey);
}
if( ! $content ) {
$content = $asset->getContent();
$assetName = end(explode('\\',get_class($asset)));
if(array_key_exists($assetName, $this->filters))
{
foreach( $this->filters[$assetName] as $filter )
{
$content = $filter->filter($content);
}
}
if($this->cache) {
$this->cache->addItem($cacheKey, $content);
}
}
}
}
else {
$content = "Asset not found!";
}
$response = new Response();
$response->setStatusCode( $responseCode );
$response->setContent( $content );
$response->setHeaders( $headers );
return $response;
}
/**
* Add fiter
* @param string $forAssetClass
* @param FilterInterface $filter
*/
public function addFilter($forAssetClass, $filter ) {
if(!$filter instanceof FilterInterface) {
throw new RuntimeException("Filter must be an instance of FilterInterface");
}
if(!isset($this->filters[$forAssetClass])) {
$this->filters[$forAssetClass] = array();
}
$this->filters[$forAssetClass][] = $filter;
}
/**
* @param $cache
*/
public function setCache($cache) {
//TODO: Cache
}
/**
* Test if file is cached
*
* @param string $file
* @return boolean
*/
protected function browserCached( $file ) {
$info = new \SplFileInfo($file);
$filetime = $info->getMTime();
unset($info);
$last_modified = date('D, d M Y H:i:s', $filetime) . ' GMT';
$PageWasUpdated = !(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) and
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $filetime);
$DoIDsMatch = (isset($_SERVER['HTTP_IF_NONE_MATCH']) and
@ereg(md5($last_modified), $_SERVER['HTTP_IF_NONE_MATCH']));
if (!$PageWasUpdated || $DoIDsMatch)
{
return true;
}
return false;
}
/**
* Get headers for caching file and type
*
* @param string $mime File mimetype
* @return \Zend\Http\Headers
*/
protected function getHeaders($file, $mime ) {
$cache_days = 20;
$info = new \SplFileInfo($file);
$filetime = $info->getMTime();
unset($info);
$last_modified = date('D, d M Y H:i:s', $filetime) . ' GMT';
$expires = date('D, d M Y H:i:s', strtotime('+30 days',$filetime)) . ' GMT';
$hasid = md5($last_modified);
$headers = "Cache-Control: max-age=".($cache_days * 24 * 60 * 60) . "\r\n" .
"Pragma: private\r\n" .
"Last-Modified: ".$last_modified . "\r\n" .
"Expires: ".$expires . "\r\n" .
"Vary: Accept-Encoding\r\n" .
"Etag: ".$hasid . "\r\n" .
"Content-Type: " . $mime . "\r\n";
return Headers::fromString($headers);
}
}