This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
/
abstract.php
362 lines (311 loc) · 10.7 KB
/
abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* Kodekit - http://timble.net/kodekit
*
* @copyright Copyright (C) 2007 - 2016 Johan Janssens and Timble CVBA. (http://www.timble.net)
* @license MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
* @link https://github.com/timble/kodekit for the canonical source repository
*/
namespace Kodekit\Library;
/**
* Abstract Dispatcher
*
* @author Johan Janssens <http://github.com/johanjanssens>
* @package Kodekit\Library\Dispatcher
*/
abstract class DispatcherAbstract extends ControllerAbstract implements DispatcherInterface
{
/**
* Controller object or identifier
*
* @var string|object
*/
protected $_controller;
/**
* Controller action
*
* @var string
*/
protected $_controller_action;
/**
* Has the dispatcher been forwarded
*
* @var boolean|DispatcherInterface
*/
protected $_forwarded;
/**
* Constructor.
*
* @param ObjectConfig $config An optional ObjectConfig object with configuration options.
*/
public function __construct(ObjectConfig $config)
{
parent::__construct($config);
//Set the forwarded state
$this->_forwarded = $config->forwarded;
//Set the controller
$this->_controller = $config->controller;
//Set the controller action
$this->_controller_action = $config->controller_action;
//Resolve the request
$this->addCommandCallback('before.dispatch', '_resolveRequest');
}
/**
* Initializes the options for the object
*
* Called from {@link __construct()} as a first step of object instantiation.
*
* @param ObjectConfig $config An optional ObjectConfig object with configuration options.
* @return void
*/
protected function _initialize(ObjectConfig $config)
{
$config->append(array(
'controller' => $this->getIdentifier()->package,
'controller_action' => 'render',
'authenticators' => array(),
'forwarded' => false,
))->append(array(
'behaviors' => array('authenticatable' => array('authenticators' => $config->authenticators)),
));
parent::_initialize($config);
}
/**
* Has the controller been forwarded
*
* @return boolean Returns true if the dispatcher has been forwarded
*/
public function isForwarded()
{
return $this->_forwarded;
}
/**
* Get the request object
*
* @throws \UnexpectedValueException If the request doesn't implement the DispatcherRequestInterface
* @return DispatcherRequest
*/
public function getRequest()
{
if(!$this->_request instanceof DispatcherRequestInterface)
{
//Setup the request
$this->_request->user = $this->getUser();
$this->_request = $this->getObject('dispatcher.request', ObjectConfig::unbox($this->_request));
if(!$this->_request instanceof DispatcherRequestInterface)
{
throw new \UnexpectedValueException(
'Request: '.get_class($this->_request).' does not implement DispatcherRequestInterface'
);
}
}
return $this->_request;
}
/**
* Get the response object
*
* @throws \UnexpectedValueException If the response doesn't implement the DispatcherResponseInterface
* @return DispatcherResponse
*/
public function getResponse()
{
if(!$this->_response instanceof DispatcherResponseInterface)
{
//Setup the response
$this->_response->request = $this->getRequest();
$this->_response->user = $this->getUser();
$this->_response = $this->getObject('dispatcher.response', ObjectConfig::unbox($this->_response));
if(!$this->_response instanceof DispatcherResponseInterface)
{
throw new \UnexpectedValueException(
'Response: '.get_class($this->_response).' does not implement DispatcherResponseInterface'
);
}
}
return $this->_response;
}
/**
* Method to get a controller object
*
* @throws \UnexpectedValueException If the controller doesn't implement the ControllerInterface
* @return ControllerInterface
*/
public function getController()
{
if(!($this->_controller instanceof ControllerInterface))
{
//Make sure we have a controller identifier
if(!($this->_controller instanceof ObjectIdentifier)) {
$this->setController($this->_controller);
}
$config = array(
'request' => $this->getRequest(),
'user' => $this->getUser(),
'response' => $this->getResponse(),
'dispatched' => $this
);
$this->_controller = $this->getObject($this->_controller, $config);
//Make sure the controller implements ControllerInterface
if(!$this->_controller instanceof ControllerInterface)
{
throw new \UnexpectedValueException(
'Controller: '.get_class($this->_controller).' does not implement ControllerInterface'
);
}
}
return $this->_controller;
}
/**
* Method to set a controller object attached to the dispatcher
*
* @param mixed $controller An object that implements ControllerInterface, ObjectIdentifier object
* or valid identifier string
* @param array $config An optional associative array of configuration options
* @return DispatcherAbstract
*/
public function setController($controller, $config = array())
{
if(!($controller instanceof ControllerInterface))
{
if(is_string($controller) && strpos($controller, '.') === false )
{
// Controller names are always singular
if(StringInflector::isPlural($controller)) {
$controller = StringInflector::singularize($controller);
}
$identifier = $this->getIdentifier()->toArray();
$identifier['path'] = array('controller');
$identifier['name'] = $controller;
$identifier = $this->getIdentifier($identifier);
}
else $identifier = $this->getIdentifier($controller);
//Set the configuration
$identifier->getConfig()->append($config);
$controller = $identifier;
}
$this->_controller = $controller;
return $this;
}
/**
* Method to get a controller action to be executed
*
* @return string
*/
public function getControllerAction()
{
return $this->_controller_action;
}
/**
* Method to set the controller action to be executed
*
* @return DispatcherAbstract
*/
public function setControllerAction($action)
{
$this->_controller_action = $action;
return $this;
}
/**
* Get the controller context
*
* @param ControllerContextInterface $context Context to cast to a local context
* @return DispatcherContext
*/
public function getContext(ControllerContextInterface $context = null)
{
$context = new DispatcherContext($context);
$context->setRequest($this->getRequest());
$context->setUser($this->getUser());
$context->setResponse($this->getResponse());
return $context;
}
/**
* Resolve the request
*
* @param DispatcherContext $context A dispatcher context object
*/
protected function _resolveRequest(DispatcherContext $context)
{
//Resolve the controller
if($context->request->query->has('view')) {
$this->setController($context->request->query->get('view', 'cmd'));
}
//Resolve the controller action
if($context->request->data->has('_action')) {
$this->setControllerAction($context->request->data->get('_action', 'cmd'));
}
}
/**
* Dispatch the request
*
* Dispatch to a controller internally or forward to another component. Functions makes an internal sub-request,
* based on the information in the request and passing along the context.
*
* @param DispatcherContext $context A dispatcher context object
* @return mixed
*/
protected function _actionDispatch(DispatcherContext $context)
{
$controller = $this->getController();
$action = $this->getControllerAction();
//Execute the component and cast the context
$context->result = $controller->execute($action, $controller->getContext($context));
//Set the result in the response
if($context->result && !$context->response->isRedirect())
{
$result = $context->result;
if ($result instanceof ObjectConfigFormat) {
$context->response->setContentType($result->getMediaType());
}
if (is_string($result) || (is_object($result) && method_exists($result, '__toString'))) {
$context->response->setContent($result);
}
}
//Send the response
if (!$this->isForwarded()) {
$this->send($context);
}
}
/**
* Redirect
*
* Redirect to a URL externally. If no redirect status code has been specified in the response a 301 (permanent)
* redirect will performed.
*
* @param DispatcherContext $context A dispatcher context object
*/
protected function _actionRedirect(DispatcherContext $context)
{
$url = $context->param;
//Only set the status if it hasn't been set yet
if(!$context->response->isRedirect()) {
$context->response->setStatus(DispatcherResponse::MOVED_PERMANENTLY);
}
$context->response->setRedirect($context->param);
//Send the response
return $this->send($context);
}
/**
* Send the response
*
* @param DispatcherContext $context A dispatcher context object
* @return mixed
*/
protected function _actionSend(DispatcherContext $context)
{
//Send the response
$context->response->send(false);
//Terminate the response
$this->terminate($context);
}
/**
* Flush the output buffer and terminate request
*
* @param DispatcherContextInterface $context
* @return void
*/
public function _actionTerminate(DispatcherContextInterface $context)
{
$context->response->terminate();
}
}