1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* RequestHandler.php - Jaxon RequestPlugin Handler |
5
|
|
|
* |
6
|
|
|
* This class processes an incoming jaxon request. |
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\Request\Handler; |
22
|
|
|
|
23
|
|
|
use Jaxon\Di\Container; |
24
|
|
|
use Jaxon\Exception\RequestException; |
25
|
|
|
use Jaxon\Plugin\Manager\PluginManager; |
26
|
|
|
use Jaxon\Plugin\RequestHandlerInterface; |
27
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagPlugin; |
28
|
|
|
use Jaxon\Request\Upload\UploadHandlerInterface; |
29
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
30
|
|
|
|
31
|
|
|
use Exception; |
32
|
|
|
|
33
|
|
|
class RequestHandler |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Container |
37
|
|
|
*/ |
38
|
|
|
private $di; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The plugin manager. |
42
|
|
|
* |
43
|
|
|
* @var PluginManager |
44
|
|
|
*/ |
45
|
|
|
private $xPluginManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The response manager. |
49
|
|
|
* |
50
|
|
|
* @var ResponseManager |
51
|
|
|
*/ |
52
|
|
|
private $xResponseManager; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The callbacks to run while processing the request |
56
|
|
|
* |
57
|
|
|
* @var CallbackManager |
58
|
|
|
*/ |
59
|
|
|
private $xCallbackManager; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var UploadHandlerInterface |
63
|
|
|
*/ |
64
|
|
|
private $xUploadHandler; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The data bag response plugin |
68
|
|
|
* |
69
|
|
|
* @var DataBagPlugin |
70
|
|
|
*/ |
71
|
|
|
private $xDataBagPlugin; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The request plugin that is able to process the current request |
75
|
|
|
* |
76
|
|
|
* @var RequestHandlerInterface |
77
|
|
|
*/ |
78
|
|
|
private $xRequestPlugin = null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The constructor |
82
|
|
|
* |
83
|
|
|
* @param Container $di |
|
|
|
|
84
|
|
|
* @param PluginManager $xPluginManager |
|
|
|
|
85
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
86
|
|
|
* @param CallbackManager $xCallbackManager |
|
|
|
|
87
|
|
|
* @param DataBagPlugin $xDataBagPlugin |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public function __construct(Container $di, PluginManager $xPluginManager, ResponseManager $xResponseManager, |
|
|
|
|
90
|
|
|
CallbackManager $xCallbackManager, DataBagPlugin $xDataBagPlugin) |
91
|
|
|
{ |
92
|
|
|
$this->di = $di; |
|
|
|
|
93
|
|
|
$this->xPluginManager = $xPluginManager; |
|
|
|
|
94
|
|
|
$this->xResponseManager = $xResponseManager; |
95
|
|
|
$this->xCallbackManager = $xCallbackManager; |
96
|
|
|
$this->xDataBagPlugin = $xDataBagPlugin; |
|
|
|
|
97
|
|
|
} |
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check if the current request can be processed |
101
|
|
|
* |
102
|
|
|
* Calls each of the request plugins and determines if the current request can be processed by one of them. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function canProcessRequest(): bool |
107
|
|
|
{ |
108
|
|
|
$this->xUploadHandler = $this->di->getUploadHandler(); |
109
|
|
|
|
110
|
|
|
// Return true if the request plugin was already found |
111
|
|
|
if($this->xRequestPlugin !== null) |
112
|
|
|
{ |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// The HTTP request |
117
|
|
|
$xRequest = $this->di->getRequest(); |
118
|
|
|
// Find a plugin to process the request |
119
|
|
|
foreach($this->xPluginManager->getRequestHandlers() as $sClassName) |
120
|
|
|
{ |
121
|
|
|
if($sClassName::canProcessRequest($xRequest)) |
122
|
|
|
{ |
123
|
|
|
$this->xRequestPlugin = $this->di->g($sClassName); |
124
|
|
|
$this->xRequestPlugin->setTarget($xRequest); |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Check if the upload plugin is enabled |
130
|
|
|
if($this->xUploadHandler === null) |
131
|
|
|
{ |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// If no other plugin than the upload plugin can process the request, |
136
|
|
|
// then it is an HTTP (not ajax) upload request |
137
|
|
|
$this->xUploadHandler->isHttpUpload(); |
138
|
|
|
return $this->xUploadHandler->canProcessRequest($xRequest); |
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Process the current request and handle errors and exceptions. |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
* @throws RequestException |
146
|
|
|
*/ |
147
|
|
|
private function _processRequest() |
148
|
|
|
{ |
149
|
|
|
// The HTTP request |
150
|
|
|
$xRequest = $this->di->getRequest(); |
151
|
|
|
// Process uploaded files, if the upload plugin is enabled |
152
|
|
|
if($this->xUploadHandler !== null && $this->xUploadHandler->canProcessRequest($xRequest)) |
153
|
|
|
{ |
154
|
|
|
$this->xUploadHandler->processRequest($xRequest); |
155
|
|
|
} |
156
|
|
|
// Process the request |
157
|
|
|
if(($this->xRequestPlugin)) |
158
|
|
|
{ |
159
|
|
|
$xResponse = $this->xRequestPlugin->processRequest(); |
160
|
|
|
if(($xResponse)) |
161
|
|
|
{ |
162
|
|
|
$this->xResponseManager->append($xResponse); |
163
|
|
|
} |
164
|
|
|
// Process the databag |
165
|
|
|
$this->xDataBagPlugin->writeCommand(); |
166
|
|
|
} |
167
|
|
|
} |
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Process the current request. |
171
|
|
|
* |
172
|
|
|
* Calls each of the request plugins to request that they process the current request. |
173
|
|
|
* If any plugin processes the request, it will return true. |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
* @throws RequestException |
177
|
|
|
*/ |
178
|
|
|
public function processRequest() |
179
|
|
|
{ |
180
|
|
|
// Check if there is a plugin to process this request |
181
|
|
|
if(!$this->canProcessRequest()) |
182
|
|
|
{ |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
try |
187
|
|
|
{ |
188
|
|
|
$bEndRequest = false; |
189
|
|
|
// Handle before processing event |
190
|
|
|
if(($this->xRequestPlugin)) |
191
|
|
|
{ |
192
|
|
|
$this->xCallbackManager->onBefore($this->xRequestPlugin->getTarget(), $bEndRequest); |
193
|
|
|
} |
194
|
|
|
if($bEndRequest) |
195
|
|
|
{ |
196
|
|
|
return; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->_processRequest(); |
200
|
|
|
|
201
|
|
|
// Handle after processing event |
202
|
|
|
if(($this->xRequestPlugin)) |
203
|
|
|
{ |
204
|
|
|
$this->xCallbackManager->onAfter($this->xRequestPlugin->getTarget(), $bEndRequest); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
// An exception was thrown while processing the request. |
208
|
|
|
// The request missed the corresponding handler function, |
209
|
|
|
// or an error occurred while attempting to execute the handler. |
210
|
|
|
catch(RequestException $e) |
211
|
|
|
{ |
212
|
|
|
$this->xResponseManager->error($e->getMessage()); |
213
|
|
|
$this->xCallbackManager->onInvalid($e); |
214
|
|
|
} |
215
|
|
|
catch(Exception $e) |
216
|
|
|
{ |
217
|
|
|
$this->xResponseManager->error($e->getMessage()); |
218
|
|
|
$this->xCallbackManager->onError($e); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Print the debug messages |
222
|
|
|
$this->xResponseManager->printDebug(); |
223
|
|
|
} |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|