forked from jakob-stoeck/yii-ext-mtClientScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtClientScript.php
279 lines (248 loc) · 8.12 KB
/
mtClientScript.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
<?php
define('DS', DIRECTORY_SEPARATOR);
class mtClientScript extends CClientScript {
const TYPE_CSS = 'css';
const TYPE_JS = 'js';
public $combine = false;
/**
* @var array files to exclude from beeing combined and compressed
*/
public $excludeFiles = array();
/**
* @var bool exclude asset files like core scripts
*/
public $excludeAssets = false;
/**
* @var string Absolute file path to java
*/
public $javaPath = '/usr/bin/java';
/**
* @var string Absolute file path to yui compressor
*/
public $yuicPath = null;
/**
* @var string Absolute file path to google closure compiler
*/
public $closurePath = null;
const CLOSURE_WHITESPACE = 'WHITESPACE_ONLY';
const CLOSURE_SIMPLE = 'SIMPLE_OPTIMIZATIONS';
const CLOSURE_ADVANCED = 'ADVANCED_OPTIMIZATIONS';
const FORMAT_PRETTY = 'PRETTY_PRINT';
const FORMAT_DELIMITER = 'PRINT_INPUT_DELIMITER';
/**
* @var string 'WHITESPACE_ONLY' | 'SIMPLE_OPTIMIZATIONS' | 'ADVANCED_OPTIMIZATIONS'
*/
public $closureConfig = self::CLOSURE_SIMPLE;
/**
* @var array list of cdn hosts
*/
public $cdn = array();
/**
* @var array list of file => host
*/
public $fileToHost;
private $_defaultCssMedia = 'screen, projection';
private $_baseUrl = '';
private $_basePath = '';
private $_assetsPath = '';
public function registerCoreScript($name) {
if (isset($this->packages[$name], $this->packages[$name]['globals'])) {
foreach ($this->packages[$name]['globals'] as $key => $value) {
if (strpos($value,'js:')!==0) {
$script = 'var ' . $key . '="' . str_replace('"', '\"', $value) . '";';
}
else {
// if the value starts with js: it will not be wrapped in quotes, as we might want to register JS objects, booleans or other data types
$value = substr($value, 3);//remove js: from the string
$script = 'var ' . $key . '=' . $value . ';';
}
$this->registerScript($key, $script, CClientScript::POS_BEGIN);
}
}
return parent::registerCoreScript($name);
}
public function init() {
parent::init();
if (!is_readable($this->yuicPath)) {
$this->yuicPath = dirname(__FILE__) . DS . 'yuicompressor-2.4.8pre.jar';
}
if (!is_readable($this->closurePath)) {
$this->closurePath = dirname(__FILE__) . DS . 'compiler.jar';
}
$this->_baseUrl = Yii::app()->baseUrl;
$this->_basePath = YiiBase::getPathOfAlias('webroot');
$this->_assetsPath = $this->_basePath . str_replace($this->_baseUrl, '', $this->getCoreScriptUrl());
}
public function setDescription() {
return 'This plugin combines and shrinks all js and css files';
}
public function getPositions() {
return array(self::POS_BEGIN, self::POS_READY, self::POS_LOAD, self::POS_HEAD, self::POS_END);
}
public function isAsset($url) {
return strpos($url, $this->getCoreScriptUrl()) === 0;
}
/**
* @param array $urls
* @return array of timestamps
*/
private function filesmtimes($urls) {
$return = array();
foreach ($urls as $url) {
$return[] = $this->filemtimeCheck($url);
}
return $return;
}
private function filemtimeCheck($url) {
$filePath = $this->_basePath . rtrim($this->_baseUrl, '/') . '/' . $url;
if (stream_resolve_include_path($filePath)) {
return filemtime($filePath);
}
}
public function registerCssFileWithTimestamp($name) {
$name .= '?t=' . $this->filemtimeCheck($name);
return parent::registerCssFile($name);
}
public function registerScriptFileWithTimestamp($name,$position=null) {
$name .= '?t=' . $this->filemtimeCheck($name);
return parent::registerScriptFile($name,$position);
}
private function combineScripts() {
// each package gets its own minified version
foreach ($this->packages as $name => $package) {
$this->_baseUrl = isset($package['baseUrl']) ? $package['baseUrl'] : $this->getCoreScriptUrl();
$files = $package[self::TYPE_JS];
$mtimes = $this->filesmtimes($files);
if (empty($mtimes)) { continue; }
$outFile = ereg_replace('[^a-zA-Z0-9]', '', $name) . '_' . md5(implode('', $mtimes));
$urls = array();
foreach ($files as $f) {
$base = basename($f);
// don't remap already mapped files
if (!array_key_exists($base, $this->scriptMap)) {
$this->scriptMap[$base] = $this->getCoreScriptUrl() . '/' . $outFile . '.' . self::TYPE_JS;
$urls[] = $this->_baseUrl . '/' . $f;
}
}
// happens if all package files were already mapped
if (empty($urls)) { continue; }
$this->combineFiles(self::TYPE_JS, $urls, $outFile);
}
$this->remapScripts();
}
/**
* @param string the output to be inserted with scripts.
*/
public function renderHead(&$output) {
if ($this->combine) {
$this->combineScripts();
}
parent::renderHead($output);
}
/**
* Returns one host per file, iterating through the hosts to distribute them evenly
*
* @param string $file
* @return string
*/
public function fileToHost($file) {
if(!isset($this->fileToHost[$file])) {
$this->fileToHost[$file] = current($this->cdn);
if(!next($this->cdn)) reset($this->cdn);
}
return $this->fileToHost[$file];
}
/**
* Combines, optimizes and compresses all given files
*
* @param string $type js or css
* @param array $urls array of url of the files
* @param string $media optional, only relevant for css
* @return string name of the resulting file
* @author Florian Fackler
*/
private function combineFiles($type, $files, $outFile='out') {
if (!in_array($type, array('js', 'css'))) {
throw new Exception('Only js or css as file type allowed');
}
if (file_exists($this->_assetsPath . DS . $outFile . '.' . $type)) {
return;
}
switch ($type) {
case self::TYPE_CSS:
return;
$joinedContent = '';
foreach ($files as $file) {
if(isset($urlOfFile[$file][1]) && $urlOfFile[$file][1] === 'themes') {
$theme = $urlOfFile[$file][1] . '/' . $urlOfFile[$file][2] . '/';
} else {
$theme = '';
}
// Correct file path in css/js files :: MUST BE RELATIVE
$content = file_get_contents($file);
$content = str_replace('../', '../../' . $theme, $content);
$search = array('/(\'\")^\//');
$replace = array('$1' . '/var/www');
if(!empty($this->cdn)) {
$search[] = '/url\([\'"]?(\/[^)\'"]+)[\'"]?\)/e'; // adds a host to absolute urls
$replace[] = '"url(http://" . $this->fileToHost("$1") . "$1)"';
}
$content = preg_replace($search, $replace, $content);
$joinedContent .= $content;
}
$this->closurify($joinedContent, $outFile, $type);
break;
case self::TYPE_JS:
$this->minifyJs($files, $outFile);
break;
}
return $outFile;
}
private function minifyJs($urls, $outFile) {
if (empty($urls))
throw new CException('Minification does not contain URLs for ' . $outFile);
$cmd = implode(' ', array(
$this->javaPath, '-jar' , $this->closurePath,
'--module_output_path_prefix', $this->_assetsPath . DS,
'--compilation_level', $this->closureConfig,
// '--warning_level', 'QUIET', // 'QUIET|DEFAULT|VERBOSE'
'--formatting', self::FORMAT_DELIMITER,
sprintf('--module %s:%d %s',
$outFile,
count($urls),
'--js ' . implode(' --js ', array_map(function($u) {
return ltrim($u, '/');
}, $urls))
),
'2>&1',
));
$cmd=sprintf('IS_RUNNING=`ps -efa | grep "%1$s" | grep -v grep | wc -l`; if [ "$IS_RUNNING" -eq 0 ]; then %1$s; fi', $cmd);
$return = shell_exec($cmd);
if (!empty($return))
Yii::log($return, CLogger::LEVEL_ERROR, 'ClientScript');
}
private function closurify($content, $outFile, $type) {
$temp = $this->_basePath . DS . 'protected' . DS . 'runtime' . DS . $outFile;
file_put_contents($temp, $content);
unset($content);
switch ($type) {
case self::TYPE_CSS:
$cmd = sprintf('%s -jar %s -o %s %s',
$this->javaPath,
$this->yuicPath,
$this->_assetsPath . DS . $outFile,
$temp);
break;
case self::TYPE_JS:
$cmd = sprintf('%s -jar %s --js_output_file %s --compilation_level %s --js %s --formatting=PRETTY_PRINT',
$this->javaPath,
$this->closurePath,
$this->_assetsPath . DS . $outFile,
$this->closureConfig,
$temp);
break;
}
$cmd = sprintf('IS_RUNNING=`ps -efa | grep "%1$s" | grep -v grep | wc -l`; if [ "$IS_RUNNING" -eq 0 ]; then %1$s; fi', $cmd);
$return = shell_exec($cmd);
}
}