|
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 Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
15
|
|
|
use Starlit\App\Provider\ServiceProviderInterface; |
|
16
|
|
|
use Starlit\App\Provider\StandardServiceProvider; |
|
17
|
|
|
use Starlit\App\Provider\ErrorServiceProvider; |
|
18
|
|
|
|
|
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 |
|
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 array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $dicValues = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private $dicObjects = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array|Config $config |
|
56
|
|
|
* @param string $environment Defaults to "production" |
|
57
|
|
|
*/ |
|
58
|
19 |
|
public function __construct($config = [], $environment = 'production') |
|
59
|
|
|
{ |
|
60
|
19 |
|
if ($config instanceof Config) { |
|
61
|
|
|
$this->config = $config; |
|
62
|
|
|
} else { |
|
63
|
19 |
|
$this->config = new Config($config); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
19 |
|
$this->environment = $environment; |
|
67
|
|
|
|
|
68
|
19 |
|
$this->init(); |
|
69
|
19 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Initializes the application. |
|
73
|
|
|
* |
|
74
|
|
|
* Put initialization code that should always be run here, but (NB!) make sure |
|
75
|
|
|
* no objects are actually instanced here, because then mock objects can't be |
|
76
|
|
|
* injected in their place. Place object instance code in the preHandle method. |
|
77
|
|
|
*/ |
|
78
|
19 |
|
protected function init() |
|
79
|
|
|
{ |
|
80
|
19 |
|
$this->set('cli', (PHP_SAPI === 'cli')); |
|
81
|
|
|
|
|
82
|
19 |
|
if ($this->config->has('phpSettings')) { |
|
83
|
19 |
|
$this->setPhpSettings($this->config->get('phpSettings')); |
|
84
|
19 |
|
} |
|
85
|
|
|
|
|
86
|
19 |
|
$this->register(new ErrorServiceProvider()); |
|
87
|
19 |
|
$this->register(new StandardServiceProvider()); |
|
88
|
19 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $phpSettings |
|
92
|
|
|
* @param string $prefix |
|
93
|
|
|
*/ |
|
94
|
19 |
|
protected function setPhpSettings($phpSettings, $prefix = '') |
|
95
|
|
|
{ |
|
96
|
19 |
|
foreach ($phpSettings as $key => $val) { |
|
97
|
19 |
|
$key = $prefix . $key; |
|
98
|
19 |
|
if (is_scalar($val)) { |
|
99
|
19 |
|
ini_set($key, $val); |
|
100
|
19 |
|
} elseif (is_array($val)) { |
|
101
|
19 |
|
$this->setPhpSettings($val, $key . '.'); // Set sub setting with a recursive call |
|
102
|
19 |
|
} |
|
103
|
19 |
|
} |
|
104
|
19 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Register service provider. |
|
108
|
|
|
* |
|
109
|
|
|
* @param ServiceProviderInterface $serviceProvider |
|
110
|
|
|
*/ |
|
111
|
19 |
|
public function register(ServiceProviderInterface $serviceProvider) |
|
112
|
|
|
{ |
|
113
|
19 |
|
$serviceProvider->register($this); |
|
114
|
19 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Pre handle method meant to be overridden in descendant classes (optional). |
|
118
|
|
|
* |
|
119
|
|
|
* This method is called before an request is handled. Object instance code should be |
|
120
|
|
|
* place here and not in init() (more info about this at init()). |
|
121
|
|
|
* |
|
122
|
|
|
* @param Request $request |
|
123
|
|
|
* @return Response|null |
|
124
|
|
|
*/ |
|
125
|
3 |
|
protected function preHandle(Request $request) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
3 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Post route method meant to be overridden in descendant classes (optional). |
|
131
|
|
|
* This method is called before an request is dispatched but after it's routed. This means that we know |
|
132
|
|
|
* it's a valid route and have access to the route attributes at this stage. |
|
133
|
|
|
* |
|
134
|
|
|
* @param Request $request |
|
135
|
|
|
* @return Response|null |
|
136
|
|
|
*/ |
|
137
|
1 |
|
protected function postRoute(Request $request) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Handles an http request and returns a response. |
|
143
|
|
|
* |
|
144
|
|
|
* @param Request $request |
|
145
|
|
|
* @return Response |
|
146
|
|
|
*/ |
|
147
|
4 |
|
public function handle(Request $request) |
|
148
|
|
|
{ |
|
149
|
4 |
|
$this->set('request', $request); |
|
150
|
|
|
|
|
151
|
4 |
|
if (($preHandleResponse = $this->preHandle($request))) { |
|
152
|
1 |
|
return $preHandleResponse; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
try { |
|
156
|
3 |
|
$controller = $this->getRouter()->route($request); |
|
157
|
|
|
|
|
158
|
2 |
|
if (($postRouteResponse = $this->postRoute($request))) { |
|
159
|
1 |
|
return $postRouteResponse; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
1 |
|
$response = $controller->dispatch(); |
|
163
|
2 |
|
} catch (ResourceNotFoundException $e) { |
|
164
|
1 |
|
$response = $this->getNoRouteResponse($request); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
2 |
|
$this->postHandle($request); |
|
168
|
|
|
|
|
169
|
2 |
|
return $response; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns a response for no route / resource not found. |
|
174
|
|
|
* |
|
175
|
|
|
* @param Request $request |
|
176
|
|
|
* @return Response |
|
177
|
|
|
*/ |
|
178
|
1 |
|
protected function getNoRouteResponse(Request $request) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
1 |
|
return new Response('Not Found', 404); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Post handle method meant to be overridden in descendant classes (optional). |
|
185
|
|
|
* This method is called after an request has been handled but before |
|
186
|
|
|
* the response is returned from the handle method. |
|
187
|
|
|
* |
|
188
|
|
|
* @param Request $request |
|
189
|
|
|
*/ |
|
190
|
2 |
|
protected function postHandle(Request $request) |
|
|
|
|
|
|
191
|
|
|
{ |
|
192
|
2 |
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Set a DIC value. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $key |
|
198
|
|
|
* @param mixed $value Wrap objects in a closure for lazy loading |
|
199
|
|
|
* @return BaseApp |
|
200
|
|
|
*/ |
|
201
|
19 |
|
public function set($key, $value) |
|
202
|
|
|
{ |
|
203
|
19 |
|
$this->dicValues[$key] = $value; |
|
204
|
19 |
|
unset($this->dicObjects[$key]); // In case an object instance was stored for sharing |
|
205
|
|
|
|
|
206
|
19 |
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Check if a DIC value/object exists. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $key |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
19 |
|
public function has($key) |
|
216
|
|
|
{ |
|
217
|
19 |
|
return array_key_exists($key, $this->dicValues); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param string $key |
|
222
|
|
|
* @return bool |
|
223
|
|
|
*/ |
|
224
|
2 |
|
public function hasInstance($key) |
|
225
|
|
|
{ |
|
226
|
2 |
|
return isset($this->dicObjects[$key]); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get the shared instance of a DIC object, or a DIC value if it's not an object. |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $key |
|
233
|
|
|
* @return mixed |
|
234
|
|
|
*/ |
|
235
|
19 |
|
public function get($key) |
|
236
|
|
|
{ |
|
237
|
19 |
|
if (!$this->has($key)) { |
|
238
|
1 |
|
throw new \InvalidArgumentException(sprintf('No application value with key "%s" is defined.', $key)); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
// Get already instantiated object if it exist |
|
242
|
19 |
|
if (isset($this->dicObjects[$key])) { |
|
243
|
2 |
|
return $this->dicObjects[$key]; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
// Check if it's an invokable (closure/anonymous function) |
|
247
|
19 |
|
if (is_object($this->dicValues[$key]) && method_exists($this->dicValues[$key], '__invoke')) { |
|
248
|
19 |
|
$this->dicObjects[$key] = $this->dicValues[$key]($this); |
|
249
|
|
|
|
|
250
|
19 |
|
return $this->dicObjects[$key]; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
19 |
|
return $this->dicValues[$key]; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Get new instance of a DIC object. |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $key |
|
260
|
|
|
* @return mixed |
|
261
|
|
|
*/ |
|
262
|
3 |
|
public function getNew($key) |
|
263
|
|
|
{ |
|
264
|
3 |
|
if (!array_key_exists($key, $this->dicValues)) { |
|
265
|
1 |
|
throw new \InvalidArgumentException(sprintf('No application value with key "%s" is defined.', $key)); |
|
266
|
2 |
|
} elseif (!is_object($this->dicValues[$key]) || !method_exists($this->dicValues[$key], '__invoke')) { |
|
267
|
1 |
|
throw new \InvalidArgumentException(sprintf('Application value "%s" is not invokable.', $key)); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
1 |
|
return $this->dicValues[$key]($this); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Destroy a DIC object instance. |
|
275
|
|
|
* |
|
276
|
|
|
* Will force a new object to be created on next call. |
|
277
|
|
|
* |
|
278
|
|
|
* @param string $key |
|
279
|
|
|
*/ |
|
280
|
1 |
|
public function destroyInstance($key) |
|
281
|
|
|
{ |
|
282
|
1 |
|
unset($this->dicObjects[$key]); |
|
283
|
1 |
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Destroy all DIC object instances. |
|
287
|
|
|
* |
|
288
|
|
|
* Will force new objects to be created on next call. |
|
289
|
|
|
*/ |
|
290
|
1 |
|
public function destroyAllInstances() |
|
291
|
|
|
{ |
|
292
|
1 |
|
$this->dicObjects = []; |
|
293
|
|
|
|
|
294
|
|
|
// To make sure objects (like database connections) are destructed properly. PHP might not destruct objects |
|
295
|
|
|
// until the end of execution otherwise. |
|
296
|
1 |
|
gc_collect_cycles(); |
|
297
|
1 |
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Magic method to get or set DIC values. |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $name |
|
303
|
|
|
* @param array $arguments |
|
304
|
|
|
* @return mixed |
|
305
|
|
|
*/ |
|
306
|
5 |
|
public function __call($name, $arguments) |
|
307
|
|
|
{ |
|
308
|
|
|
// getNew followed by an upper letter like getNewApple() |
|
309
|
5 |
|
if (preg_match('/^getNew([A-Z].*)/', $name, $matches)) { |
|
310
|
1 |
|
$key = lcfirst($matches[1]); |
|
311
|
|
|
|
|
312
|
1 |
|
return $this->getNew($key); |
|
313
|
4 |
|
} elseif (strpos($name, 'get') === 0) { |
|
314
|
2 |
|
$key = lcfirst(substr($name, 3)); |
|
315
|
|
|
|
|
316
|
2 |
|
return $this->get($key); |
|
317
|
3 |
|
} elseif (strpos($name, 'set') === 0) { |
|
318
|
2 |
|
$argumentCount = count($arguments); |
|
319
|
2 |
|
if ($argumentCount !== 1) { |
|
320
|
1 |
|
throw new \BadMethodCallException("Invalid argument count[{$argumentCount}] for application {$name}()"); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
1 |
|
$key = lcfirst(substr($name, 3)); |
|
324
|
|
|
|
|
325
|
1 |
|
return $this->set($key, $arguments[0]); |
|
326
|
|
|
} else { |
|
327
|
1 |
|
throw new \BadMethodCallException("No application method named {$name}()"); |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @return Config |
|
333
|
|
|
*/ |
|
334
|
19 |
|
public function getConfig() |
|
335
|
|
|
{ |
|
336
|
19 |
|
return $this->config; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @return bool |
|
341
|
|
|
*/ |
|
342
|
19 |
|
public function isCli() |
|
343
|
|
|
{ |
|
344
|
19 |
|
return $this->get('cli'); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @return Session |
|
349
|
|
|
*/ |
|
350
|
1 |
|
public function getSession() |
|
351
|
|
|
{ |
|
352
|
1 |
|
return $this->get('session'); // Makes this method faster by bypassing __call() (which is quite slow). |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @return Router |
|
357
|
|
|
*/ |
|
358
|
3 |
|
public function getRouter() |
|
359
|
|
|
{ |
|
360
|
3 |
|
return $this->get('router'); // Makes this method faster by bypassing __call() (which is quite slow). |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @return Request|null |
|
365
|
|
|
*/ |
|
366
|
2 |
|
public function getRequest() |
|
367
|
|
|
{ |
|
368
|
2 |
|
return $this->has('request') ? $this->get('request') : null; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* @return string |
|
373
|
|
|
*/ |
|
374
|
1 |
|
public function getEnvironment() |
|
375
|
|
|
{ |
|
376
|
1 |
|
return $this->environment; |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.