|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Plugin\Code; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Plugin\Plugin; |
|
6
|
|
|
use Jaxon\Utils\Config\Config; |
|
7
|
|
|
use Jaxon\Utils\File\Minifier; |
|
8
|
|
|
use Jaxon\Utils\Http\UriDetector; |
|
9
|
|
|
use Jaxon\Utils\Http\UriException; |
|
10
|
|
|
|
|
11
|
|
|
use function rtrim; |
|
12
|
|
|
use function is_file; |
|
13
|
|
|
use function file_put_contents; |
|
14
|
|
|
|
|
15
|
|
|
class AssetManager |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Config |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $xConfig; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var UriDetector |
|
24
|
|
|
*/ |
|
25
|
|
|
private $xUriDetector; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Minifier |
|
29
|
|
|
*/ |
|
30
|
|
|
private $xMinifier; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var bool |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
protected $bIncludeAllAssets; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Default library URL |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @param Config $xConfig |
|
|
|
|
|
|
48
|
|
|
* @param UriDetector $xUriDetector |
|
|
|
|
|
|
49
|
|
|
* @param Minifier $xMinifier |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Config $xConfig, UriDetector $xUriDetector, Minifier $xMinifier) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$this->xConfig = $xConfig; |
|
|
|
|
|
|
54
|
|
|
$this->xUriDetector = $xUriDetector; |
|
|
|
|
|
|
55
|
|
|
$this->xMinifier = $xMinifier; |
|
|
|
|
|
|
56
|
|
|
$this->bIncludeAllAssets = $xConfig->getOption('assets.include.all', true); |
|
57
|
|
|
} |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get js options |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getJsOptions(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->xConfig->getOption('js.app.options', ''); |
|
67
|
|
|
} |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if the assets of this plugin shall be included in Jaxon generated code. |
|
71
|
|
|
* |
|
72
|
|
|
* @param Plugin $xPlugin |
|
|
|
|
|
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function shallIncludeAssets(Plugin $xPlugin): bool |
|
77
|
|
|
{ |
|
78
|
|
|
if($this->bIncludeAllAssets) |
|
79
|
|
|
{ |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
$sPluginOptionName = 'assets.include.' . $xPlugin->getName(); |
|
83
|
|
|
return $this->xConfig->getOption($sPluginOptionName, true); |
|
84
|
|
|
} |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getJsLibFiles(): array |
|
92
|
|
|
{ |
|
93
|
|
|
$sJsExtension = $this->xConfig->getOption('js.app.minify') ? '.min.js' : '.js'; |
|
94
|
|
|
// The URI for the javascript library files |
|
95
|
|
|
$sJsLibUri = rtrim($this->xConfig->getOption('js.lib.uri', self::JS_LIB_URL), '/') . '/'; |
|
96
|
|
|
// Add component files to the javascript file array; |
|
97
|
|
|
$aJsFiles = [$sJsLibUri . 'jaxon.core' . $sJsExtension]; |
|
98
|
|
|
if($this->xConfig->getOption('core.debug.on')) |
|
99
|
|
|
{ |
|
100
|
|
|
$sLanguage = $this->xConfig->getOption('core.language'); |
|
101
|
|
|
$aJsFiles[] = $sJsLibUri . 'jaxon.debug' . $sJsExtension; |
|
102
|
|
|
$aJsFiles[] = $sJsLibUri . 'lang/jaxon.' . $sLanguage . $sJsExtension; |
|
103
|
|
|
} |
|
104
|
|
|
return $aJsFiles; |
|
105
|
|
|
} |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the mappings between previous and current config options |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
* @throws UriException |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getOptionVars(): array |
|
114
|
|
|
{ |
|
115
|
|
|
if(!$this->xConfig->hasOption('core.request.uri')) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->xConfig->setOption('core.request.uri', $this->xUriDetector->detect($_SERVER)); |
|
118
|
|
|
} |
|
119
|
|
|
return [ |
|
120
|
|
|
'sResponseType' => 'JSON', |
|
121
|
|
|
'sVersion' => $this->xConfig->getOption('core.version'), |
|
122
|
|
|
'sLanguage' => $this->xConfig->getOption('core.language'), |
|
123
|
|
|
'bLanguage' => $this->xConfig->hasOption('core.language'), |
|
124
|
|
|
'sRequestURI' => $this->xConfig->getOption('core.request.uri'), |
|
125
|
|
|
'sDefaultMode' => $this->xConfig->getOption('core.request.mode'), |
|
126
|
|
|
'sDefaultMethod' => $this->xConfig->getOption('core.request.method'), |
|
127
|
|
|
'sCsrfMetaName' => $this->xConfig->getOption('core.request.csrf_meta'), |
|
128
|
|
|
'bDebug' => $this->xConfig->getOption('core.debug.on'), |
|
129
|
|
|
'bVerboseDebug' => $this->xConfig->getOption('core.debug.verbose'), |
|
130
|
|
|
'sDebugOutputID' => $this->xConfig->getOption('core.debug.output_id'), |
|
131
|
|
|
'nResponseQueueSize' => $this->xConfig->getOption('js.lib.queue_size'), |
|
132
|
|
|
'sStatusMessages' => $this->xConfig->getOption('js.lib.show_status') ? 'true' : 'false', |
|
133
|
|
|
'sWaitCursor' => $this->xConfig->getOption('js.lib.show_cursor') ? 'true' : 'false', |
|
134
|
|
|
'sDefer' => $this->xConfig->getOption('js.app.options', ''), |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get the javascript file name |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function shallCreateJsFiles(): bool |
|
144
|
|
|
{ |
|
145
|
|
|
// Check config options |
|
146
|
|
|
// - The js.app.export option must be set to true |
|
147
|
|
|
// - The js.app.uri and js.app.dir options must be set to non null values |
|
148
|
|
|
if(!$this->xConfig->getOption('js.app.export', false) || |
|
149
|
|
|
!$this->xConfig->getOption('js.app.uri', false) || |
|
150
|
|
|
!$this->xConfig->getOption('js.app.dir', false)) |
|
151
|
|
|
{ |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
return true; |
|
155
|
|
|
} |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Write javascript files and return the corresponding URI |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $sHash |
|
|
|
|
|
|
161
|
|
|
* @param string $sJsCode |
|
|
|
|
|
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function createJsFiles(string $sHash, string $sJsCode): string |
|
166
|
|
|
{ |
|
167
|
|
|
// Check dir access |
|
168
|
|
|
$sJsFileName = $this->xConfig->getOption('js.app.file', $sHash); |
|
169
|
|
|
$sJsDirectory = rtrim($this->xConfig->getOption('js.app.dir'), '/') . '/'; |
|
|
|
|
|
|
170
|
|
|
// - The js.app.dir must be writable |
|
171
|
|
|
if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory)) |
|
172
|
|
|
{ |
|
173
|
|
|
return ''; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$sOutFile = $sJsFileName . '.js'; |
|
177
|
|
|
$sMinFile = $sJsFileName . '.min.js'; |
|
178
|
|
|
if(!is_file($sJsDirectory . $sOutFile)) |
|
179
|
|
|
{ |
|
180
|
|
|
if(!file_put_contents($sJsDirectory . $sOutFile, $sJsCode)) |
|
181
|
|
|
{ |
|
182
|
|
|
return ''; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
if(($this->xConfig->getOption('js.app.minify')) && !is_file($sJsDirectory . $sMinFile)) |
|
186
|
|
|
{ |
|
187
|
|
|
if(!$this->xMinifier->minify($sJsDirectory . $sOutFile, $sJsDirectory . $sMinFile)) |
|
188
|
|
|
{ |
|
189
|
|
|
return ''; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$sJsAppUri = rtrim($this->xConfig->getOption('js.app.uri'), '/') . '/'; |
|
|
|
|
|
|
194
|
|
|
$sJsExtension = $this->xConfig->getOption('js.app.minify') ? '.min.js' : '.js'; |
|
195
|
|
|
return $sJsAppUri . $sJsFileName . $sJsExtension; |
|
196
|
|
|
} |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|