|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CodeGenerator.php - Jaxon code generator |
|
5
|
|
|
* |
|
6
|
|
|
* Generate HTML, CSS and Javascript code for Jaxon. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2016 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\Di\Container; |
|
18
|
|
|
use Jaxon\Plugin\Manager\PluginManager; |
|
19
|
|
|
use Jaxon\Plugin\Plugin; |
|
20
|
|
|
use Jaxon\Utils\Http\UriException; |
|
21
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
|
22
|
|
|
|
|
23
|
|
|
use function array_reduce; |
|
24
|
|
|
use function md5; |
|
25
|
|
|
use function trim; |
|
26
|
|
|
use function is_subclass_of; |
|
27
|
|
|
|
|
28
|
|
|
class CodeGenerator |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $sVersion; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Container |
|
37
|
|
|
*/ |
|
38
|
|
|
private $di; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var PluginManager |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $xPluginManager; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The Jaxon template engine |
|
47
|
|
|
* |
|
48
|
|
|
* @var TemplateEngine |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $xTemplateEngine; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var AssetManager |
|
54
|
|
|
*/ |
|
55
|
|
|
private $xAssetManager; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $sJsOptions; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $sCss = ''; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $sJs = ''; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $sJsScript = ''; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $sJsReadyScript = ''; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $sJsInlineScript = ''; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $bGenerated = false; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* The constructor |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $sVersion |
|
|
|
|
|
|
96
|
|
|
* @param Container $di |
|
|
|
|
|
|
97
|
|
|
* @param PluginManager $xPluginManager |
|
|
|
|
|
|
98
|
|
|
* @param TemplateEngine $xTemplateEngine |
|
|
|
|
|
|
99
|
|
|
*/ |
|
100
|
|
|
public function __construct(string $sVersion, Container $di, PluginManager $xPluginManager, TemplateEngine $xTemplateEngine) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$this->sVersion = $sVersion; |
|
|
|
|
|
|
103
|
|
|
$this->di = $di; |
|
|
|
|
|
|
104
|
|
|
$this->xPluginManager = $xPluginManager; |
|
105
|
|
|
$this->xTemplateEngine = $xTemplateEngine; |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Generate a hash for all the javascript code generated by the library |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getHash(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return md5(array_reduce($this->xPluginManager->getCodeGenerators(), function($sHash, $sClassName) { |
|
116
|
|
|
return $sHash . $this->di->g($sClassName)->getHash(); |
|
117
|
|
|
}, $this->sVersion)); |
|
118
|
|
|
} |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Render a template in the 'plugins' subdir |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $sTemplate The template filename |
|
|
|
|
|
|
124
|
|
|
* @param array $aVars The template variables |
|
|
|
|
|
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
private function render(string $sTemplate, array $aVars = []): string |
|
129
|
|
|
{ |
|
130
|
|
|
$aVars['sJsOptions'] = $this->sJsOptions; |
|
131
|
|
|
return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
|
132
|
|
|
} |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Generate the Jaxon CSS and js codes for a given plugin |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $sClassName |
|
|
|
|
|
|
138
|
|
|
* |
|
139
|
|
|
* @return void |
|
140
|
|
|
*/ |
|
141
|
|
|
private function generatePluginCodes(string $sClassName) |
|
142
|
|
|
{ |
|
143
|
|
|
$xGenerator = $this->di->g($sClassName); |
|
144
|
|
|
if(!is_subclass_of($xGenerator, Plugin::class) || $this->xAssetManager->shallIncludeAssets($xGenerator)) |
|
145
|
|
|
{ |
|
146
|
|
|
// HTML tags for CSS |
|
147
|
|
|
$this->sCss = trim($this->sCss) . "\n" . trim($xGenerator->getCss(), " \n"); |
|
148
|
|
|
// HTML tags for js |
|
149
|
|
|
$this->sJs = trim($this->sJs) . "\n" . trim($xGenerator->getJs(), " \n"); |
|
150
|
|
|
} |
|
151
|
|
|
// Javascript code |
|
152
|
|
|
$this->sJsScript = trim($this->sJsScript) . "\n\n" . trim($xGenerator->getScript(), " \n"); |
|
153
|
|
|
if($xGenerator->readyEnabled()) |
|
154
|
|
|
{ |
|
155
|
|
|
$sScriptAttr = $xGenerator->readyInlined() ? 'sJsInlineScript' : 'sJsReadyScript'; |
|
|
|
|
|
|
156
|
|
|
$this->$sScriptAttr = trim($this->$sScriptAttr) . "\n\n" . trim($xGenerator->getReadyScript(), " \n"); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Render the generated CSS ans js codes |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
private function renderCodes() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->sCss = trim($this->sCss, " \n"); |
|
|
|
|
|
|
168
|
|
|
$this->sJs = trim($this->sJs, " \n"); |
|
|
|
|
|
|
169
|
|
|
$this->sJsScript = trim($this->sJsScript, " \n"); |
|
|
|
|
|
|
170
|
|
|
$this->sJsReadyScript = trim($this->sJsReadyScript, " \n"); |
|
171
|
|
|
$this->sJsInlineScript = trim($this->sJsInlineScript, " \n"); |
|
172
|
|
|
if(($this->sJsReadyScript)) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->sJsReadyScript = $this->render('ready.js', ['sScript' => $this->sJsReadyScript . "\n"]); |
|
175
|
|
|
} |
|
176
|
|
|
if(($this->sJsInlineScript)) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->sJsInlineScript = $this->render('ready.js', ['sScript' => $this->sJsInlineScript . "\n"]); |
|
179
|
|
|
} |
|
180
|
|
|
// Prepend Jaxon javascript files to HTML tags for Js |
|
181
|
|
|
$aJsFiles = $this->xAssetManager->getJsLibFiles(); |
|
182
|
|
|
$this->sJs = trim($this->render('includes.js', ['aUrls' => $aJsFiles])) . "\n\n" . $this->sJs; |
|
183
|
|
|
} |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Generate the Jaxon CSS ans js codes |
|
187
|
|
|
* |
|
188
|
|
|
* @return void |
|
189
|
|
|
*/ |
|
190
|
|
|
private function generateCodes() |
|
191
|
|
|
{ |
|
192
|
|
|
if($this->bGenerated) |
|
193
|
|
|
{ |
|
194
|
|
|
return; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$this->xAssetManager = $this->di->getAssetManager(); |
|
198
|
|
|
$this->sJsOptions = $this->xAssetManager->getJsOptions(); |
|
|
|
|
|
|
199
|
|
|
foreach($this->xPluginManager->getCodeGenerators() as $sClassName) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->generatePluginCodes($sClassName); |
|
202
|
|
|
} |
|
203
|
|
|
$this->renderCodes(); |
|
204
|
|
|
|
|
205
|
|
|
$sJsConfigVars = $this->render('config.js', $this->xAssetManager->getOptionVars()); |
|
206
|
|
|
// These three parts are always rendered together |
|
207
|
|
|
$this->sJsScript = trim($sJsConfigVars) . "\n\n" . |
|
208
|
|
|
trim($this->sJsScript) . "\n\n" . trim($this->sJsReadyScript); |
|
209
|
|
|
|
|
210
|
|
|
// The codes are already generated. |
|
211
|
|
|
$this->bGenerated = true; |
|
|
|
|
|
|
212
|
|
|
} |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get the HTML tags to include Jaxon CSS code and files into the page |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
public function getCss(): string |
|
220
|
|
|
{ |
|
221
|
|
|
$this->generateCodes(); |
|
222
|
|
|
return $this->sCss; |
|
223
|
|
|
} |
|
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page |
|
227
|
|
|
* |
|
228
|
|
|
* @return string |
|
229
|
|
|
*/ |
|
230
|
|
|
public function getJs(): string |
|
231
|
|
|
{ |
|
232
|
|
|
$this->generateCodes(); |
|
233
|
|
|
return $this->sJs; |
|
234
|
|
|
} |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get the generated javascript code |
|
238
|
|
|
* |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getJsScript(): string |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->sJsScript; |
|
244
|
|
|
} |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get the javascript code to be sent to the browser |
|
248
|
|
|
* |
|
249
|
|
|
* @param bool $bIncludeJs Also get the JS files |
|
|
|
|
|
|
250
|
|
|
* @param bool $bIncludeCss Also get the CSS files |
|
251
|
|
|
* |
|
252
|
|
|
* @return string |
|
253
|
|
|
* @throws UriException |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getScript(bool $bIncludeJs, bool $bIncludeCss): string |
|
256
|
|
|
{ |
|
257
|
|
|
$this->generateCodes(); |
|
258
|
|
|
$sScript = ''; |
|
259
|
|
|
if(($bIncludeCss)) |
|
260
|
|
|
{ |
|
261
|
|
|
$sScript .= $this->getCss() . "\n"; |
|
262
|
|
|
} |
|
263
|
|
|
if(($bIncludeJs)) |
|
264
|
|
|
{ |
|
265
|
|
|
$sScript .= $this->getJs() . "\n"; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if(!($sUrl = $this->xAssetManager->createJsFiles($this))) |
|
|
|
|
|
|
269
|
|
|
{ |
|
270
|
|
|
return trim($sScript) . "\n\n" . $this->render('wrapper.js', |
|
271
|
|
|
['sScript' => trim($this->sJsScript) . "\n\n" . trim($this->sJsInlineScript)]); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return trim($sScript) . "\n\n" . trim($this->render('include.js', ['sUrl' => $sUrl])) . |
|
275
|
|
|
"\n\n" . $this->render('wrapper.js', ['sScript' => $this->sJsInlineScript]); |
|
276
|
|
|
} |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|