|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Generator.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\Plugin\Code\Contracts\Generator as GeneratorContract; |
|
18
|
|
|
use Jaxon\Utils\Template\Engine as TemplateEngine; |
|
19
|
|
|
use Jaxon\Utils\Http\URI; |
|
20
|
|
|
|
|
21
|
|
|
class Generator |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
use \Jaxon\Features\Config; |
|
24
|
|
|
use \Jaxon\Features\Minifier; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Default library URL |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The objects that generate code |
|
35
|
|
|
* |
|
36
|
|
|
* @var array<GeneratorContract> |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $aGenerators = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The Jaxon template engine |
|
42
|
|
|
* |
|
43
|
|
|
* @var TemplateEngine |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $xTemplateEngine; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The constructor |
|
49
|
|
|
* |
|
50
|
|
|
* @param TemplateEngine $xTemplateEngine The template engine |
|
|
|
|
|
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(TemplateEngine $xTemplateEngine) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$this->xTemplateEngine = $xTemplateEngine; |
|
55
|
|
|
} |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the correspondances between previous and current config options |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getOptionVars() |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
|
|
'sResponseType' => 'JSON', |
|
66
|
|
|
'sVersion' => $this->getOption('core.version'), |
|
67
|
|
|
'sLanguage' => $this->getOption('core.language'), |
|
68
|
|
|
'bLanguage' => $this->hasOption('core.language') ? true : false, |
|
69
|
|
|
'sRequestURI' => $this->getOption('core.request.uri'), |
|
70
|
|
|
'sDefaultMode' => $this->getOption('core.request.mode'), |
|
71
|
|
|
'sDefaultMethod' => $this->getOption('core.request.method'), |
|
72
|
|
|
'sCsrfMetaName' => $this->getOption('core.request.csrf_meta'), |
|
73
|
|
|
'bDebug' => $this->getOption('core.debug.on'), |
|
74
|
|
|
'bVerboseDebug' => $this->getOption('core.debug.verbose'), |
|
75
|
|
|
'sDebugOutputID' => $this->getOption('core.debug.output_id'), |
|
76
|
|
|
'nResponseQueueSize' => $this->getOption('js.lib.queue_size'), |
|
77
|
|
|
'sStatusMessages' => $this->getOption('js.lib.show_status') ? 'true' : 'false', |
|
78
|
|
|
'sWaitCursor' => $this->getOption('js.lib.show_cursor') ? 'true' : 'false', |
|
79
|
|
|
'sDefer' => $this->getOption('js.app.options', ''), |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Render a template in the 'plugins' subdir |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $sTemplate The template filename |
|
|
|
|
|
|
87
|
|
|
* @param array $aVars The template variables |
|
|
|
|
|
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
private function _render($sTemplate, array $aVars = []) |
|
92
|
|
|
{ |
|
93
|
|
|
$aVars['sJsOptions'] = $this->getOption('js.app.options', ''); |
|
94
|
|
|
return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars); |
|
95
|
|
|
} |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add a new generator to the list |
|
99
|
|
|
* |
|
100
|
|
|
* @param GeneratorContract $xGenerator The code generator |
|
|
|
|
|
|
101
|
|
|
* @param integer $nPriority The desired priority, used to order the plugins |
|
|
|
|
|
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function addGenerator(GeneratorContract $xGenerator, $nPriority) |
|
106
|
|
|
{ |
|
107
|
|
|
while(isset($this->aGenerators[$nPriority])) |
|
108
|
|
|
{ |
|
109
|
|
|
$nPriority++; |
|
110
|
|
|
} |
|
111
|
|
|
$this->aGenerators[$nPriority] = $xGenerator; |
|
112
|
|
|
// Sort the array by ascending keys |
|
113
|
|
|
ksort($this->aGenerators); |
|
114
|
|
|
} |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Generate a hash for all the javascript code generated by the library |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
private function getHash() |
|
122
|
|
|
{ |
|
123
|
|
|
$sHash = jaxon()->getVersion(); |
|
124
|
|
|
foreach($this->aGenerators as $xGenerator) |
|
125
|
|
|
{ |
|
126
|
|
|
$sHash .= $xGenerator->getHash(); |
|
127
|
|
|
} |
|
128
|
|
|
return md5($sHash); |
|
129
|
|
|
} |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get the HTML tags to include Jaxon CSS code and files into the page |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getCss() |
|
137
|
|
|
{ |
|
138
|
|
|
$sCssCode = ''; |
|
139
|
|
|
foreach($this->aGenerators as $xGenerator) |
|
140
|
|
|
{ |
|
141
|
|
|
$sCssCode = rtrim($sCssCode, " \n") . "\n" . $xGenerator->getCss(); |
|
142
|
|
|
} |
|
143
|
|
|
return rtrim($sCssCode, " \n") . "\n"; |
|
144
|
|
|
} |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getJs() |
|
152
|
|
|
{ |
|
153
|
|
|
$sJsExtension = $this->getOption('js.app.minify') ? '.min.js' : '.js'; |
|
154
|
|
|
|
|
155
|
|
|
// The URI for the javascript library files |
|
156
|
|
|
$sJsLibUri = rtrim($this->getOption('js.lib.uri', self::JS_LIB_URL), '/') . '/'; |
|
157
|
|
|
// Add component files to the javascript file array; |
|
158
|
|
|
$aJsFiles = [$sJsLibUri . 'jaxon.core' . $sJsExtension]; |
|
159
|
|
|
if($this->getOption('core.debug.on')) |
|
160
|
|
|
{ |
|
161
|
|
|
$sLanguage = $this->getOption('core.language'); |
|
162
|
|
|
$aJsFiles[] = $sJsLibUri . 'jaxon.debug' . $sJsExtension; |
|
163
|
|
|
$aJsFiles[] = $sJsLibUri . 'lang/jaxon.' . $sLanguage . $sJsExtension; |
|
164
|
|
|
/*if($this->getOption('core.debug.verbose')) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$aJsFiles[] = $sJsLibUri . 'jaxon.verbose' . $sJsExtension; |
|
167
|
|
|
}*/ |
|
168
|
|
|
} |
|
169
|
|
|
$sJsFiles = $this->_render('includes.js', ['aUrls' => $aJsFiles]); |
|
170
|
|
|
|
|
171
|
|
|
$sJsCode = ''; |
|
172
|
|
|
foreach($this->aGenerators as $xGenerator) |
|
173
|
|
|
{ |
|
174
|
|
|
$sJsCode = rtrim($sJsCode, " \n") . "\n" . $xGenerator->getJs(); |
|
175
|
|
|
} |
|
176
|
|
|
return $sJsFiles . "\n" . rtrim($sJsCode, " \n") . "\n"; |
|
177
|
|
|
} |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the javascript code to be sent to the browser |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
private function _getScript() |
|
185
|
|
|
{ |
|
186
|
|
|
$sScript = ''; |
|
|
|
|
|
|
187
|
|
|
$sReadyScript = ''; |
|
188
|
|
|
foreach($this->aGenerators as $xGenerator) |
|
189
|
|
|
{ |
|
190
|
|
|
$sScript .= rtrim($xGenerator->getScript(), " \n") . "\n"; |
|
191
|
|
|
if($xGenerator->readyEnabled() && !$xGenerator->readyInlined()) |
|
192
|
|
|
{ |
|
193
|
|
|
// Ready code which can nbe exported to an external file. |
|
194
|
|
|
$sReadyScript .= rtrim($xGenerator->getReadyScript(), " \n") . "\n"; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
// These three parts are always rendered together |
|
199
|
|
|
$aConfigVars = $this->getOptionVars(); |
|
200
|
|
|
return $this->_render('config.js', $aConfigVars) . "\n" . $sScript . "\n" . |
|
201
|
|
|
$this->_render('ready.js', ['sScript' => $sReadyScript]); |
|
202
|
|
|
} |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Get the javascript code to include directly in HTML |
|
206
|
|
|
* |
|
207
|
|
|
* @return string |
|
208
|
|
|
*/ |
|
209
|
|
|
private function _getInlineScript() |
|
210
|
|
|
{ |
|
211
|
|
|
$sScript = ''; |
|
212
|
|
|
foreach($this->aGenerators as $xGenerator) |
|
213
|
|
|
{ |
|
214
|
|
|
if($xGenerator->readyEnabled() && $xGenerator->readyInlined()) |
|
215
|
|
|
{ |
|
216
|
|
|
// Ready code which must be inlined in HTML. |
|
217
|
|
|
$sScript .= rtrim($xGenerator->getReadyScript(), " \n") . "\n"; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
return $this->_render('ready.js', ['sScript' => $sScript]); |
|
221
|
|
|
} |
|
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get the javascript file name |
|
225
|
|
|
* |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
|
|
private function getJsFileName() |
|
229
|
|
|
{ |
|
230
|
|
|
// Check config options |
|
231
|
|
|
// - The js.app.export option must be set to true |
|
232
|
|
|
// - The js.app.uri and js.app.dir options must be set to non null values |
|
233
|
|
|
if(!$this->getOption('js.app.export') || |
|
234
|
|
|
!$this->getOption('js.app.uri') || |
|
235
|
|
|
!$this->getOption('js.app.dir')) |
|
236
|
|
|
{ |
|
237
|
|
|
return ''; |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// The file name |
|
241
|
|
|
return $this->hasOption('js.app.file') ? $this->getOption('js.app.file') : $this->getHash(); |
|
|
|
|
|
|
242
|
|
|
} |
|
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
|
|
|
|
|
245
|
|
|
* Write javascript files and return the corresponding URI |
|
246
|
|
|
* |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
public function createFiles($sJsDirectory, $sJsFileName) |
|
250
|
|
|
{ |
|
251
|
|
|
// Check dir access |
|
252
|
|
|
// - The js.app.dir must be writable |
|
253
|
|
|
if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
|
254
|
|
|
{ |
|
255
|
|
|
return ''; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$sOutFile = $sJsFileName . '.js'; |
|
259
|
|
|
$sMinFile = $sJsFileName . '.min.js'; |
|
260
|
|
|
if(!is_file($sJsDirectory . $sOutFile)) |
|
261
|
|
|
{ |
|
262
|
|
|
if(!file_put_contents($sJsDirectory . $sOutFile, $this->_getScript())) |
|
263
|
|
|
{ |
|
264
|
|
|
return ''; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
if(($this->getOption('js.app.minify')) && !is_file($sJsDirectory . $sMinFile)) |
|
268
|
|
|
{ |
|
269
|
|
|
if(!$this->minify($sJsDirectory . $sOutFile, $sJsDirectory . $sMinFile)) |
|
270
|
|
|
{ |
|
271
|
|
|
return ''; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$sJsAppUri = rtrim($this->getOption('js.app.uri'), '/') . '/'; |
|
|
|
|
|
|
276
|
|
|
$sJsExtension = $this->getOption('js.app.minify') ? '.min.js' : '.js'; |
|
277
|
|
|
return $sJsAppUri . $sJsFileName . $sJsExtension; |
|
278
|
|
|
} |
|
|
|
|
|
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Get the javascript code to be sent to the browser |
|
282
|
|
|
* |
|
283
|
|
|
* @param boolean $bIncludeJs Also get the JS files |
|
|
|
|
|
|
284
|
|
|
* @param boolean $bIncludeCss Also get the CSS files |
|
|
|
|
|
|
285
|
|
|
* |
|
286
|
|
|
* @return string |
|
287
|
|
|
*/ |
|
288
|
|
|
public function getScript($bIncludeJs, $bIncludeCss) |
|
289
|
|
|
{ |
|
290
|
|
|
if(!$this->getOption('core.request.uri')) |
|
291
|
|
|
{ |
|
292
|
|
|
$this->setOption('core.request.uri', jaxon()->di()->get(URI::class)->detect()); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
$sScript = ''; |
|
296
|
|
|
if(($bIncludeCss)) |
|
297
|
|
|
{ |
|
298
|
|
|
$sScript .= $this->getCss() . "\n"; |
|
299
|
|
|
} |
|
300
|
|
|
if(($bIncludeJs)) |
|
301
|
|
|
{ |
|
302
|
|
|
$sScript .= $this->getJs() . "\n"; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$sJsDirectory = rtrim($this->getOption('js.app.dir'), '/') . '/'; |
|
306
|
|
|
$sUrl = $this->createFiles($sJsDirectory, $this->getJsFileName()); |
|
|
|
|
|
|
307
|
|
|
if(($sUrl)) |
|
308
|
|
|
{ |
|
309
|
|
|
return $sScript . $this->_render('include.js', ['sUrl' => $sUrl]) . "\n" . |
|
310
|
|
|
$this->_render('wrapper.js', ['sScript' => $this->_getInlineScript()]); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $sScript . $this->_render('wrapper.js', [ |
|
314
|
|
|
'sScript' => $this->_getScript() . "\n" . $this->_getInlineScript() |
|
315
|
|
|
]); |
|
316
|
|
|
} |
|
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
|