/
Application.php
executable file
·173 lines (145 loc) · 4.3 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
<?php
namespace Pagekit\Framework;
use Pagekit\Framework\Exception\ExceptionListenerWrapper;
use Pagekit\Framework\Provider\EventServiceProvider;
use Pagekit\Framework\Provider\RoutingServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\TerminableInterface;
class Application extends \Pimple implements HttpKernelInterface, TerminableInterface
{
const EARLY_EVENT = 512;
const LATE_EVENT = -512;
protected $providers = [];
protected $booted = false;
/**
* Constructor.
*
* @param array $values
*/
public function __construct(array $values = [])
{
parent::__construct($values);
$this['app'] = $this;
$this->register(new EventServiceProvider);
$this->register(new RoutingServiceProvider);
ApplicationTrait::setApplication($this);
}
/**
* Registers a service provider.
*
* @param ServiceProviderInterface|string $provider
* @param array $values
* @throws \InvalidArgumentException
* @return Application
*/
public function register($provider, array $values = [])
{
if (is_string($provider)) {
$provider = new $provider;
}
if (!$provider instanceof ServiceProviderInterface) {
throw new \InvalidArgumentException('Provider must implement the ServiceProviderInterface.');
}
$this->providers[] = $provider;
$provider->register($this);
foreach ($values as $key => $value) {
$this[$key] = $value;
}
return $this;
}
/**
* Boots all service providers.
*
* This method is automatically called by handle(), but you can use it
* to boot all service providers when not handling a request.
*/
public function boot()
{
if (!$this->booted) {
foreach ($this->providers as $provider) {
$provider->boot($this);
}
$this->booted = true;
}
}
/**
* Adds an event listener that listens on the specified events.
*
* @param string $event
* @param mixed $callback
* @param int $priority
*/
public function on($event, $callback, $priority = 0)
{
$this['events']->addListener($event, $callback, $priority);
}
/**
* Aborts the current request by sending a proper HTTP error.
*
* @param int $code
* @param string $message
* @param array $headers
* @throws HttpException
* @throws NotFoundHttpException
*/
public function abort($code, $message = '', array $headers = [])
{
$this['router']->abort($code, $message, $headers);
}
/**
* Registers an error handler.
*
* @param mixed $callback
* @param integer $priority
*/
public function error($callback, $priority = -8)
{
$this->on(KernelEvents::EXCEPTION, new ExceptionListenerWrapper($this, $callback), $priority);
}
/**
* Handles the request and delivers the response.
*
* @param Request $request
*/
public function run(Request $request = null)
{
if ($request === null) {
$request = Request::createFromGlobals();
}
$response = $this->handle($request);
$response->send();
$this->terminate($request, $response);
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
if (!$this->booted) {
$this->boot();
}
$this['request'] = $request;
return $this['router']->handle($request, $type, $catch);
}
/**
* {@inheritdoc}
*/
public function terminate(Request $request, Response $response)
{
$this['router']->terminate($request, $response);
}
/**
* Determine if we are running in the console.
*
* @return bool
*/
public function runningInConsole()
{
return PHP_SAPI == 'cli';
}
}