|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Code.php |
|
5
|
|
|
* |
|
6
|
|
|
* Javascript and CSS codes generated by a Jaxon plugin. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2025 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\Plugin\Code; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Plugin\CodeGeneratorInterface; |
|
18
|
|
|
use Jaxon\Plugin\CssCodeGeneratorInterface; |
|
19
|
|
|
use Jaxon\Plugin\JsCodeGeneratorInterface; |
|
20
|
|
|
use Closure; |
|
21
|
|
|
|
|
22
|
|
|
use function is_array; |
|
23
|
|
|
use function is_string; |
|
24
|
|
|
|
|
25
|
|
|
class Code |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $aCssTags = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $aCssCodes = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $aJsTags = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $aJsCodes = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $aJsCodesBefore = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $aJsCodesAfter = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected CONST TRIM = " \t\r\n"; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function cssTags(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->aCssTags; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $sTag |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public function addCssTag(string $sTag): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->aCssTags[] = $sTag; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function cssCodes(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->aCssCodes; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function jsTags(): array |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->aJsTags; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $sTag |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function addJsTag(string $sTag): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->aJsTags[] = $sTag; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function jsCodes(): array |
|
110
|
|
|
{ |
|
111
|
|
|
return [ |
|
112
|
|
|
...$this->aJsCodesBefore, |
|
113
|
|
|
...$this->aJsCodes, |
|
114
|
|
|
...$this->aJsCodesAfter, |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param CodeGeneratorInterface $xCodeGenerator |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
public function mergeCode(CodeGeneratorInterface $xGenerator, bool $bIncludeAssets): void |
|
124
|
|
|
{ |
|
125
|
|
|
if($bIncludeAssets) |
|
126
|
|
|
{ |
|
127
|
|
|
// HTML tags for CSS |
|
128
|
|
|
if(($sCssTags = trim($xGenerator->getCss(), self::TRIM)) !== '') |
|
129
|
|
|
{ |
|
130
|
|
|
$this->aCssTags[] = $sCssTags; |
|
131
|
|
|
} |
|
132
|
|
|
// HTML tags for js |
|
133
|
|
|
if(($sJsTags = trim($xGenerator->getJs(), self::TRIM)) !== '') |
|
134
|
|
|
{ |
|
135
|
|
|
$this->aJsTags[] = $sJsTags; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
// Javascript code |
|
139
|
|
|
if(($sJsScript = trim($xGenerator->getScript(), self::TRIM)) !== '') |
|
140
|
|
|
{ |
|
141
|
|
|
$this->aJsCodes[] = $sJsScript; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param CodeGeneratorInterface $xCodeGenerator |
|
147
|
|
|
* @param Closure $renderTag |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function mergeCssCode(CssCodeGeneratorInterface $xGenerator, |
|
152
|
|
|
Closure $renderTag, bool $bIncludeAssets): void |
|
153
|
|
|
{ |
|
154
|
|
|
$xCode = $xGenerator->getCssCode(); |
|
155
|
|
|
if($bIncludeAssets) |
|
156
|
|
|
{ |
|
157
|
|
|
// HTML tags for CSS |
|
158
|
|
|
foreach($xCode->urls() as $xUrl) |
|
159
|
|
|
{ |
|
160
|
|
|
$aCssFile = match(true) { |
|
161
|
|
|
is_string($xUrl) => ['uri' => $xUrl, 'options' => []], |
|
162
|
|
|
is_array($xUrl) => $xUrl, |
|
163
|
|
|
default => null, |
|
164
|
|
|
}; |
|
165
|
|
|
if($aCssFile !== null) |
|
166
|
|
|
{ |
|
167
|
|
|
// Save the HTML tag for the file. |
|
168
|
|
|
$this->aCssTags[] = $renderTag($aCssFile); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
// CSS code |
|
173
|
|
|
if(($aCssCode = trim($xCode->code(), self::TRIM)) !== '') |
|
174
|
|
|
{ |
|
175
|
|
|
$this->aCssCodes[] = $aCssCode; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param CodeGeneratorInterface $xCodeGenerator |
|
181
|
|
|
* @param Closure $renderTag |
|
182
|
|
|
* |
|
183
|
|
|
* @return void |
|
184
|
|
|
*/ |
|
185
|
|
|
public function mergeJsCode(JsCodeGeneratorInterface $xGenerator, |
|
186
|
|
|
Closure $renderTag, bool $bIncludeAssets): void |
|
187
|
|
|
{ |
|
188
|
|
|
$xCode = $xGenerator->getJsCode(); |
|
189
|
|
|
if($bIncludeAssets) |
|
190
|
|
|
{ |
|
191
|
|
|
// HTML tags for js |
|
192
|
|
|
foreach($xCode->urls() as $xUrl) |
|
193
|
|
|
{ |
|
194
|
|
|
$aJsFile = match(true) { |
|
195
|
|
|
is_string($xUrl) => ['uri' => $xUrl, 'options' => []], |
|
196
|
|
|
is_array($xUrl) => $xUrl, |
|
197
|
|
|
default => null, |
|
198
|
|
|
}; |
|
199
|
|
|
if($aJsFile !== null) |
|
200
|
|
|
{ |
|
201
|
|
|
// Save the HTML tag for the file. |
|
202
|
|
|
$this->aJsTags[] = $renderTag($aJsFile); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
// Javascript codes |
|
207
|
|
|
if(($aJsCode = trim($xCode->code(), self::TRIM)) !== '') |
|
208
|
|
|
{ |
|
209
|
|
|
$this->aJsCodes[] = $aJsCode; |
|
210
|
|
|
} |
|
211
|
|
|
// Javascript codes before the main code |
|
212
|
|
|
if(($aJsCode = trim($xCode->before(), self::TRIM)) !== '') |
|
213
|
|
|
{ |
|
214
|
|
|
$this->aJsCodesBefore[] = $aJsCode; |
|
215
|
|
|
} |
|
216
|
|
|
// Javascript codes after the main code |
|
217
|
|
|
if(($aJsCode = trim($xCode->after(), self::TRIM)) !== '') |
|
218
|
|
|
{ |
|
219
|
|
|
$this->aJsCodesAfter[] = $aJsCode; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|