|
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 Exception;
|
|
17
|
|
|
|
|
18
|
|
|
/**
|
|
19
|
|
|
* The uSilex framework class.
|
|
20
|
|
|
*/
|
|
21
|
|
|
class Application extends Container
|
|
22
|
|
|
{
|
|
23
|
|
|
|
|
24
|
|
|
protected $providers = [];
|
|
25
|
|
|
protected $booted = false;
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/**
|
|
29
|
|
|
* Redefine Registers a service provider.
|
|
30
|
|
|
*
|
|
31
|
|
|
* @param ServiceProviderInterface $provider A ServiceProviderInterface instance
|
|
32
|
|
|
* @param array $values An array of values that customizes the provider
|
|
33
|
|
|
*
|
|
34
|
|
|
*/
|
|
35
|
1 |
|
public function register(ServiceProviderInterface $provider, array $values = []) : self
|
|
36
|
|
|
{
|
|
37
|
1 |
|
$this->providers[] = $provider;
|
|
38
|
|
|
|
|
39
|
1 |
|
parent::register($provider, $values);
|
|
40
|
|
|
|
|
41
|
1 |
|
return $this;
|
|
42
|
|
|
}
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/**
|
|
46
|
|
|
* Boots all service providers.
|
|
47
|
|
|
*
|
|
48
|
|
|
* This method is automatically called by handle(), but you can use it
|
|
49
|
|
|
* to boot all service providers when not handling a request.
|
|
50
|
|
|
*/
|
|
51
|
5 |
|
public function boot() : self
|
|
52
|
|
|
{
|
|
53
|
5 |
|
if ($this->booted) {
|
|
54
|
|
|
return this;
|
|
|
|
|
|
|
55
|
|
|
}
|
|
56
|
|
|
|
|
57
|
5 |
|
$this->booted = true;
|
|
58
|
|
|
|
|
59
|
5 |
|
foreach ($this->providers as $provider) {
|
|
60
|
1 |
|
if ( ($provider instanceof ServiceProviderInterface) && method_exists($provider,'boot') ) {
|
|
61
|
1 |
|
$provider->boot($this);
|
|
62
|
|
|
}
|
|
63
|
|
|
}
|
|
64
|
|
|
|
|
65
|
5 |
|
return $this;
|
|
66
|
|
|
}
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/**
|
|
70
|
|
|
* Handles the request and delivers the response.
|
|
71
|
|
|
*
|
|
72
|
|
|
*/
|
|
73
|
4 |
|
public function run() : bool
|
|
74
|
|
|
{
|
|
75
|
|
|
try {
|
|
76
|
4 |
|
if( !isset($this['response.emit'])){
|
|
77
|
|
|
$this['response.emit'] = $this->protect( function(){} );
|
|
78
|
|
|
}
|
|
79
|
|
|
|
|
80
|
4 |
|
$this->boot();
|
|
81
|
4 |
|
$response = $this['kernel']->handle($this['request']);
|
|
82
|
|
|
|
|
83
|
1 |
|
call_user_func($this['response.emit'],$response);
|
|
84
|
|
|
|
|
85
|
1 |
|
$result = true;
|
|
86
|
3 |
|
} catch (Exception $e) {
|
|
87
|
3 |
|
$result = false;
|
|
88
|
3 |
|
if( isset($this['uSilex.errorManagement']) && !$this['uSilex.errorManagement']) {
|
|
89
|
1 |
|
throw $e;
|
|
90
|
2 |
|
} elseif (isset($this['uSilex.errorManagement'])) {
|
|
91
|
1 |
|
call_user_func($this['uSilex.errorManagement'],$e);
|
|
92
|
|
|
} else {
|
|
93
|
1 |
|
header('X-PHP-Response-Code: '. $e->getCode(), true, 500);
|
|
94
|
1 |
|
echo "Internal error. " . $e->getMessage();
|
|
95
|
1 |
|
$this['uSilex.panic.error'] = $e;
|
|
96
|
|
|
}
|
|
97
|
|
|
}
|
|
98
|
|
|
|
|
99
|
3 |
|
return $result;
|
|
100
|
|
|
}
|
|
101
|
|
|
|
|
102
|
|
|
} |