|
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
|
|
|
* @param string $sUri |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
private function getCssTag(string $sUri): string |
|
160
|
|
|
{ |
|
161
|
|
|
return '<link rel="stylesheet" href="' . $sUri . '" />'; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get the CSS HTML header code |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $aFiles The CSS files |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getCss(array $aFiles): string |
|
172
|
|
|
{ |
|
173
|
|
|
$aFiles = $this->getUris('assets.css', $aFiles); |
|
174
|
|
|
$aFiles = array_map(fn($sUri) => $this->getCssTag($sUri), $aFiles); |
|
175
|
|
|
return implode("\n", $aFiles); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get the library script code |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
|
|
184
|
|
|
public function getScript(): string |
|
185
|
|
|
{ |
|
186
|
|
|
return !$this->getLibOption('js.app.export', false) ? |
|
187
|
|
|
$this->xTemplateEngine->render("jaxon::dialogs::{$this->sName}.js") : ''; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get the javascript HTML header code |
|
192
|
|
|
* |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getFiles(): array |
|
196
|
|
|
{ |
|
197
|
|
|
if(!$this->getLibOption('js.app.export', false)) |
|
198
|
|
|
{ |
|
199
|
|
|
return []; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$sJsFileName = !$this->getLibOption('js.app.minify', false) ? |
|
203
|
|
|
"{$this->sName}.js" : "{$this->sName}.min.js"; |
|
204
|
|
|
return [self::JS_LIB_URL . "/$sJsFileName"]; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|