|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Starlit App. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016 Starweb AB |
|
6
|
|
|
* @license BSD 3-Clause |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Starlit\App; |
|
10
|
|
|
|
|
11
|
|
|
use Starlit\App\Container\Container; |
|
12
|
|
|
use Starlit\App\Provider\BootableServiceProviderInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
16
|
|
|
use Starlit\App\Provider\ServiceProviderInterface; |
|
17
|
|
|
use Starlit\App\Provider\StandardServiceProvider; |
|
18
|
|
|
use Starlit\App\Provider\ErrorServiceProvider; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Main framework application and bootstrap class, which also serves as a micro service/dependency injection container. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Andreas Nilsson <http://github.com/jandreasn> |
|
24
|
|
|
*/ |
|
25
|
|
|
class BaseApp extends Container |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @const string |
|
29
|
|
|
*/ |
|
30
|
|
|
const CHARSET = 'UTF-8'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Config |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $config; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $environment; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ServiceProviderInterface[] |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $providers = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $booted = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var bool |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $isCli = false; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array|Config $config |
|
61
|
|
|
* @param string $environment Defaults to "production" |
|
62
|
|
|
*/ |
|
63
|
21 |
|
public function __construct($config = [], $environment = 'production') |
|
64
|
|
|
{ |
|
65
|
21 |
|
if ($config instanceof Config) { |
|
66
|
|
|
$this->config = $config; |
|
67
|
|
|
} else { |
|
68
|
21 |
|
$this->config = new Config($config); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
21 |
|
$this->environment = $environment; |
|
72
|
|
|
|
|
73
|
21 |
|
$this->init(); |
|
74
|
21 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Initializes the application object. |
|
78
|
|
|
|
|
79
|
|
|
* Override and put initialization code that should always be run as early as |
|
80
|
|
|
* possible here, but make sure no objects are actually instanced here, because then |
|
81
|
|
|
* mock objects can't be injected in their place. Place object instance code in |
|
82
|
|
|
* the preHandle method. |
|
83
|
|
|
*/ |
|
84
|
21 |
|
protected function init() |
|
85
|
|
|
{ |
|
86
|
21 |
|
$this->isCli = (PHP_SAPI === 'cli'); |
|
87
|
|
|
|
|
88
|
21 |
|
if ($this->config->has('phpSettings')) { |
|
89
|
21 |
|
$this->setPhpSettings($this->config->get('phpSettings')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
21 |
|
$this->registerProviders(); |
|
93
|
21 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Register service providers. |
|
97
|
|
|
*/ |
|
98
|
21 |
|
protected function registerProviders() |
|
99
|
|
|
{ |
|
100
|
21 |
|
$this->register(new ErrorServiceProvider()); |
|
101
|
21 |
|
$this->register(new StandardServiceProvider()); |
|
102
|
21 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Register service provider. |
|
106
|
|
|
* |
|
107
|
|
|
* @param ServiceProviderInterface $provider |
|
108
|
|
|
*/ |
|
109
|
21 |
|
public function register(ServiceProviderInterface $provider) |
|
110
|
|
|
{ |
|
111
|
21 |
|
$this->providers[] = $provider; |
|
112
|
|
|
|
|
113
|
21 |
|
$provider->register($this); |
|
114
|
21 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $phpSettings |
|
118
|
|
|
* @param string $prefix |
|
119
|
|
|
*/ |
|
120
|
21 |
|
protected function setPhpSettings($phpSettings, $prefix = '') |
|
121
|
|
|
{ |
|
122
|
21 |
|
foreach ($phpSettings as $key => $val) { |
|
123
|
21 |
|
$key = $prefix . $key; |
|
124
|
21 |
|
if (is_scalar($val)) { |
|
125
|
21 |
|
ini_set($key, $val); |
|
126
|
21 |
|
} elseif (is_array($val)) { |
|
127
|
21 |
|
$this->setPhpSettings($val, $key . '.'); // Set sub setting with a recursive call |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
21 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Boot the application and its service providers. |
|
134
|
|
|
* |
|
135
|
|
|
* This is normally called by handle(). If requests are not handled |
|
136
|
|
|
* this method will have to called manually to boot. |
|
137
|
|
|
*/ |
|
138
|
5 |
|
public function boot() |
|
139
|
|
|
{ |
|
140
|
5 |
|
if ($this->booted) { |
|
141
|
|
|
return; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
5 |
|
foreach ($this->providers as $provider) { |
|
145
|
5 |
|
if ($provider instanceof BootableServiceProviderInterface) { |
|
146
|
5 |
|
$provider->boot($this); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
5 |
|
$this->booted = true; |
|
151
|
5 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Pre handle method meant to be overridden in descendant classes (optional). |
|
155
|
|
|
* |
|
156
|
|
|
* This method is called before an request is handled. Object instance code should be |
|
157
|
|
|
* place here and not in init() (more info about this at init()). |
|
158
|
|
|
* |
|
159
|
|
|
* @param Request $request |
|
160
|
|
|
* @return Response|null |
|
161
|
|
|
*/ |
|
162
|
3 |
|
protected function preHandle(Request $request) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
3 |
|
return null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Post route method meant to be overridden in descendant classes (optional). |
|
169
|
|
|
* This method is called before an request is dispatched but after it's routed. This means that we know |
|
170
|
|
|
* it's a valid route and have access to the route attributes at this stage. |
|
171
|
|
|
* |
|
172
|
|
|
* @param Request $request |
|
173
|
|
|
* @return Response|null |
|
174
|
|
|
*/ |
|
175
|
1 |
|
protected function postRoute(Request $request) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
1 |
|
return null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Handles an http request and returns a response. |
|
182
|
|
|
* |
|
183
|
|
|
* @param Request $request |
|
184
|
|
|
* @return Response |
|
185
|
|
|
*/ |
|
186
|
4 |
|
public function handle(Request $request) |
|
187
|
|
|
{ |
|
188
|
4 |
|
$this->alias('request', Request::class); |
|
189
|
4 |
|
$this->set(Request::class, $request); |
|
190
|
|
|
|
|
191
|
4 |
|
$this->boot(); |
|
192
|
|
|
|
|
193
|
4 |
|
if (($preHandleResponse = $this->preHandle($request))) { |
|
194
|
1 |
|
return $preHandleResponse; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
try { |
|
198
|
3 |
|
$controller = $this->get(RouterInterface::class)->route($request); |
|
199
|
|
|
|
|
200
|
2 |
|
if (($postRouteResponse = $this->postRoute($request))) { |
|
201
|
1 |
|
return $postRouteResponse; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
1 |
|
$response = $controller->dispatch(); |
|
205
|
1 |
|
} catch (ResourceNotFoundException $e) { |
|
206
|
1 |
|
$response = $this->getNoRouteResponse($request); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
2 |
|
$this->postHandle($request); |
|
210
|
|
|
|
|
211
|
2 |
|
return $response; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Returns a response for no route / resource not found. |
|
216
|
|
|
* |
|
217
|
|
|
* @param Request $request |
|
218
|
|
|
* @return Response |
|
219
|
|
|
*/ |
|
220
|
1 |
|
protected function getNoRouteResponse(Request $request) |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
1 |
|
return new Response('Not Found', 404); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Post handle method meant to be overridden in descendant classes (optional). |
|
227
|
|
|
* This method is called after an request has been handled but before |
|
228
|
|
|
* the response is returned from the handle method. |
|
229
|
|
|
* |
|
230
|
|
|
* @param Request $request |
|
231
|
|
|
*/ |
|
232
|
2 |
|
protected function postHandle(Request $request) |
|
|
|
|
|
|
233
|
|
|
{ |
|
234
|
2 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return Config |
|
238
|
|
|
*/ |
|
239
|
21 |
|
public function getConfig() |
|
240
|
|
|
{ |
|
241
|
21 |
|
return $this->config; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return bool |
|
246
|
|
|
*/ |
|
247
|
21 |
|
public function isCli(): bool |
|
248
|
|
|
{ |
|
249
|
21 |
|
return $this->isCli; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return Request|null |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getRequest() |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->has(Request::class) ? $this->get(Request::class) : null; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @return string |
|
262
|
|
|
*/ |
|
263
|
1 |
|
public function getEnvironment() |
|
264
|
|
|
{ |
|
265
|
1 |
|
return $this->environment; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.