forked from markstory/asset_compress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetCompressor.php
124 lines (107 loc) · 3.05 KB
/
AssetCompressor.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
<?php
App::uses('DispatcherFilter', 'Routing');
App::uses('AssetConfig', 'AssetCompress.Lib');
App::uses('AssetCompiler', 'AssetCompress.Lib');
App::uses('AssetCache', 'AssetCompress.Lib');
App::uses('Folder', 'Utility');
class AssetCompressor extends DispatcherFilter {
/**
* Filter priority, we need it to run before router
*
* @var integer
*/
public $priority = 9;
/**
* Object containing configuration settings for asset compressor
*
* @var AssetConfig
*/
protected $_Config;
/**
* Checks if request is for a compiled asset, otherwise skip any operation
*
* @param CakeEvent $event containing the request and response object
* @throws NotFoundException
* @return CakeResponse if the client is requesting a recognized asset, null otherwise
*/
public function beforeDispatch(CakeEvent $event) {
$request = $event->data['request'];
$response = $event->data['response'];
$url = $request->url;
$config = $this->_getConfig();
$production = !Configure::read('debug');
if ($production && !$config->general('alwaysEnableController')) {
return;
}
$build = $this->_getBuild($url);
if ($build === false) {
return;
}
if (isset($request->query['theme'])) {
$config->theme($event->data['request']->query['theme']);
}
// Dynamically defined build file. Disabled in production for
// hopefully obvious reasons.
if ($config->files($build) === array()) {
$files = array();
if (isset($request->query['file'])) {
$files = $request->query['file'];
}
$config->files($build, $files);
}
// Use the TMP dir for dev builds.
// This is to avoid permissions issues with the configured paths.
$cachePath = CACHE . 'asset_compress' . DS;
$folder = new Folder($cachePath, true);
$folder->chmod($cachePath, 0777);
$ext = $config->getExt($build);
$config->cachePath($ext, $cachePath);
$config->set("$ext.timestamp", false);
try {
$compiler = new AssetCompiler($config);
$cache = new AssetCache($config);
if ($cache->isFresh($build)) {
$contents = file_get_contents($cachePath . $build);
} else {
$contents = $compiler->generate($build);
$cache->write($build, $contents);
}
} catch (Exception $e) {
throw new NotFoundException($e->getMessage());
}
$response->type($config->getExt($build));
$response->body($contents);
$event->stopPropagation();
return $response;
}
/**
* Returns the build name for a requested asset
*
* @return boolean|string false if no build can be parsed from URL
* with url path otherwise
*/
protected function _getBuild($url) {
$parts = explode('.', $url);
if (count($parts) < 2) {
return false;
}
$path = $this->_getConfig()->cachePath($parts[(count($parts) - 1)]);
if (empty($path)) {
return false;
}
$path = str_replace(WWW_ROOT, '', $path);
if (strpos($url, $path) !== 0) {
return false;
}
return str_replace($path, '', $url);
}
/**
* Config setter, used for testing the filter.
*/
protected function _getConfig() {
if (empty($this->_Config)) {
$this->_Config = AssetConfig::buildFromIniFile();
}
return $this->_Config;
}
}