-
Notifications
You must be signed in to change notification settings - Fork 3
/
PackageCompressor.php
615 lines (521 loc) · 20.5 KB
/
PackageCompressor.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* PackageCompressor
*
* A Javascript and CSS compressor based on Yii's package system.
*
* @author Michael Härtl <[email protected]>
* @version 1.0.4
*/
Yii::setPathOfAlias('_packagecompressor', dirname(__FILE__));
class PackageCompressor extends CClientScript
{
/**
* @var bool wether to enable package compression
*/
public $enableCompression = true;
/**
* @var bool wether to only combine, not compress (useful for debugging)
*/
public $combineOnly = false;
/**
* If this is enabled, during compression all other requests will wait until the compressing
* process has completed. If disabled, the uncompressed files will be delivered for these
* requests. This should prevent the thundering herd problem.
*
* @var bool wether other requests should pause during compression. On by default.
*/
public $blockDuringCompression = true;
/**
* @var bool whether to add a fingerprint to CSS image urls.
*/
public $enableCssImageFingerPrinting = false;
/**
* @var string name or path of/to the JAVA executable. Default is 'java'.
*/
public $javaBin = 'java';
/**
* @var array names of packages that where registered on current page
*/
private $_registeredPackages = array();
/**
* @var mixed meta information about the compressed packages
*/
private $_pd;
/**
* @var mixed the EMutex component
*/
private $_mutex;
// Locking parameter to prevent parallel compression
const LOCK_ID = '_PackageCompressor';
const LOCK_TIMEOUT = 15;
// YUI compressor jar name
const YUI_COMPRESSOR_JAR = 'yuicompressor-2.4.7.jar';
/**
* Used internally: Create a compressed version of all package files, publish the
* compressed file through asset manager and store compressedInfo. Js and CSS will
* be processed independently.
*
* @param string $name of package
*/
public function compressPackage($name)
{
// Backup registered scripts, css and core scripts, as we only want to
// catch the files contained in the package, not those registered from elsewhere
$coreScripts = $this->coreScripts;
$scriptFiles = $this->scriptFiles;
$cssFiles = $this->cssFiles;
$this->coreScripts = $this->cssFiles = $this->scriptFiles = array();
// Now we register only the package and let yii resolve dependencies
// and expand coreScripts into scriptFiles (usually happens during rendering)
$this->registerCoreScript($name);
$this->renderCoreScripts();
// Copied from CClientScript: process the scriptMap and remove duplicates
if(!empty($this->scriptMap))
$this->remapScripts();
$this->unifyScripts();
$info = array();
$am = Yii::app()->assetManager;
$basePath = Yii::getPathOfAlias('webroot');
// /www/root/sub -> /www/root (baseUrl=/sub)
if(($baseUrl = Yii::app()->request->baseUrl)!=='')
$basePath = substr($basePath,0,-strlen($baseUrl));
// Process all JS files from the package (if any)
if(isset($this->scriptFiles[$this->coreScriptPosition]))
{
$scripts = array();
$urls = array();
foreach($this->scriptFiles[$this->coreScriptPosition] as $script)
if (strtolower(substr($script,0,4))==='http' || substr($script,0,2)==='//') // Exclude external scripts
$urls[] = $script;
else
$scripts[] = $basePath.$script; // '/www/root'.'/sub/js/some.js'
if ($scripts!==array())
{
$fileName = $this->compressFiles($name,'js',$scripts);
$urls[] = $am->publish($fileName,true); // URL to compressed file
// Remove current package name from meta data
$core = array_keys($this->coreScripts);
if(($key = array_search($name,$core))!==false)
unset($core[$key]);
$info['js'] = array(
'file' => $am->getPublishedPath($fileName,true), // path to compressed file
'files' => $scripts,
'urls' => $urls,
'coreScripts' => $core,
);
unlink($fileName);
}
}
// Process all CSS files from the package (if any)
if ($this->cssFiles!==array())
{
$files = array();
$urls = array();
foreach(array_keys($this->cssFiles) as $file)
$files[] = $basePath.$file;
$fileName = $this->compressFiles($name,'css',$files);
if(isset($this->packages[$name]['baseUrl']))
{
// If a CSS package uses 'baseUrl' we do not use the asset publisher
// because this could break CSS URLs. Instead we copy to baseUrl:
$url = '/'.trim($this->packages[$name]['baseUrl'],'/').'/'.basename($fileName);
// '/www/root'.'/sub/'.'/css/some.css'
$destFile = $basePath.$baseUrl.$url;
copy($fileName, $destFile);
$urls[] = $baseUrl.$url; // '/sub'.'/css/some.css'
}
else
{
$urls[] = $am->publish($fileName,true); // URL to compressed file
$destFile = $am->getPublishedPath($fileName,true);
}
$info['css'] = array(
'file' => $destFile,
'files' => $files,
'urls' => $urls,
'media' => isset($this->packages[$name]['media']) ? $this->packages[$name]['media'] : '',
);
unlink($fileName);
}
// Store package meta info
if($info!==array())
$this->setCompressedInfo($name,$info);
// Restore original coreScripts, scriptFiles and cssFiles
$this->coreScripts = $coreScripts;
$this->scriptFiles = $scriptFiles;
$this->cssFiles = $cssFiles;
}
/**
* Override CClientScript::registerPackage() to initialize the compression algorithm
*
* @param string $name Name of Package to register
* @return CClientScript the CClientScript object itself
*/
public function registerPackage($name)
{
if ($this->enableCompression && !in_array($name,$this->_registeredPackages))
{
$this->hasScripts = true;
$this->_registeredPackages[$name] = $name;
// Create compressed package if not done so yet
if(($info = $this->getCompressedInfo($name))===null)
{
$mutex = $this->getMutex();
// Compresssion must only be performed once, even for several parallel requests
while(!$mutex->lock(self::LOCK_ID,self::LOCK_TIMEOUT))
if($this->blockDuringCompression)
sleep(1);
else
return parent::registerPackage($name);
// We have a Mutex lock, now check if another process already compressed this package
if ($this->getCompressedInfo($name,true)!==null) {
$mutex->unlock();
return $this;
}
$this->compressPackage($name);
$mutex->unlock();
}
return $this;
} else
return parent::registerPackage($name);
}
/**
* Override CClientScript::render() to add compressed package files if available.
*
* @param string $output the existing output that needs to be inserted with script tags
*/
public function render(&$output)
{
if(!$this->hasScripts)
return;
$packages = $this->_registeredPackages;
if($this->enableCompression)
foreach($packages as $package)
$this->unregisterPackagedCoreScripts($package);
$this->renderCoreScripts();
if(!empty($this->scriptMap))
$this->remapScripts();
// Register package files as *first* files always
if($this->enableCompression)
foreach(array_reverse($this->_registeredPackages) as $package)
$this->renderCompressedPackage($package);
$this->unifyScripts();
$this->renderHead($output);
if($this->enableJavaScript)
{
$this->renderBodyBegin($output);
$this->renderBodyEnd($output);
}
}
/**
* Delete cached package file
*
* @param string (optional) $name of package
*/
public function resetCompressedPackage($name=null)
{
if($this->_pd===null)
$this->loadPackageData();
if($name===null)
$packages = $this->_pd;
elseif(isset($this->_pd[$name]))
$packages = array($name=>$this->_pd[$name]);
else
$packages = array();
if($packages===array())
return false;
foreach($packages as $package => $info)
{
if(isset($info['js']['file']))
@unlink($info['js']['file']);
if(isset($info['css']['file']))
@unlink($info['css']['file']);
unset($this->_pd[$package]);
}
$this->savePackageData();
return true;
}
/**
* If a compressed package is available, will return an array of this format:
*
* array(
* 'js'=>array(
* 'file' =>'/path/to/compressed/file',
* 'files' => <list of original file names>
* 'urls' => <list of script URLs (incl. external)>
* 'coreScripts' => <list of core scripts contained in this package>
* ),
* 'css'=>array(
* 'file' =>'/path/to/compressed/file',
* 'urls' => <list of script URLs (incl. external)>
* ),
*
* @param string name of package to load
* @param bool wether to enforce that package data is read again from global state
* @return mixed array with compressed package information or null if none
*/
public function getCompressedInfo($name,$forceRefresh=false)
{
if ($this->_pd===null || $forceRefresh)
$this->loadPackageData($forceRefresh);
$i=isset($this->_pd[$name]) ? $this->_pd[$name] : null;
// Safety check: Verify that compressed files exist
if( isset($i['js']['file']) && !file_exists($i['js']['file']) ||
isset($i['css']['file']) && !file_exists($i['css']['file']))
{
$this->setCompressedInfo($name,null);
YII_DEBUG && Yii::trace(
sprintf(
"Remove %s:\n%s\n%s",
$name,
isset($i['js']) ? $i['js']['file'] : '-',
isset($i['css']) ? $i['css']['file'] : '-'
),
'application.components.packagecompressor'
);
$i=null;
}
return $i;
}
/**
* @return array list of compressed package names
*/
public function getCompressedPackageNames()
{
if ($this->_pd===null)
$this->loadPackageData();
return array_keys($this->_pd);
}
/**
* @return EMutex the mutex component from the bundled EMutext extension
*/
public function getMutex()
{
if($this->_mutex===null)
$this->_mutex = Yii::createComponent(array(
'class' => '_packagecompressor.EMutex',
'mutexFile' => Yii::app()->runtimePath.'/packagecompressor_mutex.bin',
));
return $this->_mutex;
}
/**
* @return string unique key per application
*/
public function getStateKey()
{
return '__packageCompressor:'.Yii::app()->getId();
}
/**
* Combine the set of given text files into one file
*
* For js files, we add a semicolon to the end, in case the file forgot it, e.g. jquery.history.js in the yii repo.
*
* @param string $name of package
* @param string $type of package, either js or css
* @param array $files List of files to combine (full path)
* @return string full path name of combined file
*/
private function combineFiles($name,$type,$files)
{
$fileName = tempnam(Yii::app()->runtimePath,'combined_'.$name);
foreach($files as $f) {
if($type == 'css' && $this->enableCssImageFingerPrinting)
$fileContents = $this->addCssImageFingerPrints($f);
else
$fileContents = file_get_contents($f);
if(!file_put_contents($fileName, $fileContents.($type==='js'?';':'')."\n", FILE_APPEND))
throw new CException(sprintf(
'Could not combine file "%s" into "%s"',
$f,
$fileName
));
}
return $fileName;
}
/**
* Create a compressed file using YUI compressor (requires JRE!)
*
* @param string $name of package
* @param string $type of package, either js or css
* @param array $files list of full file paths to files
* @return string file name of compressed file
*/
private function compressFiles($name,$type,$files)
{
YII_DEBUG && Yii::trace(sprintf(
"Compressing %s package %s:\n%s",
$type,
$name,
implode(",\n",$files)
),'application.components.packagecompressor');
$inFile = $this->combineFiles($name,$type,$files);
$outFile = sprintf(
'%s/%s_pkg_%s_%s.%s',
Yii::app()->runtimePath,
$type,
$name,
md5_file($inFile),
$type
);
$jar = Yii::getPathOfAlias('_packagecompressor.yuicompressor').DIRECTORY_SEPARATOR.self::YUI_COMPRESSOR_JAR;
// See http://developer.yahoo.com/yui/compressor/
$command = sprintf("%s -jar %s --type %s -o %s %s",escapeshellarg($this->javaBin),escapeshellarg($jar),$type,escapeshellarg($outFile),escapeshellarg($inFile));
if($this->combineOnly)
copy($inFile,$outFile);
else
{
exec($command,$output,$result);
if ($result!==0)
throw new CException(sprintf(
"Could not create compressed $type file. Maybe missing a JRE?\nCommand was:\n%s",
$command
));
}
unlink($inFile);
return $outFile;
}
/**
* Load meta information about compressed packages from global state
*
* @param bool wether to enforce that global state is refreshed
*/
private function loadPackageData($forceRefresh=false)
{
// Make sure, statefile is read in again. It could have been changed from
// another request, while we were waiting for the mutex lock
if ($forceRefresh)
Yii::app()->loadGlobalState();
$this->_pd = Yii::app()->getGlobalState($this->getStateKey(),array());
}
/**
* Replace all sripts registered at $coreScriptPosition with compressed file
*/
private function renderCompressedPackage($name)
{
if(($package = $this->getCompressedInfo($name))===null)
return;
if(isset($package['js']))
{
$p = $this->coreScriptPosition;
// Keys in scriptFiles must be equal to value to make unifyScripts work:
$packageFiles = array_combine($package['js']['urls'],$package['js']['urls']);
$this->scriptFiles[$p]=isset($this->scriptFiles[$p]) ?
array_merge($packageFiles,$this->scriptFiles[$p]) : $packageFiles;
YII_DEBUG && Yii::trace(
sprintf(
"Render compressed js package '%s'\nContains:\n%s\nURLs:\n%s\nCoreScripts: %s",
$name,
implode(",\n",$package['js']['files']),
implode(",\n",$package['js']['urls']),
implode(", ",$package['js']['coreScripts'])
),
'application.components.packagecompressor'
);
}
if(isset($package['css']))
{
$cssFiles = $this->cssFiles;
$urls = $this->cssFiles = array();
foreach($package['css']['urls'] as $url)
$this->cssFiles[$url] = $package['css']['media'];
foreach($cssFiles as $url => $media)
$this->cssFiles[$url] = $media;
YII_DEBUG && Yii::trace(
sprintf(
"Render compressed css package '%s'\nContains:\n%s\nURLs:\n%s",
$name,
implode(",\n",$package['css']['files']),
implode(",\n",$package['css']['urls'])
),
'application.components.packagecompressor'
);
}
}
/**
* Remove any registered core scripts and packages if we have it in the package to prevent publishing
*
* @param string $name of package
*/
private function unregisterPackagedCoreScripts($package)
{
if (($info = $this->getCompressedInfo($package))===null || !isset($info['js']))
return;
// Remove the package itself from the coreScripts or it would
// still render the uncompressed script files
unset($this->coreScripts[$package]);
// Also remove the coreScripts contained in the package
foreach($info['js']['coreScripts'] as $name) {
unset($this->coreScripts[$name]);
unset($this->_registeredPackages[$name]);
}
}
/**
* Save meta information about compressed packages to global state
*/
private function savePackageData()
{
Yii::app()->setGlobalState($this->getStateKey(),$this->_pd);
// We want to be sure that global state is written immediately. Default would be onEndRequest.
Yii::app()->saveGlobalState();
}
/**
* Stores meta information about compressed package to cache and global state
*
* @param mixed Array of format array('file'=>...,'urls'=>...) or null to reset
*/
private function setCompressedInfo($name,$value)
{
if($this->_pd===null)
$this->loadPackageData();
if($value!==null)
$this->_pd[$name]=$value;
elseif(isset($this->_pd[$name]))
unset($this->_pd[$name]);
$this->savePackageData();
}
/**
* Attempt to add a fingerprint to any image url found in the input file
*
* Example CSS that we want to match:
* background: url('/images/stars/star.png') no-repeat 0px 0px;
*
* Resulting image file that we want to get a fingerprint for: /images/stars/star.png;
*
* Notes:
* A) Cope with ' or " or no string encloser.
* B) Only modify local assets, i.e. not apsolute urls.
* C) Only modify jpg|jpeg|gif|png assets.
* D) Don't modify assets that already have a fingerprint
*
* @param String $fileName Full file name of a CSS file
* @return string Modified CSS text
*/
private function addCssImageFingerPrints($fileName)
{
$webRoot = realpath(Yii::getPathOfAlias('webroot'));
return preg_replace_callback(
'/url\([\'\"]?(.*?\.(jpg|jpeg|gif|png))[\'\"]?\)/',
function($matches) use ($fileName,$webRoot)
{
$imageURL = $matches[1];
if (preg_match('/^(https?:)?\/\//', $imageURL))
return $matches[0]; // We don't fingerprint absolute urls
// image urls that start with a / are relative to the webroot, otherwise they are relative to the url of the CSS file.
$imageFile = ( $imageURL[0]==='/' ? $webRoot : dirname($fileName).'/' ) . $imageURL;
if (file_exists($imageFile))
return 'url(\''.$imageURL.'?'.substr(md5_file($imageFile),0,8).'\')';
// We won't always find the image, e.g. there might be a mod_rewrite rule in play.
YII_DEBUG && Yii::trace(
sprintf("Unable to find css image '%s' on disk. Css File: '%s'. CSS: %s.",
$imageFile,
$fileName,
$matches[0]),
'application.components.packagecompressor'
);
return $matches[0];
},
file_get_contents($fileName)
);
}
}