1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DialogLibraryManager.php |
5
|
|
|
* |
6
|
|
|
* Manage dialog library list and defaults. |
7
|
|
|
* |
8
|
|
|
* @author Thierry Feuzeu <[email protected]> |
9
|
|
|
* @copyright 2019 Thierry Feuzeu <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
11
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace Jaxon\App\Dialog\Library; |
15
|
|
|
|
16
|
|
|
use Jaxon\App\Config\ConfigListenerInterface; |
17
|
|
|
use Jaxon\App\Config\ConfigManager; |
18
|
|
|
use Jaxon\App\Dialog\LibraryInterface; |
19
|
|
|
use Jaxon\App\Dialog\MessageInterface; |
20
|
|
|
use Jaxon\App\Dialog\ModalInterface; |
21
|
|
|
use Jaxon\App\Dialog\QuestionInterface; |
22
|
|
|
use Jaxon\App\I18n\Translator; |
23
|
|
|
use Jaxon\Di\Container; |
24
|
|
|
use Jaxon\Exception\SetupException; |
25
|
|
|
use Jaxon\Utils\Config\Config; |
26
|
|
|
|
27
|
|
|
use function array_map; |
28
|
|
|
use function array_keys; |
29
|
|
|
use function class_implements; |
30
|
|
|
use function in_array; |
31
|
|
|
use function substr; |
32
|
|
|
|
33
|
|
|
class DialogLibraryManager implements ConfigListenerInterface |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Container |
37
|
|
|
*/ |
38
|
|
|
private $di; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $aLibraries = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $aQuestionLibraries = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $aMessageLibraries = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $aModalLibraries = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The QuestionInterface class name |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $sQuestionLibrary = ''; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The MessageInterface class name |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $sMessageLibrary = ''; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The ModalInterface class name |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
private $sModalLibrary = ''; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The name of the library to use for the next call. |
83
|
|
|
* This is used to override the default library. |
84
|
|
|
* |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
protected $sNextLibrary = ''; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var ConfigManager |
91
|
|
|
*/ |
92
|
|
|
protected $xConfigManager; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var Translator |
96
|
|
|
*/ |
97
|
|
|
private $xTranslator; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The constructor |
101
|
|
|
* |
102
|
|
|
* @param Container $di |
|
|
|
|
103
|
|
|
* @param ConfigManager $xConfigManager |
|
|
|
|
104
|
|
|
* @param Translator $xTranslator |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
public function __construct(Container $di, ConfigManager $xConfigManager, Translator $xTranslator) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->di = $di; |
|
|
|
|
109
|
|
|
$this->xConfigManager = $xConfigManager; |
110
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
111
|
|
|
} |
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Register a javascript dialog library adapter. |
115
|
|
|
* |
116
|
|
|
* @param string $sClassName |
|
|
|
|
117
|
|
|
* @param string $sLibraryName |
|
|
|
|
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
* @throws SetupException |
121
|
|
|
*/ |
122
|
|
|
public function registerLibrary(string $sClassName, string $sLibraryName) |
123
|
|
|
{ |
124
|
|
|
$bIsUsed = false; |
|
|
|
|
125
|
|
|
$aInterfaces = class_implements($sClassName); |
126
|
|
|
if(in_array(QuestionInterface::class, $aInterfaces)) |
127
|
|
|
{ |
128
|
|
|
$this->aQuestionLibraries[$sLibraryName] = $bIsUsed = true; |
129
|
|
|
} |
130
|
|
|
if(in_array(MessageInterface::class, $aInterfaces)) |
131
|
|
|
{ |
132
|
|
|
$this->aMessageLibraries[$sLibraryName] = $bIsUsed = true; |
133
|
|
|
} |
134
|
|
|
if(in_array(ModalInterface::class, $aInterfaces)) |
135
|
|
|
{ |
136
|
|
|
$this->aModalLibraries[$sLibraryName] = $bIsUsed = true; |
137
|
|
|
} |
138
|
|
|
if(!$bIsUsed) |
139
|
|
|
{ |
140
|
|
|
// The class is invalid. |
141
|
|
|
$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
142
|
|
|
throw new SetupException($sMessage); |
143
|
|
|
} |
144
|
|
|
// Register the library in the container |
145
|
|
|
$this->di->registerDialogLibrary($sClassName, $sLibraryName); |
146
|
|
|
} |
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the library class instances |
150
|
|
|
* |
151
|
|
|
* @return LibraryInterface[] |
152
|
|
|
*/ |
153
|
|
|
public function getLibraries(): array |
154
|
|
|
{ |
155
|
|
|
return array_map(function($sLibraryName) { |
156
|
|
|
return $this->di->getDialogLibrary($sLibraryName); |
157
|
|
|
}, array_keys($this->aLibraries)); |
158
|
|
|
} |
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set the QuestionInterface library |
162
|
|
|
* |
163
|
|
|
* @param string $sLibraryName The QuestionInterface library name |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
* @throws SetupException |
167
|
|
|
*/ |
168
|
|
|
public function setQuestionLibrary(string $sLibraryName) |
169
|
|
|
{ |
170
|
|
|
if(!isset($this->aQuestionLibraries[$sLibraryName])) |
171
|
|
|
{ |
172
|
|
|
$sMessage = $this->xTranslator->trans('errors.dialog.library', |
173
|
|
|
['type' => 'question', 'name' => $sLibraryName]); |
174
|
|
|
throw new SetupException($sMessage); |
175
|
|
|
} |
176
|
|
|
$this->sQuestionLibrary = $sLibraryName; |
|
|
|
|
177
|
|
|
$this->aLibraries[$sLibraryName] = true; |
178
|
|
|
} |
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the QuestionInterface library |
182
|
|
|
* |
183
|
|
|
* @return QuestionInterface |
184
|
|
|
*/ |
185
|
|
|
public function getQuestionLibrary(): QuestionInterface |
186
|
|
|
{ |
187
|
|
|
return $this->di->getQuestionLibrary($this->sNextLibrary ?: $this->sQuestionLibrary); |
188
|
|
|
} |
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set MessageInterface library |
192
|
|
|
* |
193
|
|
|
* @param string $sLibraryName The MessageInterface library name |
194
|
|
|
* |
195
|
|
|
* @return void |
196
|
|
|
* @throws SetupException |
197
|
|
|
*/ |
198
|
|
|
public function setMessageLibrary(string $sLibraryName) |
199
|
|
|
{ |
200
|
|
|
if(!isset($this->aMessageLibraries[$sLibraryName])) |
201
|
|
|
{ |
202
|
|
|
$sMessage = $this->xTranslator->trans('errors.dialog.library', |
203
|
|
|
['type' => 'message', 'name' => $sLibraryName]); |
204
|
|
|
throw new SetupException($sMessage); |
205
|
|
|
} |
206
|
|
|
$this->sMessageLibrary = $sLibraryName; |
|
|
|
|
207
|
|
|
$this->aLibraries[$sLibraryName] = true; |
208
|
|
|
} |
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the MessageInterface library |
212
|
|
|
* |
213
|
|
|
* @return MessageInterface |
214
|
|
|
*/ |
215
|
|
|
public function getMessageLibrary(): MessageInterface |
216
|
|
|
{ |
217
|
|
|
return $this->di->getMessageLibrary($this->sNextLibrary ?: $this->sMessageLibrary); |
218
|
|
|
} |
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set the ModalInterface library |
222
|
|
|
* |
223
|
|
|
* @param string $sLibraryName The ModalInterface library name |
224
|
|
|
* |
225
|
|
|
* @return void |
226
|
|
|
* @throws SetupException |
227
|
|
|
*/ |
228
|
|
|
public function setModalLibrary(string $sLibraryName) |
229
|
|
|
{ |
230
|
|
|
if(!isset($this->aModalLibraries[$sLibraryName])) |
231
|
|
|
{ |
232
|
|
|
$sMessage = $this->xTranslator->trans('errors.dialog.library', |
233
|
|
|
['type' => 'modal', 'name' => $sLibraryName]); |
234
|
|
|
throw new SetupException($sMessage); |
235
|
|
|
} |
236
|
|
|
$this->sModalLibrary = $sLibraryName; |
|
|
|
|
237
|
|
|
$this->aLibraries[$sLibraryName] = true; |
238
|
|
|
} |
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get the ModalInterface library |
242
|
|
|
* |
243
|
|
|
* @return ModalInterface |
244
|
|
|
*/ |
245
|
|
|
public function getModalLibrary(): ?ModalInterface |
246
|
|
|
{ |
247
|
|
|
return $this->di->getModalLibrary($this->sNextLibrary ?: $this->sModalLibrary); |
248
|
|
|
} |
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set the name of the library to use for the next call |
252
|
|
|
* |
253
|
|
|
* @param string $sNextLibrary |
|
|
|
|
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function setNextLibrary(string $sNextLibrary): void |
258
|
|
|
{ |
259
|
|
|
$this->sNextLibrary = $sNextLibrary; |
260
|
|
|
} |
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Register the javascript dialog libraries from config options. |
264
|
|
|
* |
265
|
|
|
* @return void |
266
|
|
|
* @throws SetupException |
267
|
|
|
*/ |
268
|
|
|
protected function registerLibraries() |
269
|
|
|
{ |
270
|
|
|
$aLibraries = $this->xConfigManager->getOption('dialogs.classes', []); |
271
|
|
|
foreach($aLibraries as $sLibraryName => $sClassName) |
272
|
|
|
{ |
273
|
|
|
$this->registerLibrary($sClassName, $sLibraryName); |
274
|
|
|
} |
275
|
|
|
} |
|
|
|
|
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Update the javascript dialog libraries list. |
279
|
|
|
* |
280
|
|
|
* @return void |
281
|
|
|
*/ |
282
|
|
|
protected function updateLibraries() |
283
|
|
|
{ |
284
|
|
|
$aLibraries = $this->xConfigManager->getOption('dialogs.libraries', []); |
285
|
|
|
foreach($aLibraries as $sLibraryName) |
286
|
|
|
{ |
287
|
|
|
$this->aLibraries[$sLibraryName] = true; |
288
|
|
|
} |
289
|
|
|
} |
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Set the default library for each dialog feature. |
293
|
|
|
* |
294
|
|
|
* @return void |
295
|
|
|
* @throws SetupException |
296
|
|
|
*/ |
297
|
|
|
protected function setDefaultLibraries() |
298
|
|
|
{ |
299
|
|
|
// Set the default modal library |
300
|
|
|
if(($sLibraryName = $this->xConfigManager->getOption('dialogs.default.modal', ''))) |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
$this->setModalLibrary($sLibraryName); |
303
|
|
|
} |
304
|
|
|
// Set the default message library |
305
|
|
|
if(($sLibraryName = $this->xConfigManager->getOption('dialogs.default.message', ''))) |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
$this->setMessageLibrary($sLibraryName); |
308
|
|
|
} |
309
|
|
|
// Set the default question library |
310
|
|
|
if(($sLibraryName = $this->xConfigManager->getOption('dialogs.default.question', ''))) |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
$this->setQuestionLibrary($sLibraryName); |
313
|
|
|
} |
314
|
|
|
} |
|
|
|
|
315
|
|
|
|
316
|
|
|
/** |
|
|
|
|
317
|
|
|
* @inheritDoc |
318
|
|
|
* @throws SetupException |
319
|
|
|
*/ |
|
|
|
|
320
|
|
|
public function onChanges(Config $xConfig) |
321
|
|
|
{ |
322
|
|
|
// Reset the default libraries any time the config is changed. |
323
|
|
|
$this->registerLibraries(); |
324
|
|
|
$this->updateLibraries(); |
325
|
|
|
$this->setDefaultLibraries(); |
326
|
|
|
} |
|
|
|
|
327
|
|
|
|
328
|
|
|
/** |
|
|
|
|
329
|
|
|
* @inheritDoc |
330
|
|
|
* @throws SetupException |
331
|
|
|
*/ |
|
|
|
|
332
|
|
|
public function onChange(Config $xConfig, string $sName) |
333
|
|
|
{ |
334
|
|
|
if(substr($sName, 0, 15) === 'dialogs.classes') |
335
|
|
|
{ |
336
|
|
|
$this->registerLibraries(); |
337
|
|
|
return; |
338
|
|
|
} |
339
|
|
|
if($sName === 'dialogs.libraries') |
340
|
|
|
{ |
341
|
|
|
$this->updateLibraries(); |
342
|
|
|
return; |
343
|
|
|
} |
344
|
|
|
if(substr($sName, 0, 15) === 'dialogs.default') |
345
|
|
|
{ |
346
|
|
|
$this->setDefaultLibraries(); |
347
|
|
|
} |
348
|
|
|
} |
|
|
|
|
349
|
|
|
} |
350
|
|
|
|