|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the uSilex project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Enrico Fagnoni <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace uSilex; |
|
13
|
|
|
|
|
14
|
|
|
use Pimple\Container; |
|
15
|
|
|
use Pimple\ServiceProviderInterface; |
|
16
|
|
|
use uSilex\Api\BootableProviderInterface; |
|
17
|
|
|
use Psr\Container\ContainerInterface; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
21
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
22
|
|
|
use Exception; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The uSilex framework class. |
|
26
|
|
|
*/ |
|
27
|
|
|
class Application extends Container implements MiddlewareInterface, ContainerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
protected $providers = []; |
|
30
|
|
|
protected $booted = false; |
|
31
|
|
|
|
|
32
|
1 |
|
public function get($id) |
|
33
|
|
|
{ |
|
34
|
1 |
|
return $this[$id]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function has($id) |
|
38
|
|
|
{ |
|
39
|
1 |
|
return isset($this[$id]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Redefine Registers a service provider. |
|
44
|
|
|
* |
|
45
|
|
|
* @param ServiceProviderInterface $provider A ServiceProviderInterface instance |
|
46
|
|
|
* @param array $values An array of values that customizes the provider |
|
47
|
|
|
* |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function register(ServiceProviderInterface $provider, array $values = []) : self |
|
50
|
|
|
{ |
|
51
|
6 |
|
$this->providers[] = $provider; |
|
52
|
|
|
|
|
53
|
6 |
|
parent::register($provider, $values); |
|
54
|
|
|
|
|
55
|
6 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Boots all service providers. |
|
61
|
|
|
* |
|
62
|
|
|
* This method is automatically called by handle(), but you can use it |
|
63
|
|
|
* to boot all service providers when not handling a request. |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function boot() : self |
|
66
|
|
|
{ |
|
67
|
4 |
|
if ($this->booted) { |
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
$this->booted = true; |
|
72
|
|
|
|
|
73
|
4 |
|
foreach ($this->providers as $provider) { |
|
74
|
1 |
|
if (($provider instanceof BootableProviderInterface)) { |
|
75
|
1 |
|
$provider->boot($this); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// ensure 'uSilex.responseEmitter' exists |
|
80
|
4 |
|
if (!isset($this['uSilex.responseEmitter'])) { |
|
81
|
|
|
$this['uSilex.responseEmitter'] = $this->protect(function($response) { |
|
82
|
1 |
|
echo (string) $response->getBody(); |
|
83
|
3 |
|
}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Handles the request and delivers the response. |
|
92
|
|
|
* |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $handler->handle($request); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Process the request and delivers the response with error management. |
|
102
|
|
|
* |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function run() : bool |
|
105
|
|
|
{ |
|
106
|
|
|
try { |
|
107
|
|
|
// ensure boot |
|
108
|
3 |
|
$this->boot(); |
|
109
|
3 |
|
$request = $this['uSilex.request']; |
|
110
|
1 |
|
$handler = $this['uSilex.httpHandler']; |
|
111
|
1 |
|
$response = $this->process($request, $handler); |
|
112
|
|
|
|
|
113
|
1 |
|
call_user_func($this['uSilex.responseEmitter'], $response, $this); |
|
114
|
|
|
|
|
115
|
1 |
|
$result = true; |
|
116
|
2 |
|
} catch (Exception $e) { |
|
117
|
2 |
|
if (isset($this['uSilex.exceptionHandler'])) { |
|
118
|
1 |
|
$response = isset($request) |
|
119
|
|
|
? call_user_func($this['uSilex.exceptionHandler'], $e, $request) |
|
120
|
1 |
|
: call_user_func($this['uSilex.exceptionHandler'], $e); |
|
121
|
1 |
|
call_user_func($this['uSilex.responseEmitter'], $response); |
|
122
|
|
|
} else { |
|
123
|
1 |
|
header('X-PHP-Response-Code: '.$e->getCode(), true, 500); |
|
124
|
1 |
|
echo $e->getMessage(); |
|
125
|
|
|
} |
|
126
|
2 |
|
$result = false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
3 |
|
return $result; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|