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