/
Type.php
317 lines (275 loc) · 8.29 KB
/
Type.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
<?php
/**
* Munee: Optimising Your Assets
*
* @copyright Cody Lundquist 2013
* @license http://opensource.org/licenses/mit-license.php
*/
namespace Munee\Asset;
use Munee\ErrorException;
use Munee\Request;
use Munee\Response;
use Munee\Utils;
use Munee\Asset\NotFoundException;
/**
* Base Asset Class
* All Asset Types need to extend this and implement the getHeaders() method.
*
* @author Cody Lundquist
*/
abstract class Type
{
/**
* Stores the Request Options for the Asset Type
*
* @var array
*/
protected $options = array();
/**
* Stores the list of filters that will be applied to the requested asset.
*
* @var array
*/
protected $filters = array();
/**
* Stores the path to the cache directory
*
* @var string
*/
protected $cacheDir;
/**
* Stores the last modified date (Epoch) for the requested asset
*
* @var integer
*/
protected $lastModifiedDate = 0;
/**
* Stores the content of the asset
*
* @var string
*/
protected $content;
/**
* Reference to the \Munee\Request class
*
* @var \Munee\Request
*/
protected $request;
/**
* Reference to the \Munee\Response class
*
* @var \Munee\Response
*/
protected $response;
/**
* All Type Sub-Classes must create this method to set their additional headers
*/
abstract public function getHeaders();
/**
* Constructor
*
* @param \Munee\Request $Request
*
* @throws NotFoundException
*/
public function __construct(Request $Request)
{
$this->request = $Request;
// Pull in filters based on the raw params that were passed in
$rawParams = $Request->getRawParams();
$assetShortName = preg_replace('%^.*\\\\%','', get_class($this));
$allowedParams = array();
foreach (array_keys($rawParams) as $filterName) {
$filterClass = 'Munee\\Asset\\Filter\\' . $assetShortName . '\\' . ucfirst($filterName);
if (class_exists($filterClass)) {
$Filter = new $filterClass();
$allowedParams += $Filter->getAllowedParams();
$this->filters[$filterName] = $Filter;
}
}
// Parse the raw params based on a map of allowedParams for those filters
$this->request->parseParams($allowedParams);
$this->cacheDir = MUNEE_CACHE . DS . $assetShortName;
$optionsKey = strtolower($assetShortName);
// Set the AssetType options if someone were passed in through the Request Class
if (isset($this->request->options[$optionsKey])) {
$this->options = array_merge(
$this->options,
$this->request->options[$optionsKey]
);
}
// Create cache dir if needed
Utils::createDir($this->cacheDir);
}
/**
* Process all files in the request and set the content
*/
public function init()
{
$content = array();
foreach ($this->request->files as $file) {
$cacheFile = $this->generateCacheFile($file);
if (! $fileContent = $this->checkCache($file, $cacheFile)) {
$this->setupFile($file, $cacheFile);
$fileContent = $this->getFileContent($file, $cacheFile);
}
$content[] = $this->afterGetFileContent($fileContent);
}
$this->content = implode("\n", $content);
}
/**
* Sets the Munee\Response class to the AssetType
*
* @param $Response
*
* @throws \Munee\ErrorException
*/
public function setResponse($Response)
{
if (! $Response instanceof Response) {
throw new ErrorException('Response class must be an instance of Munee\Response');
}
$this->response = $Response;
}
/**
* Grabs the content for the Response class
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Return a this requests Last Modified Date.
*
* @return integer timestamp
*/
public function getLastModifiedDate()
{
return $this->lastModifiedDate;
}
/**
* If an exception is handled this function will fire and clean up any files
* that have been cached as they have not properly compiled.
*/
public function cleanUpAfterError()
{
foreach ($this->request->files as $file) {
$cacheFile = $this->generateCacheFile($file);
unlink($cacheFile);
}
}
/**
* Callback method called before filters are run
*
* @param string $originalFile
* @param string $cacheFile
*/
protected function beforeFilter($originalFile, $cacheFile) {}
/**
* Callback function called after filters are run
*
* @param string $originalFile
* @param string $cacheFile
*/
protected function afterFilter($originalFile, $cacheFile) {}
/**
* Callback function called after _getFileContent() is called
*
* @param string $content
*
* @return string
*/
protected function afterGetFileContent($content)
{
return $content;
}
/**
* Checks to see if the file exists and then copies it to the cache folder for further manipulation
*
* @param $originalFile
* @param $cacheFile
*
* @throws NotFoundException
*/
protected function setupFile($originalFile, $cacheFile)
{
// Check if the file exists
if (! file_exists($originalFile)) {
throw new NotFoundException('File does not exist: ' . str_replace($this->request->webroot, '', $originalFile));
}
// Copy the original file to the cache location
copy($originalFile, $cacheFile);
}
/**
* Grab a files content but check to make sure it exists first
*
* @param string $originalFile
* @param string $cacheFile
*
* @return string
*
* @throws NotFoundException
*/
protected function getFileContent($originalFile, $cacheFile)
{
$this->beforeFilter($originalFile, $cacheFile);
// Run through each filter
foreach ($this->filters as $filterName => $Filter) {
$arguments = isset($this->request->params[$filterName]) ?
$this->request->params[$filterName] : array();
if (! is_array($arguments)) {
$arguments = array($filterName => $arguments);
}
// Do not minify if .min. is in the filename as it has already been minified
if(strpos($originalFile, '.min.') !== FALSE) {
$arguments['minify'] = false;
}
$Filter->doFilter($cacheFile, $arguments, $this->options);
}
$this->afterFilter($originalFile, $cacheFile);
$this->lastModifiedDate = time();
return file_get_contents($cacheFile);
}
/**
* Checks to see if cache exists and is the latest, if it does, return it
*
* @param string $originalFile
* @param string $cacheFile
*
* @return bool|string
*/
protected function checkCache($originalFile, $cacheFile)
{
if (! file_exists($cacheFile) || ! file_exists($originalFile)) {
return false;
}
$cacheFileLastModified = filemtime($cacheFile);
if (filemtime($originalFile) > $cacheFileLastModified) {
return false;
}
if ($this->lastModifiedDate < $cacheFileLastModified) {
$this->lastModifiedDate = $cacheFileLastModified;
}
return file_get_contents($cacheFile);
}
/**
* Generate File Name Hash based on filename, request params and request options
*
* @param string $file
*
* @return string
*/
protected function generateCacheFile($file)
{
$requestOptions = serialize($this->request->options);
$params = serialize($this->request->params);
$ext = pathinfo($file, PATHINFO_EXTENSION);
$fileHash = md5($file);
$optionsHash = md5($params . $requestOptions);
$cacheDir = $this->cacheDir . DS . substr($fileHash, 0, 2);
Utils::createDir($cacheDir);
return $cacheDir . DS . substr($fileHash, 2) . '-' . $optionsHash . '.' . $ext;
}
}