1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DOM component functions |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait DomCssAndJavascriptByDanielGPwithCDN |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
private function knownCloudFlareJavascript($jsFileName) |
40
|
|
|
{ |
41
|
|
|
$justFile = pathinfo($jsFileName)['basename']; |
42
|
|
|
switch ($justFile) { |
43
|
|
|
case 'jquery.placeholder.min.js': |
44
|
|
|
return [ |
45
|
|
|
'justFile' => $justFile, |
46
|
|
|
'version' => 'jquery-placeholder/2.0.8/', |
47
|
|
|
'eVerify' => 'jQuery.placeholder', |
48
|
|
|
]; |
49
|
|
|
// intentionally left blank |
50
|
|
|
case 'jquery.easing.1.3.min.js': |
51
|
|
|
return [ |
52
|
|
|
'justFile' => str_replace('.1.3', '', $justFile), |
53
|
|
|
'version' => 'jquery-easing/1.3/', |
54
|
|
|
'eVerify' => 'jQuery.easing["jswing"]', |
55
|
|
|
]; |
56
|
|
|
// intentionally left blank |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Manages all known CSS that can be handled through CDNs |
62
|
|
|
* |
63
|
|
|
* @param string $cssFileName |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function setCssFileCDN($cssFileName) |
67
|
|
|
{ |
68
|
|
|
$patternFound = null; |
69
|
|
|
if (strpos(pathinfo($cssFileName)['basename'], 'font-awesome-') !== false) { |
70
|
|
|
$patternFound = $this->setCssFileCDNforFontAwesome($cssFileName); |
71
|
|
|
} |
72
|
|
View Code Duplication |
if (is_null($patternFound)) { |
|
|
|
|
73
|
|
|
$patternFound = [false, filter_var($cssFileName, FILTER_SANITIZE_STRING)]; |
74
|
|
|
} |
75
|
|
|
return $patternFound; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns css link to a given file |
80
|
|
|
* Returns an array with CDN call of a known Font-websome css |
81
|
|
|
* |
82
|
|
|
* @param string $cssFileName |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
private function setCssFileCDNforFontAwesome($cssFileName) |
86
|
|
|
{ |
87
|
|
|
$patternFound = [ |
88
|
|
|
true, |
89
|
|
|
implode('', [ |
90
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/font-awesome/', |
91
|
|
|
str_replace(['font-awesome-', '.min.css'], '', pathinfo($cssFileName)['basename']), |
92
|
|
|
'/css/font-awesome.min.css', |
93
|
|
|
]) |
94
|
|
|
]; |
95
|
|
|
return $patternFound; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Manages all known Javascript that can be handled through CDNs |
100
|
|
|
* |
101
|
|
|
* @param string $jsFileName |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function setJavascriptFileCDN($jsFileName) |
105
|
|
|
{ |
106
|
|
|
$onlyFileName = pathinfo($jsFileName)['basename']; |
107
|
|
|
// if within local network makes no sense to use CDNs |
108
|
|
|
$patternFound = null; |
109
|
|
|
if (strpos($onlyFileName, 'jquery-') !== false) { |
110
|
|
|
$patternFound = $this->setJavascriptFileCDNjQuery($jsFileName); |
111
|
|
|
} elseif (strpos($onlyFileName, 'jquery.placeholder.min.js') !== false) { |
112
|
|
|
$patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName); |
113
|
|
|
} elseif (strpos($onlyFileName, 'jquery.easing.1.3.min.js') !== false) { |
114
|
|
|
$patternFound = $this->setJavascriptFileCDNjQueryLibs($jsFileName); |
115
|
|
|
} elseif (strpos($onlyFileName, 'highcharts-') !== false) { |
116
|
|
|
$patternFound = $this->setJavascriptFileCDNforHighCharts($jsFileName); |
117
|
|
|
} elseif (strpos($onlyFileName, 'exporting-') !== false) { |
118
|
|
|
$patternFound = $this->setJavascriptFileCDNforHighChartsExporting($jsFileName); |
119
|
|
|
} |
120
|
|
View Code Duplication |
if (is_null($patternFound)) { |
|
|
|
|
121
|
|
|
$patternFound = [false, filter_var($jsFileName, FILTER_SANITIZE_STRING), '']; |
122
|
|
|
} |
123
|
|
|
return $patternFound; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns an array with CDN call of a known Javascript library |
128
|
|
|
* and fall-back line that points to local cache of it |
129
|
|
|
* specific for HighCharts |
130
|
|
|
* |
131
|
|
|
* @param string $jsFileName |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function setJavascriptFileCDNforHighCharts($jsFileName) |
135
|
|
|
{ |
136
|
|
|
return $this->setJavascriptFileCDNforHighChartsMain($jsFileName, 'highcharts'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns an array with CDN call of a known Javascript library |
141
|
|
|
* and fall-back line that points to local cache of it |
142
|
|
|
* specific for HighCharts Exporting feature |
143
|
|
|
* |
144
|
|
|
* @param string $jsFileName |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
private function setJavascriptFileCDNforHighChartsExporting($jsFileName) |
148
|
|
|
{ |
149
|
|
|
return $this->setJavascriptFileCDNforHighChartsMain($jsFileName, 'exporting'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns an array with CDN call of a known Javascript library |
154
|
|
|
* and fall-back line that points to local cache of it |
155
|
|
|
* specific for HighCharts |
156
|
|
|
* |
157
|
|
|
* @param string $jsFileName |
158
|
|
|
* @param string $libName |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
private function setJavascriptFileCDNforHighChartsMain($jsFileName, $libName) |
162
|
|
|
{ |
163
|
|
|
$jsFN = filter_var($jsFileName, FILTER_SANITIZE_STRING); |
164
|
|
|
$jsVersionlessFN = str_replace([$libName . '-', '.js'], '', pathinfo($jsFileName)['basename']); |
165
|
|
|
if ($libName === 'exporting') { |
166
|
|
|
$jsVersionlessFN .= '/modules'; |
167
|
|
|
} |
168
|
|
|
if (strpos($jsFileName, $libName) !== false) { |
169
|
|
|
return [ |
170
|
|
|
true, |
171
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/highcharts/' . $jsVersionlessFN . '/' . $libName . '.js', |
172
|
|
|
'<script>!window.Highcharts && document.write(\'<script src="' . $jsFN . '">\x3C/script>\')</script>', |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns an array with CDN call of a known Javascript library |
180
|
|
|
* and fall-back line that points to local cache of it |
181
|
|
|
* specific for jQuery |
182
|
|
|
* |
183
|
|
|
* @param string $jsFileName |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
private function setJavascriptFileCDNjQuery($jsFileName) |
187
|
|
|
{ |
188
|
|
|
$patternFound = null; |
189
|
|
|
$jQueryPosition = strpos($jsFileName, 'jquery-'); |
190
|
|
|
$jQueryMajorVersion = substr($jsFileName, 7, 1); |
191
|
|
|
if (($jQueryPosition !== false) && is_numeric($jQueryMajorVersion) && (substr($jsFileName, -7) == '.min.js')) { |
192
|
|
|
$patternFound = [ |
193
|
|
|
true, |
194
|
|
|
implode('', [ |
195
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/jquery/', |
196
|
|
|
str_replace(['jquery-', '.min.js'], '', pathinfo($jsFileName)['basename']), |
197
|
|
|
'/jquery.min.js', |
198
|
|
|
]), |
199
|
|
|
implode('', [ |
200
|
|
|
'<script>window.jQuery || document.write(\'<script src="', |
201
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
202
|
|
|
'">\x3C/script>\')</script>' |
203
|
|
|
]) |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
return $patternFound; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns an array with CDN call of a known Javascript library |
211
|
|
|
* and fall-back line that points to local cache of it |
212
|
|
|
* specific for jQuery Libraries |
213
|
|
|
* |
214
|
|
|
* @param string $jsFileName |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
private function setJavascriptFileCDNjQueryLibs($jsFileName) |
218
|
|
|
{ |
219
|
|
|
$patternFound = null; |
220
|
|
|
$eArray = $this->knownCloudFlareJavascript(filter_var($jsFileName, FILTER_SANITIZE_STRING)); |
221
|
|
|
if (!is_null($eArray['version'])) { |
222
|
|
|
$patternFound = [ |
223
|
|
|
true, |
224
|
|
|
implode('', [ |
225
|
|
|
'//cdnjs.cloudflare.com/ajax/libs/', |
226
|
|
|
$eArray['version'], |
227
|
|
|
$eArray['justFile'], |
228
|
|
|
]), |
229
|
|
|
implode('', [ |
230
|
|
|
'<script>' . $eArray['eVerify'] . ' || document.write(\'<script src="', |
231
|
|
|
filter_var($jsFileName, FILTER_SANITIZE_STRING), |
232
|
|
|
'">\x3C/script>\')</script>' |
233
|
|
|
]) |
234
|
|
|
]; |
235
|
|
|
} |
236
|
|
|
return $patternFound; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.