This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEClientScriptBoost.php
183 lines (171 loc) · 7.48 KB
/
EClientScriptBoost.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
<?php
/**
* EClientScriptBoost class
*
* Is an extended version of CClientScript to compress registered scripts
*
* @author Antonio Ramirez Cobos
* @link www.ramirezcobos.com
*
*
* @copyright
*
* Copyright (c) 2010 Antonio Ramirez Cobos
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class EClientScriptBoost extends CClientScript
{
public $cacheDuration = 0;
/**
* @var boolean Allows to put the component into debug mode. Registered content will be checked
* against its cached version (if present) and a warning will be issued if the
* freshly compressed and cached content do not match.
* If there is a mismatch, it means that wrong content is being served from cache. There could be two
* reasons for that:
* 1. Original script has been changed, but the cache holds the previous version. Flushing or
* clearing cache will resolve this.
* 2. There are views or components that are using same IDs in the registerScript() and registerCss()
* calls. The debug message includes the IDs with mismatch, which will help to identify the source.
* It is recommended to run your project for couple days or weeks with this debug mode on to catch
* such overlapping IDs.
* NOTE: When debug mode is off, cached content will silently served. If you have overlapping content
* IDs, then this will most likely result in all kinds of unxpected behaviour! Make sure to run
* EClientScriptBoost in debug mode after you introduce changes or add new scripts. Also, DO NOT include
* dynamic stuff like model IDs, links etc. in the scripts you register. It is a good practice to write
* the scripts to use variables defined by the view itself, for example in a <script></script> section.
*/
public $debug;
/**
* @var array Ids for scripts that should not be cached.
* For instance: when the cached script has a CSRF token.
* The solution for application controlled scripts is to add the CSRF Value to the ID,
* but for scripts in extensions or the framework, it is not recommended to change the framework
* nor the extension, so they are added in this list.
*/
public $skipList=array(
'CActiveForm#',
'CButtonColumn',
'CGridView#',
'CJuiDialog',
'CJuiButton',
'EditableField#',
'JToggleColumn#',
'TbBulkActions#',
'TbEditableField#',
'TbEditable#',
'TbGridView#',
'TbSelect2#',
'Yii2Debug#',
'Yii.CHtml.#',
);
/**
* @see CClientScript::registerScript()
*/
public function registerScript($id,$script,$position=null,array $htmlOptions=array())
{
return $this->registerContent('script', $id, $script, $position, $htmlOptions);
}
/**
* @see CClientScript::registerCss()
*/
public function registerCss($id, $css, $media = '')
{
return $this->registerContent('css', $id, $css, $media);
}
/**
* All minifying and caching logic for registerScript() and registerCss() is defined here
* for much of this logic is the same between the two methods.
* @param string $type either 'script' or 'css', determines which minify function
* is used on the content.
* @param string $id ID that uniquely identifies this piece of JavaScript code
* @param string $content content that should be compressed (either JS or CSS) and registered
* @param integer|string $registerParam CClientScript::POS_* value when called
* by {@link registerScript()}, CSS media string when called by {@link registerContent()}
* @param array $htmlOptions additional HTML attributes (only for scripts)
* Note: HTML attributes are not allowed for script positions "CClientScript::POS_LOAD" and "CClientScript::POS_READY".
* @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5)
* @throws CException
*/
protected function registerContent($type, $id, $content, $registerParam=null, array $htmlOptions=array())
{
if ($type != 'script' && $type != 'css')
throw new CException(__CLASS__ . " supports only registerScript() and registerCss()");
// if debug mode has not been specified by the Yii config part of clientScript
// set the mode based on global YII_DEBUG
$debug = $this->debug === null ? YII_DEBUG : $this->debug;
// Check if this script is in the exceptions - if so, skip caching.
// Should be checked for both script and CSS content
$skip = false;
foreach($this->skipList as $s) {
$skip|=strpos($id, $s) === 0;
if($skip) break;
}
// Do not use cache for content with IDs listed in {@link $this->skipList}
if ($skip)
{
if ($type == 'script')
$compressed = EScriptBoost::minifyJs($content);
else if ($type == 'css')
$compressed = EScriptBoost::minifyCss($content);
}
else
$compressed = Yii::app()->cache->get($id);
// if in debug mode -- recompress the content and compare with the
// cached version if present
if ($debug && $compressed !== false)
{
// During debug check that the newly minified content is not different from the cached one.
// If so, log the difference so that it can be fixed.
if ($type == 'script')
$recompressed = EScriptBoost::minifyJs($content);
else if ($type == 'css')
$recompressed = EScriptBoost::minifyCss($content);
if ($recompressed !== $compressed)
{
Yii::log(
"Recompressed {$type} with id '$id' does not match to the previously cached version.\n\n"
. "Newly compressed version:\n\n"
. "----------------------- < begin > -----------------------\n\n"
. CVarDumper::dumpAsString($recompressed)
. "\n\n----------------------- < end > -----------------------\n\n"
. PHP_EOL
. "Cached compressed version:\n\n"
. "----------------------- < begin > -----------------------\n\n"
. CVarDumper::dumpAsString($compressed)
. "\n\n----------------------- < end > -----------------------\n\n"
, CLogger::LEVEL_WARNING
);
}
}
elseif ($compressed === false)
{
if ($type == 'script')
$compressed = EScriptBoost::minifyJs($content);
else if ($type == 'css')
$compressed = EScriptBoost::minifyCss($content);
Yii::app()->cache->set($id, $compressed, $this->cacheDuration);
}
if ($type == 'script')
{
$position = empty($registerParam) ? null : $registerParam;
return parent::registerScript($id, $compressed, $registerParam, $htmlOptions);
}
else if ($type == 'css')
{
$media = empty($registerParam) ? '' : $registerParam;
return parent::registerCss($id, $compressed, $media);
}
}
}