|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PluginManager.php - Jaxon plugin registry |
|
5
|
|
|
* |
|
6
|
|
|
* Register Jaxon plugins and callables. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Jared White |
|
|
|
|
|
|
10
|
|
|
* @author J. Max Wilson |
|
|
|
|
|
|
11
|
|
|
* @author Joseph Woolley |
|
|
|
|
|
|
12
|
|
|
* @author Steffen Konerow |
|
|
|
|
|
|
13
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
|
|
15
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
|
|
16
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
17
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
18
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
namespace Jaxon\Plugin\Manager; |
|
22
|
|
|
|
|
23
|
|
|
use Jaxon\Jaxon; |
|
24
|
|
|
use Jaxon\App\I18n\Translator; |
|
25
|
|
|
use Jaxon\Di\Container; |
|
26
|
|
|
use Jaxon\Exception\SetupException; |
|
27
|
|
|
use Jaxon\Plugin\CallableRegistryInterface; |
|
28
|
|
|
use Jaxon\Plugin\Code\CodeGenerator; |
|
29
|
|
|
use Jaxon\Plugin\CodeGeneratorInterface; |
|
30
|
|
|
use Jaxon\Plugin\Request\CallableClass\CallableClassPlugin; |
|
31
|
|
|
use Jaxon\Plugin\Request\CallableDir\CallableDirPlugin; |
|
32
|
|
|
use Jaxon\Plugin\Request\CallableFunction\CallableFunctionPlugin; |
|
33
|
|
|
use Jaxon\Plugin\RequestHandlerInterface; |
|
34
|
|
|
use Jaxon\Plugin\RequestPlugin; |
|
35
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagPlugin; |
|
36
|
|
|
use Jaxon\Plugin\Response\Dialog\DialogPlugin; |
|
37
|
|
|
use Jaxon\Plugin\Response\JQuery\JQueryPlugin; |
|
38
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
39
|
|
|
use Jaxon\Plugin\ResponsePluginInterface; |
|
40
|
|
|
use Jaxon\Request\Handler\ParameterReader; |
|
41
|
|
|
use Jaxon\Response\Response; |
|
42
|
|
|
|
|
43
|
|
|
use function class_implements; |
|
44
|
|
|
use function in_array; |
|
45
|
|
|
|
|
46
|
|
|
class PluginManager |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* @var Container |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $di; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var CodeGenerator |
|
55
|
|
|
*/ |
|
56
|
|
|
private $xCodeGenerator; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Translator |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $xTranslator; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Request plugins, indexed by name |
|
65
|
|
|
* |
|
66
|
|
|
* @var array<CallableRegistryInterface> |
|
67
|
|
|
*/ |
|
68
|
|
|
private $aRegistryPlugins = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Request handlers, indexed by name |
|
72
|
|
|
* |
|
73
|
|
|
* @var array<RequestHandlerInterface> |
|
74
|
|
|
*/ |
|
75
|
|
|
private $aRequestHandlers = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Response plugins, indexed by name |
|
79
|
|
|
* |
|
80
|
|
|
* @var array<ResponsePluginInterface> |
|
81
|
|
|
*/ |
|
82
|
|
|
private $aResponsePlugins = []; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The constructor |
|
86
|
|
|
* |
|
87
|
|
|
* @param Container $di |
|
|
|
|
|
|
88
|
|
|
* @param CodeGenerator $xCodeGenerator |
|
|
|
|
|
|
89
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct(Container $di, CodeGenerator $xCodeGenerator, Translator $xTranslator) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$this->di = $di; |
|
|
|
|
|
|
94
|
|
|
$this->xCodeGenerator = $xCodeGenerator; |
|
95
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
96
|
|
|
} |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the request plugins |
|
100
|
|
|
* |
|
101
|
|
|
* @return array<RequestPlugin> |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getRequestHandlers(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->aRequestHandlers; |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Register a plugin |
|
110
|
|
|
* |
|
111
|
|
|
* Below is a table for priorities and their description: |
|
112
|
|
|
* - 0 to 999: Plugins that are part of or extensions to the jaxon core |
|
113
|
|
|
* - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
|
114
|
|
|
* - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $sClassName The plugin class |
|
|
|
|
|
|
117
|
|
|
* @param string $sPluginName The plugin name |
|
|
|
|
|
|
118
|
|
|
* @param integer $nPriority The plugin priority, used to order the plugins |
|
|
|
|
|
|
119
|
|
|
* |
|
120
|
|
|
* @return void |
|
121
|
|
|
* @throws SetupException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
|
124
|
|
|
{ |
|
125
|
|
|
$bIsUsed = false; |
|
|
|
|
|
|
126
|
|
|
$aInterfaces = class_implements($sClassName); |
|
127
|
|
|
if(in_array(CodeGeneratorInterface::class, $aInterfaces)) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->xCodeGenerator->addCodeGenerator($sClassName, $nPriority); |
|
130
|
|
|
$bIsUsed = true; |
|
131
|
|
|
} |
|
132
|
|
|
if(in_array(CallableRegistryInterface::class, $aInterfaces)) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->aRegistryPlugins[$sPluginName] = $sClassName; |
|
135
|
|
|
$bIsUsed = true; |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
if(in_array(RequestHandlerInterface::class, $aInterfaces)) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->aRequestHandlers[$sPluginName] = $sClassName; |
|
140
|
|
|
$bIsUsed = true; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
if(in_array(ResponsePluginInterface::class, $aInterfaces)) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->aResponsePlugins[$sPluginName] = $sClassName; |
|
145
|
|
|
$bIsUsed = true; |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if(!$bIsUsed) |
|
149
|
|
|
{ |
|
150
|
|
|
// The class is invalid. |
|
151
|
|
|
$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
152
|
|
|
throw new SetupException($sMessage); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// Register the plugin in the DI container, if necessary |
|
156
|
|
|
if(!$this->di->has($sClassName)) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->di->auto($sClassName); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Find the specified response plugin by name and return a reference to it if one exists |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $sName The name of the plugin |
|
|
|
|
|
|
166
|
|
|
* @param Response|null $xResponse The response to attach the plugin to |
|
|
|
|
|
|
167
|
|
|
* |
|
168
|
|
|
* @return ResponsePlugin|null |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getResponsePlugin(string $sName, ?Response $xResponse = null): ?ResponsePlugin |
|
171
|
|
|
{ |
|
172
|
|
|
if(!isset($this->aResponsePlugins[$sName])) |
|
173
|
|
|
{ |
|
174
|
|
|
return null; |
|
175
|
|
|
} |
|
176
|
|
|
$xPlugin = $this->di->g($this->aResponsePlugins[$sName]); |
|
|
|
|
|
|
177
|
|
|
if(($xResponse)) |
|
178
|
|
|
{ |
|
179
|
|
|
$xPlugin->setResponse($xResponse); |
|
180
|
|
|
} |
|
181
|
|
|
return $xPlugin; |
|
182
|
|
|
} |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Register a function or callable class |
|
186
|
|
|
* |
|
187
|
|
|
* Call the request plugin with the $sType defined as name. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $sType The type of request handler being registered |
|
|
|
|
|
|
190
|
|
|
* @param string $sCallable The callable entity being registered |
|
|
|
|
|
|
191
|
|
|
* @param array|string $xOptions The associated options |
|
|
|
|
|
|
192
|
|
|
* |
|
193
|
|
|
* @return void |
|
194
|
|
|
* @throws SetupException |
|
195
|
|
|
*/ |
|
196
|
|
|
public function registerCallable(string $sType, string $sCallable, $xOptions = []) |
|
197
|
|
|
{ |
|
198
|
|
|
if(isset($this->aRegistryPlugins[$sType]) && |
|
199
|
|
|
($xPlugin = $this->di->g($this->aRegistryPlugins[$sType]))) |
|
|
|
|
|
|
200
|
|
|
{ |
|
201
|
|
|
$xPlugin->register($sType, $sCallable, $xPlugin->checkOptions($sCallable, $xOptions)); |
|
202
|
|
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
throw new SetupException($this->xTranslator->trans('errors.register.plugin', |
|
205
|
|
|
['name' => $sType, 'callable' => $sCallable])); |
|
206
|
|
|
} |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Register the Jaxon request plugins |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
* @throws SetupException |
|
213
|
|
|
*/ |
|
214
|
|
|
public function registerPlugins() |
|
215
|
|
|
{ |
|
216
|
|
|
// Request plugins |
|
217
|
|
|
$this->registerPlugin(CallableClassPlugin::class, Jaxon::CALLABLE_CLASS, 101); |
|
218
|
|
|
$this->registerPlugin(CallableFunctionPlugin::class, Jaxon::CALLABLE_FUNCTION, 102); |
|
219
|
|
|
$this->registerPlugin(CallableDirPlugin::class, Jaxon::CALLABLE_DIR, 103); |
|
220
|
|
|
|
|
221
|
|
|
// Response plugins |
|
222
|
|
|
$this->registerPlugin(JQueryPlugin::class, JQueryPlugin::NAME, 700); |
|
223
|
|
|
$this->registerPlugin(DataBagPlugin::class, DataBagPlugin::NAME, 700); |
|
224
|
|
|
$this->registerPlugin(DialogPlugin::class, DialogPlugin::NAME, 750); |
|
225
|
|
|
} |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Get the parameter reader |
|
229
|
|
|
* |
|
230
|
|
|
* @return ParameterReader |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getParameterReader() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->di->g(ParameterReader::class); |
|
235
|
|
|
} |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|