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