-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractPacker.php
More file actions
360 lines (318 loc) · 7.62 KB
/
AbstractPacker.php
File metadata and controls
360 lines (318 loc) · 7.62 KB
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
<?php
/**
* @package SugiPHP
* @subpackage Assets
* @author Plamen Popov <tzappa@gmail.com>
* @license http://opensource.org/licenses/mit-license.php (MIT License)
*/
namespace SugiPHP\Assets;
abstract class AbstractPacker
{
/**
* Adds asset
*
* @param string $asset The file can be absolute path or relative to the input_path
*/
abstract protected function addAsset($asset);
abstract protected function dumpAssets();
/**
* Configuration settings.
*
* @var array
*/
protected $config = array();
/**
* List of loaded assets.
*
* @var array
*/
protected $assets = array();
/**
* Most resent modified time from all added assets.
*
* @var integer
*/
protected $lastModified = 0;
/**
* File Name Template. Asterix (*) will be replaced with
* unique name based on all assets added, configuration settings
* and last modification time.
*
* @var string
*/
protected $filenameTemplate = "*";
/**
* Asset Packer Constructor
*
* @param array $config
* - output_path - the directory where cached files will be created.
* This should be within your DOCUMENT ROOT and be visible from web.
* The server must have write permissions for this path.
* - input_path - the directory where actual uncompressed files are. This can be anywhere in the server.
* - debug - minifications are not done when debug is TRUE; default is FALSE;
*/
public function __construct($config)
{
$this->setInputPath($config["input_path"]);
$this->setOutputPath($config["output_path"]);
if (isset($config["file_name"])) {
$this->setFilename($config["file_name"]);
}
$this->setDebug(isset($config["debug"]) ? $config["debug"] : false);
}
/**
* Returns input path(s).
*
* @return array
*/
public function getInputPath()
{
return $this->config["input_path"];
}
/**
* Sets default input path or an array with paths.
*
* @param string|array $path
*/
public function setInputPath($path)
{
if (is_array($path)) {
$this->config["input_path"] = array_map(array($this, "preparePath"), $path);
} else {
$this->config["input_path"] = (array) $this->preparePath($path);
}
}
/**
* Adds a path on the end of the input path array
*
* @param string $path Input search path
*/
public function addInputPath($path)
{
$this->config["input_path"][] = $this->preparePath($path);
}
/**
* Removes last search input path.
*/
public function popInputPath()
{
array_pop($this->config["input_path"]);
}
/**
* Appends a path to the beginning of the input path array.
*
* @param string $path Input search path
*/
public function prependInputPath($path)
{
array_unshift($this->config["input_path"], $this->preparePath($path));
}
/**
* Gets the first path of the array and removes it from the input paths.
*/
public function shiftInputPath()
{
array_shift($this->config["input_path"]);
}
/**
* Returns default output path.
*
* @return array
*/
public function getOutputPath()
{
return $this->config["output_path"];
}
/**
* Sets default output path.
*
* @param string $path
*/
public function setOutputPath($path)
{
$this->config["output_path"] = $this->preparePath($path);
}
/**
* Sets output filename template.
*
* @param string $filenameTemplate
*/
public function setFilename($filenameTemplate)
{
$this->filenameTemplate = $filenameTemplate;
}
/**
* Returns filename template.
*
* @return string
*/
public function getFilenameTemplate()
{
return $this->filenameTemplate;
}
/**
* Returns unique output file name (if * exists in the filename template).
*
* @return string
*/
public function getFileName()
{
if (strpos($this->filenameTemplate, "*") === false) {
return $this->filenameTemplate;
}
$serial = serialize($this->config) . serialize($this->assets) . serialize($this->lastModified);
$filename = substr(sha1($serial), 0, 11);
return str_replace("*", $filename, $this->filenameTemplate);
}
/**
* Returns debug configuration option.
*
* @return bool
*/
public function getDebug()
{
return $this->config["debug"];
}
/**
* Sets debug configuration option. If this is set some minifications
* will not be done to ease debugging in development environment.
*
* @param boolean $debug
*/
public function setDebug($debug)
{
$this->config["debug"] = $debug;
}
/**
* Add asset(s) file(s)
*
* @param string|array $assets List of files or a single file. The file can be
* absolute path or relative to the input_path
*/
public function add($assets)
{
$this->addAssetsArray((array) $assets, false);
}
/**
* Add asset(s) file(s) only if they were not added to the list.
*
* @param string|array $assets List of files or a single file. The file can be
* absolute path or relative to the input_path
*/
public function addOnce($assets)
{
$this->addAssetsArray((array) $assets, true);
}
/**
* @param array $assets
* @param boolean $addOnce
*/
protected function addAssetsArray(array $assets, $addOnce)
{
// Check one by one and throw an exception if the file is not found
foreach ($assets as $asset) {
// prepend search path?
if (!$this->isAbsolutePath($asset)) {
// Stack of paths
if (is_array($this->config["input_path"])) {
$assetFound = false;
foreach ($this->config["input_path"] as $input_path) {
$fileName = $input_path . $asset;
if (file_exists($fileName)) {
$this->addSingleAsset($fileName, $addOnce);
$assetFound = true;
break;
}
}
// Throws an exception if an asset is not found
if (!$assetFound) {
throw new \Exception("Could not find $asset");
}
} else { //single path
$this->addSingleAsset($this->config["input_path"] . $asset, $addOnce);
}
} else {
$this->addSingleAsset($asset, $addOnce);
}
}
}
protected function addSingleAsset($asset, $addOnce)
{
// Check the file and gets last modified date
if (!$mtime = @filemtime($asset)) {
throw new \Exception("Could not stat $asset");
}
if ($addOnce and in_array($asset, $this->assets)) {
return;
}
// make some custom stuff
$this->addAsset($asset);
// add it in the list
$this->assets[] = $asset;
// last modified time
$this->lastModified = max($mtime, $this->lastModified);
}
/**
* Returns list of all added assets.
*
* @return array
*/
public function getAssets()
{
return $this->assets;
}
/**
* Dumps all assets files as one packed and minified version.
*
* @return string
*/
public function dump()
{
$filename = $this->getFileName();
$path = $this->config["output_path"].$filename;
return file_exists($path) ? file_get_contents($path) : $this->dumpAssets();
}
/**
* Saves packed and minified assets in a file with unique name.
* Returns the filename without the path.
*
* @return string Unique filename of the saved file
*/
public function pack()
{
$filename = $this->getFileName();
$path = $this->config["output_path"].$filename;
if (!file_exists($path)) {
file_put_contents($path, $this->dumpAssets());
}
return $filename;
}
/**
* Prepares an input path.
*
* @param string $path A path to an input directory
* @return string The prepared path
*/
protected function preparePath($path)
{
return rtrim($path, "/\\") . DIRECTORY_SEPARATOR;
}
/**
* Check if the file is given with absolute path.
*
* @param string $path
* @return bool
*/
protected function isAbsolutePath($path)
{
// *nix style
if ($path[0] == "/") {
return true;
}
// windows
if ((strlen($path) > 3) and ctype_alpha($path[0]) and ($path[1] == ":") and ($path[2] = "\\")) {
return true;
}
return false;
}
}