1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jaxon\Dialogs\Dialog; |
4
|
|
|
|
5
|
|
|
use Jaxon\App\Config\ConfigManager; |
6
|
|
|
use Jaxon\App\Config\ConfigTrait; |
7
|
|
|
use Jaxon\App\Dialog\Library\LibraryInterface; |
8
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
9
|
|
|
|
10
|
|
|
use function array_filter; |
11
|
|
|
use function array_map; |
12
|
|
|
use function implode; |
13
|
|
|
use function is_array; |
14
|
|
|
use function rtrim; |
15
|
|
|
use function trim; |
16
|
|
|
|
17
|
|
|
class LibraryHelper |
18
|
|
|
{ |
19
|
|
|
use ConfigTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Default library URL |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/jaxon-dialogs@main/js'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The name of the library |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $sName = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The URI where to get the library files from |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $sUri = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The constructor |
44
|
|
|
* |
45
|
|
|
* @param LibraryInterface $xDialogLibrary |
46
|
|
|
* @param ConfigManager $xConfigManager |
47
|
|
|
* @param TemplateEngine $xTemplateEngine |
48
|
|
|
*/ |
49
|
|
|
public function __construct(LibraryInterface $xDialogLibrary, |
50
|
|
|
private ConfigManager $xConfigManager, private TemplateEngine $xTemplateEngine) |
51
|
|
|
{ |
52
|
|
|
// Set the library name |
53
|
|
|
$this->sName = $xDialogLibrary->getName(); |
54
|
|
|
// Set the default URI. |
55
|
|
|
$sDefaultUri = $this->getAppOption('dialogs.lib.uri', $xDialogLibrary->getUri()); |
56
|
|
|
// Set the library URI. |
57
|
|
|
$this->sUri = rtrim($this->getOption('uri', $sDefaultUri), '/'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return ConfigManager |
62
|
|
|
*/ |
63
|
|
|
protected function config(): ConfigManager |
64
|
|
|
{ |
65
|
|
|
return $this->xConfigManager; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the value of a config option |
70
|
|
|
* |
71
|
|
|
* @param string $sOptionName The option name |
72
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function getOption(string $sOptionName, $xDefault = null) |
77
|
|
|
{ |
78
|
|
|
$sOptionName = "dialogs.{$this->sName}.$sOptionName"; |
79
|
|
|
return $this->getAppOption($sOptionName, $xDefault); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check the presence of a config option |
84
|
|
|
* |
85
|
|
|
* @param string $sOptionName The option name |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasOption(string $sOptionName): bool |
90
|
|
|
{ |
91
|
|
|
return $this->hasAppOption("dialogs.{$this->sName}.$sOptionName"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the options of the js library |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function getJsOptions(): array |
100
|
|
|
{ |
101
|
|
|
$xOptions = $this->getAppOption("dialogs.{$this->sName}.options", []); |
102
|
|
|
return is_array($xOptions) ? $xOptions : []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $sOption The assets option name |
107
|
|
|
* @param string $sFile The javascript file name |
108
|
|
|
* |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
|
|
private function getAssetUri(string $sOption, string $sFile): ?string |
112
|
|
|
{ |
113
|
|
|
return !$this->hasOption($sOption) ? "{$this->sUri}/$sFile" : |
114
|
|
|
(trim($this->getOption($sOption)) ?: null); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $sOption The assets option name |
119
|
|
|
* @param array $aFiles The asset files |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
private function getUris(string $sOption, array $aFiles): array |
124
|
|
|
{ |
125
|
|
|
$aFiles = array_map(fn($sFile) => |
126
|
|
|
$this->getAssetUri($sOption, $sFile), $aFiles); |
127
|
|
|
return array_filter($aFiles, fn($sFile) => $sFile !== null); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $sUri |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function getJsTag(string $sUri): string |
136
|
|
|
{ |
137
|
|
|
return '<script type="text/javascript" src="' . $sUri . '"></script>'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the javascript HTML header code |
142
|
|
|
* |
143
|
|
|
* @param array $aFiles The js files |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getJs(array $aFiles): string |
148
|
|
|
{ |
149
|
|
|
$aFiles = $this->getUris('assets.js', $aFiles); |
150
|
|
|
$aFiles = array_map(fn($sUri) => $this->getJsTag($sUri), $aFiles); |
151
|
|
|
return implode("\n", $aFiles); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the javascript HTML header code |
156
|
|
|
* |
157
|
|
|
* @param string $sFile The javascript file name |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getJsHtml(string $sFile): string |
162
|
|
|
{ |
163
|
|
|
// If this 'assets.js' option is defined and evaluates to false, then the asset is not displayed. |
164
|
|
|
$sUri = $this->getAssetUri('assets.js', $sFile); |
165
|
|
|
return !$sUri ? '' : $this->getJsTag($sUri); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $sUri |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
private function getCssTag(string $sUri): string |
174
|
|
|
{ |
175
|
|
|
return '<link rel="stylesheet" href="' . $sUri . '" />'; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the CSS HTML header code |
180
|
|
|
* |
181
|
|
|
* @param array $aFiles The CSS files |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getCss(array $aFiles): string |
186
|
|
|
{ |
187
|
|
|
$aFiles = $this->getUris('assets.css', $aFiles); |
188
|
|
|
$aFiles = array_map(fn($sUri) => $this->getCssTag($sUri), $aFiles); |
189
|
|
|
return implode("\n", $aFiles); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the CSS HTML header code |
194
|
|
|
* |
195
|
|
|
* @param string $sFile The CSS file name |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function getCssHtml(string $sFile): string |
200
|
|
|
{ |
201
|
|
|
// If this 'assets.css' option is defined and evaluates to false, then the asset is not displayed. |
202
|
|
|
$sUri = $this->getAssetUri('assets.css', $sFile); |
203
|
|
|
return !$sUri ? '' : $this->getCssTag($sUri); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the library script code |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
|
212
|
|
|
public function getScript(): string |
213
|
|
|
{ |
214
|
|
|
return !$this->getLibOption('js.app.export', false) ? |
215
|
|
|
$this->xTemplateEngine->render("jaxon::dialogs::{$this->sName}.js") : ''; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get the javascript HTML header code |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function getFiles(): array |
224
|
|
|
{ |
225
|
|
|
if(!$this->getLibOption('js.app.export', false)) |
226
|
|
|
{ |
227
|
|
|
return []; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$sJsFileName = !$this->getLibOption('js.app.minify', false) ? |
231
|
|
|
"{$this->sName}.js" : "{$this->sName}.min.js"; |
232
|
|
|
return [self::JS_LIB_URL . "/$sJsFileName"]; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|