-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.php
349 lines (304 loc) · 9.74 KB
/
Application.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
<?php
/**
* File defining \Backend\Core\Application
*
* PHP Version 5.3
*
* @category Backend
* @package Core
* @author J Jurgens du Toit <[email protected]>
* @copyright 2011 - 2012 Jade IT (cc)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
namespace Backend\Core;
use Backend\Interfaces\ApplicationInterface;
use Backend\Interfaces\RouterInterface;
use Backend\Interfaces\RequestInterface;
use Backend\Interfaces\ResponseInterface;
use Backend\Interfaces\CallbackInterface;
use Backend\Interfaces\ConfigInterface;
use Backend\Interfaces\DependencyInjectionContainerInterface;
use Backend\Core\Utilities\Router;
use Backend\Core\Utilities\Callback;
use Backend\Core\Exception as CoreException;
/**
* The main application class.
*
* @category Backend
* @package Core
* @author J Jurgens du Toit <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://backend-php.net
*/
class Application implements ApplicationInterface
{
/**
* Router to map callbacks to requests, and vice versa.
*
* @var Backend\Interfaces\RouterInterface
*/
protected $router = null;
/**
* The request currently being executed.
*
* @var \Backend\Interfaces\RequestInterface
*/
protected $request;
/**
* The Application Configuration.
*
* @var Backend\Interfaces\ConfigInterface
*/
protected $config;
/**
* The Dependency Injection Container for the Application.
*
* @var Backend\Interfaces\DependencyInjectionContainerInterface
*/
protected $container;
/**
* The constructor for the object.
*
* @param Backend\Interfaces\ConfigInterface $config The Configuration for the
* Application.
* @param Backend\Interfaces\DependencyInjectionContainerInterface $container The
* DI Container for the Application.
* Application.
*/
public function __construct(ConfigInterface $config, DependencyInjectionContainerInterface $container)
{
$this->container = $container;
$this->setConfig($config);
$this->init();
}
/**
* Initialize the Application.
*
* @return boolean Returns true if the initialization ran. False otherwise.
*/
public function init()
{
static $ran = false;
if ($ran) {
return false;
}
$this->raiseEvent('core.init');
// PHP Helpers
register_shutdown_function(array($this, 'shutdown'));
set_exception_handler(array($this, 'exception'));
set_error_handler(array($this, 'error'));
$ran = true;
return true;
}
/**
* Main function for the application.
*
* Inspect the request and subsequent results, chain if necessary.
*
* @param Backend\Interfaces\RequestInterface $request The request the
* application should handle
*
* @return \Backend\Interfaces\ResponseInterface
* @throws \Backend\Core\Exception When there's no route or formatter for the
* request.
*/
public function main(RequestInterface $request = null)
{
// Get the initial Request / result
$result = $request ?: $this->container->get('request');
do {
// Raise the event with the request
if ($result instanceof RequestInterface) {
$event = new Event\RequestEvent($result);
$this->raiseEvent('core.request', $event);
$this->setRequest($event->getRequest());
$callback = $this->getRouter()->inspect($this->getRequest());
} elseif ($result instanceof CallbackInterface) {
$callback = $result;
}
if (empty($callback) || ($callback instanceof CallbackInterface) === false) {
// 404 - Not Found
$message = 'Unknown route requested:' . $result->getMethod()
. ' ' . $result->getPath();
throw new CoreException($message, 404);
}
$this->container->set('callback', $callback);
// Callback Event
$event = new Event\CallbackEvent($callback);
$this->raiseEvent('core.callback', $event);
$callback = $event->getCallback();
// Transform the request by executing the Callback
$result = $callback->execute();
} while ($result instanceof RequestInterface
|| $result instanceof CallbackInterface);
// Transform the Result into a Response
$event = new Event\ResultEvent($result);
$this->raiseEvent('core.result', $event);
$response = $event->getResponse();
// Transform the Response
$event = new Event\ResponseEvent($response);
$this->raiseEvent('core.response', $event);
return $event->getResponse();
}
/**
* Get the request that's currently being executed.
*
* @return \Backend\Interfaces\RequestInterface
*/
public function getRequest()
{
if ($this->request === null && $this->container->has('request')) {
$this->request = $this->container->get('request');
}
return $this->request;
}
/**
* Set the request that's will be executed.
*
* @param \Backend\Interfaces\RequestInterface $request The request to set.
*
* @return \Backend\Core\Application
*/
public function setRequest($request)
{
$this->request = $request;
$this->container->set('request', $this->request);
return $this;
}
/**
* Get the application's configuration.
*
* @return \Backend\Interfaces\ConfigInterface
*/
public function getConfig()
{
return $this->config;
}
/**
* Set the request that's will be executed.
*
* @param \Backend\Interfaces\RequestInterface $request The request to set.
*
* @return \Backend\Core\Application
*/
public function setConfig($config)
{
$this->config = $config;
$this->container->set('application.config', $this->config);
return $this;
}
/**
* Get the Router for the Application.
*
* @return Backend\Interfaces\RouterInterface
*/
public function getRouter()
{
if (empty($this->router)) {
$this->router = $this->container->get('router');
}
return $this->router;
}
/**
* Set the Router for the Application.
*
* @param Backend\Interfaces\RouterInterface $router The router for the Applciation.
*
* @return Backend\Core\Application
*/
public function setRouter(RouterInterface $router)
{
$this->router = $router;
return $this;
}
/**
* Set the Application's DI Container.
*
* @param \Backend\Interfaces\DependencyInjectionContainerInterface $container
* The DI Container for the Application.
*
* @return \Backend\Interfaces\ControllerInterface The current object.
*/
public function setContainer(DependencyInjectionContainerInterface $container)
{
$this->container = $container;
return $this;
}
/**
* Get the Application's DI Container
*
* @return \Backend\Interfaces\DependencyInjectionContainerInterface The
* Application's DI Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Raise an event.
*
* @return \Backend\Core\Application The current object
*/
public function raiseEvent($name, $event = null)
{
if ($this->container->has('event_dispatcher')) {
$this->container->get('event_dispatcher')->dispatch($name, $event);
}
return $this;
}
/**
* Shutdown function called when ever the script ends
*
* @return null
*/
public function shutdown()
{
$this->raiseEvent('core.shutdown');
}
/**
* Error handling function called when ever an error occurs.
*
* Called by set_error_handler. Some types of errors will be converted into
* excceptions.
*
* @param int $errno The error number.
* @param string $errstr The error string.
* @param string $errfile The file the error occured in.
* @param int $errline The line number the error occured on.
* @param array $errcontext The context the error occured in.
* @param bool $return Return the exception instead of running it.
*
* @return \Exception
*/
public function error($errno, $errstr, $errfile, $errline, $errcontext, $return = false)
{
$exception = new \ErrorException($errstr, 500, $errno, $errfile, $errline);
// Don't raise an event here, it's raised in the exception method.
if ($return) {
return $exception;
}
throw $exception;
}
/**
* Exception handling function called when ever an exception isn't handled.
*
* Called by set_exception_handler.
*
* @param \Exception $exception The thrown exception.
* @param bool $return Return the response instead of outputting it.
*
* @return \Backend\Interfaces\ResponseInterface
*/
public function exception(\Exception $exception)
{
$event = new Event\ExceptionEvent($exception);
$this->raiseEvent('core.exception', $event);
$exception = $event->getException();
$response = $event->getResponse();
// Not 100% sure this is good design
$response instanceof ResponseInterface && $response->output() && die;
// If output returns false, it won't exit, which will throw the exception
throw $exception;
}
}