1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\View\Mustache; |
4
|
|
|
|
5
|
|
|
use \Mustache_LambdaHelper as LambdaHelper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Mustache helpers for rendering CSS and JavaScript. |
9
|
|
|
*/ |
10
|
|
|
class AssetsHelpers implements HelpersInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* A string concatenation of inline `<script>` elements. |
14
|
|
|
* |
15
|
|
|
* @var string $js |
16
|
|
|
*/ |
17
|
|
|
private static $js = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An array of `<script>` elements referencing external scripts. |
21
|
|
|
* |
22
|
|
|
* @var array $jsRequirements |
23
|
|
|
*/ |
24
|
|
|
private static $jsRequirements = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A string concatenation of inline `<style>` elements. |
28
|
|
|
* |
29
|
|
|
* @var string $css; |
30
|
|
|
*/ |
31
|
|
|
private static $css = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* An array of `<link>` elements referencing external style sheets. |
35
|
|
|
* |
36
|
|
|
* @var array $cssRequirements |
37
|
|
|
*/ |
38
|
|
|
private static $cssRequirements = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve the collection of helpers. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function toArray() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'purgeJs' => function() { |
49
|
|
|
$this->purgeJs(); |
50
|
|
|
}, |
51
|
|
|
'addJs' => function($js, LambdaHelper $helper) { |
52
|
|
|
$this->addJs($js, $helper); |
53
|
|
|
}, |
54
|
|
|
'js' => function() { |
55
|
|
|
return $this->js(); |
56
|
|
|
}, |
57
|
|
|
'addJsRequirement' => function($js, LambdaHelper $helper) { |
58
|
|
|
$this->addJsRequirement($js, $helper); |
59
|
|
|
}, |
60
|
|
|
'jsRequirements' => function() { |
61
|
|
|
return $this->jsRequirements(); |
62
|
|
|
}, |
63
|
|
|
'addCss' => function($css, LambdaHelper $helper) { |
64
|
|
|
$this->addCss($css, $helper); |
65
|
|
|
}, |
66
|
|
|
'purgeCss' => function() { |
67
|
|
|
$this->purgeCss(); |
68
|
|
|
}, |
69
|
|
|
'css' => function() { |
70
|
|
|
return $this->css(); |
71
|
|
|
}, |
72
|
|
|
'addCssRequirement' => function($css, LambdaHelper $helper) { |
73
|
|
|
$this->addCssRequirement($css, $helper); |
74
|
|
|
}, |
75
|
|
|
'cssRequirements' => function() { |
76
|
|
|
return $this->cssRequirements(); |
77
|
|
|
}, |
78
|
|
|
'purgeAssets' => function() { |
79
|
|
|
$this->purgeAssets(); |
80
|
|
|
} |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Empty the JS assets queue. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function purgeJs() |
90
|
|
|
{ |
91
|
|
|
self::$js = ''; |
92
|
|
|
self::$jsRequirements = []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Enqueue (concatenate) inline JavaScript content. |
97
|
|
|
* |
98
|
|
|
* Must include `<script>` surrounding element. |
99
|
|
|
* |
100
|
|
|
* @param string $js The JavaScript to add. |
101
|
|
|
* @param LambdaHelper $helper For rendering strings in the current context. |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function addJs($js, LambdaHelper $helper = null) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if ($helper !== null) { |
107
|
|
|
$js = $helper->render($js); |
108
|
|
|
} |
109
|
|
|
self::$js .= $js; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the saved inline JavaScript content and purge the store. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function js() |
118
|
|
|
{ |
119
|
|
|
$js = self::$js; |
120
|
|
|
self::$js = ''; |
121
|
|
|
return $js; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Enqueue an external JavaScript file. |
126
|
|
|
* |
127
|
|
|
* Must include `<script>` surrounding element. |
128
|
|
|
* |
129
|
|
|
* @param string $js The JavaScript to add. |
130
|
|
|
* @param LambdaHelper $helper For rendering strings in the current context. |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function addJsRequirement($js, LambdaHelper $helper = null) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$js = trim($js); |
136
|
|
|
$key = md5($js); |
137
|
|
|
|
138
|
|
|
if (!isset(self::$jsRequirements[$key])) { |
139
|
|
|
if ($helper !== null) { |
140
|
|
|
$js = $helper->render($js); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
self::$jsRequirements[$key] = $js; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the JavaScript requirements and purge the store. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function jsRequirements() |
153
|
|
|
{ |
154
|
|
|
$req = implode("\n", self::$jsRequirements); |
155
|
|
|
self::$jsRequirements = []; |
156
|
|
|
return $req; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Empty the CSS assets queue. |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function purgeCss() |
165
|
|
|
{ |
166
|
|
|
self::$css = ''; |
167
|
|
|
self::$cssRequirements = []; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Enqueue (concatenate) inline CSS content. |
172
|
|
|
* |
173
|
|
|
* Must include `<style>` surrounding element. |
174
|
|
|
* |
175
|
|
|
* @param string $css The CSS string to add. |
176
|
|
|
* @param LambdaHelper $helper For rendering strings in the current context. |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function addCss($css, LambdaHelper $helper = null) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
if ($helper !== null) { |
182
|
|
|
$css = $helper->render($css); |
183
|
|
|
} |
184
|
|
|
self::$css .= $css; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the saved inline CSS content and purge the store. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function css() |
193
|
|
|
{ |
194
|
|
|
$css = self::$css; |
195
|
|
|
self::$css = ''; |
196
|
|
|
return $css; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Enqueue an external CSS file. |
201
|
|
|
* |
202
|
|
|
* Must include `<link />` or surrounding `<style>` element. |
203
|
|
|
* |
204
|
|
|
* @param string $css The CSS requirements. |
205
|
|
|
* @param LambdaHelper $helper For rendering strings in the current context. |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
public function addCssRequirement($css, LambdaHelper $helper = null) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$css = trim($css); |
211
|
|
|
$key = md5($css); |
212
|
|
|
|
213
|
|
|
if (!isset(self::$cssRequirements[$key])) { |
214
|
|
|
if ($helper !== null) { |
215
|
|
|
$css = $helper->render($css); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
self::$cssRequirements[$key] = $css; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the CSS requirements and purge the store. |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function cssRequirements() |
228
|
|
|
{ |
229
|
|
|
$req = implode("\n", self::$cssRequirements); |
230
|
|
|
self::$cssRequirements = []; |
231
|
|
|
return $req; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Empty the all asset queues. |
236
|
|
|
* |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
|
|
public function purgeAssets() |
240
|
|
|
{ |
241
|
|
|
$this->purgeJs(); |
242
|
|
|
$this->purgeCss(); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
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.