-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathApplication.php
More file actions
452 lines (380 loc) · 10.5 KB
/
Application.php
File metadata and controls
452 lines (380 loc) · 10.5 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
declare(strict_types=1);
namespace Bow\Application;
use Bow\Application\Exception\ApplicationException;
use Bow\Configuration\Loader;
use Bow\Container\Capsule;
use Bow\Container\Compass;
use Bow\Contracts\ResponseInterface;
use Bow\Http\Exception\BadRequestException;
use Bow\Http\Exception\HttpException;
use Bow\Http\Request;
use Bow\Http\Response;
use Bow\Router\Exception\RouterException;
use Bow\Router\Resource;
use Bow\Router\Route;
use Bow\Router\Router;
use ReflectionException;
class Application
{
/**
* The Application instance
*
* @var ?Application
*/
private static ?Application $instance = null;
/**
* The Capsule instance
*
* @var Capsule
*/
private Capsule $capsule;
/**
* The booting flag
*
* @var bool
*/
private bool $booted = false;
/**
* The HTTP Request
*
* @var Request
*/
private Request $request;
/**
* The HTTP Response
*
* @var Response
*/
private Response $response;
/**
* The router instance
*
* @var Router
*/
private Router $router;
/**
* The Configuration Loader instance
*
* @var Loader
*/
private Loader $config;
/**
* This defines if the X-powered-By header must be put in response
*
* @var bool
*/
private bool $disable_powered_by = false;
/**
* Application constructor
*
* @param Request $request
* @param Response $response
* @throws BadRequestException
*/
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
$this->router = Router::configure($request->get('_method'));
$this->capsule = Capsule::getInstance();
$this->capsule->instance('response', $response);
$this->capsule->instance('request', $request);
$this->capsule->instance('router', $this->router);
$this->capsule->instance('app', $this);
$this->request->capture();
}
/**
* Get container
*
* @return Capsule
*/
public function getContainer(): Capsule
{
return $this->capsule;
}
/**
* Get router
*
* @return Router
*/
public function getRouter(): Router
{
return $this->router;
}
/**
* Check if it is running on php cli
*
* @return bool
*/
public function isRunningOnCli(): bool
{
return php_sapi_name() == 'cli';
}
/**
* Launcher of the application
*
* @return bool
* @throws ReflectionException
* @throws RouterException
*/
public function run(): bool
{
if ($this->config->isCli()) {
return true;
}
// We add of the X-Powered-By header when disable_powered_by is true
if (!$this->disable_powered_by) {
$this->response->withHeader('X-Powered-By', 'Bow Framework');
}
$this->router->setPrefix('');
$method = $this->request->method();
// We verify the existence of a special method DELETE, PUT
if ($method == 'POST') {
if ($this->router->hasSpecialMethod()) {
$method = $this->router->getSpecialMethod();
}
}
// We verify the existence of the method of the request in
// the routing collection
$routes = $this->router->getRoutes();
$response = null;
$resolved = false;
foreach ($routes[$method] ?? [] as $route) {
// The route must be an instance of Route
if (!($route instanceof Route)) {
continue;
}
// We launch the search of the method that arrived in the query
// then start checking the url of the request
if (!$route->match($this->request->path(), $this->request->domain())) {
continue;
}
$this->router->setCurrentPath($route->getPath());
// We call the action associate with the route
$response = $route->call();
$resolved = true;
break;
}
// Error management
if ($resolved) {
$this->sendResponse($response);
return true;
}
// We apply the 404 error code
$this->response->status(404);
$error_code = $this->router->getErrorCodes();
if (!array_key_exists(404, $error_code)) {
throw new RouterException(
sprintf('Route "%s" not found', $this->request->path())
);
}
$response = Compass::getInstance()->execute($this->router->getErrorCodes(), []);
$this->sendResponse($response, 404);
return false;
}
/**
* Send the answer to the customer
*
* @param mixed $response
* @param int $code
* @return void
*/
private function sendResponse(mixed $response, int $code = 200): void
{
if ($response instanceof ResponseInterface) {
$response->sendContent();
} else {
echo $this->response->send($response, $code);
}
}
/**
* Allows you to enable writing the X-Powered-By header
* in the answer of the inquiry.
*
* @return void
*/
public function disablePoweredByMention(): void
{
$this->disable_powered_by = true;
}
/**
* Make the REST API base on route and resource controller.
*
* @param string $url
* @param string|array $controller_name
* @param array $where
* @return Application
*
* @throws ApplicationException
*/
public function rest(string $url, string|array $controller_name, array $where = []): Application
{
if (!is_string($controller_name) && !is_array($controller_name)) {
throw new ApplicationException(
'The first parameter must be an array or a string',
E_ERROR
);
}
$ignore_method = [];
$controller = $controller_name;
if (is_array($controller_name)) {
// Get controller
if (isset($controller_name['controller'])) {
$controller = $controller_name['controller'];
unset($controller_name['controller']);
}
// Get all ignores methods
if (isset($controller_name['ignores'])) {
$ignore_method = $controller_name['ignores'];
unset($controller_name['ignores']);
}
}
if (!is_string($controller)) {
throw new ApplicationException(
"[REST] No defined controller!",
E_ERROR
);
}
// Normalize url
$url = preg_replace('/\/+$/', '', $url);
Resource::make($url, $controller, $where, $ignore_method);
return $this;
}
/**
* Build the application
*
* @param Request $request
* @param Response $response
* @return Application
* @throws BadRequestException
*/
public static function make(Request $request, Response $response): Application
{
if (is_null(static::$instance)) {
static::$instance = new Application($request, $response);
}
return static::$instance;
}
/**
* Abort application
*
* @param int $code
* @param string $message
* @param array $headers
* @return void
*
* @throws HttpException
*/
public function abort(int $code = 500, string $message = '', array $headers = []): void
{
$this->response->status($code);
foreach ($headers as $key => $value) {
$this->response->withHeader($key, $value);
}
if ($message == null) {
$message = 'The trial was suspended.';
}
throw new HttpException($message);
}
/**
* Build dependence
*
* @param ?string $name
* @param ?callable $callable
* @return mixed
* @throws ApplicationException
*/
public function container(?string $name = null, ?callable $callable = null): mixed
{
if (is_null($name)) {
return $this->capsule;
}
if (is_null($callable)) {
return $this->capsule->make($name);
}
if (!is_callable($callable)) {
throw new ApplicationException(
'The second parameter must be a callable.'
);
}
$this->capsule->bind($name, $callable);
return $this;
}
/**
* Configuration Association
*
* @param Loader $config
* @return void
*/
public function bind(Loader $config): void
{
$this->config = $config;
$this->capsule->instance('config', $config);
$this->boot();
// We activate the auto csrf switcher
$this->router->setAutoCsrf((bool) ($config['app']['auto_csrf'] ?? false));
if (is_string($config['app']['root'])) {
$this->router->setBaseRoute($config['app']['root']);
}
}
/**
* Boot the application
*
* @return void
*/
private function boot(): void
{
if ($this->booted) {
return;
}
$this->config->boot();
$this->booted = true;
}
/**
* __invoke
*
* This point method on the container system
*
* @param array $params
* @return mixed
* @throws ApplicationException
*/
public function __invoke(...$params): mixed
{
if (count($params)) {
return $this->capsule;
}
if (count($params) > 2) {
throw new ApplicationException('Second parameter must be pass.');
}
if (count($params) == 1) {
return $this->capsule->make($params[0]);
}
return $this->capsule->bind($params[0], $params[1]);
}
/**
* Send the application response
*
* @return void
*/
public function send(): void
{
$this->run();
}
/**
* Delegate method calls to the router
*
* @param string $method
* @param array $args
* @return mixed
* @throws ApplicationException
*/
public function __call(string $method, array $args): mixed
{
if (!method_exists($this->router, $method)) {
throw new ApplicationException("Method [$method] does not exist in Application.");
}
return call_user_func_array([$this->router, $method], $args);
}
}