1
|
|
|
<?php /** Micro */ |
2
|
|
|
|
3
|
|
|
namespace Micro; |
4
|
|
|
|
5
|
|
|
use Micro\base\Container; |
6
|
|
|
use Micro\base\Dispatcher; |
7
|
|
|
use Micro\base\IContainer; |
8
|
|
|
use Micro\cli\Consoles\DefaultConsoleCommand; |
9
|
|
|
use Micro\resolver\ConsoleResolver; |
10
|
|
|
use Micro\resolver\HMVCResolver; |
11
|
|
|
use Micro\web\IOutput; |
12
|
|
|
use Micro\web\IRequest; |
13
|
|
|
use Micro\web\Response; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Micro class file. |
17
|
|
|
* |
18
|
|
|
* Base class for initialize MicroPHP, used as bootstrap framework. |
19
|
|
|
* |
20
|
|
|
* @author Oleg Lunegov <[email protected]> |
21
|
|
|
* @link https://github.com/lugnsk/micro |
22
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
23
|
|
|
* @license /LICENSE |
24
|
|
|
* @package micro |
25
|
|
|
* @version 1.0 |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
|
|
class Micro |
29
|
|
|
{ |
30
|
|
|
/** @const string VERSION Version framework */ |
31
|
|
|
const VERSION = '1.1'; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** @var bool $loaded Micro loaded flag */ |
35
|
|
|
private $loaded; |
36
|
|
|
/** @var bool $debug Debug-mode flag */ |
37
|
|
|
private $debug = true; |
38
|
|
|
/** @var string $environment Application environment */ |
39
|
|
|
private $environment = 'devel'; |
40
|
|
|
/** @var float $startTime Time of start framework */ |
41
|
|
|
private $startTime; |
42
|
|
|
|
43
|
|
|
/** @var IContainer $container Container is a container for components and options */ |
44
|
|
|
protected $container; |
45
|
|
|
/** @var string $appDir */ |
46
|
|
|
protected $appDir; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initialize application |
51
|
|
|
* |
52
|
|
|
* @access public |
53
|
|
|
* |
54
|
|
|
* @param string $environment Application environment: devel , production , test, other |
55
|
|
|
* @param bool $debug Debug-mode flag |
56
|
|
|
* |
57
|
|
|
* @result void |
58
|
|
|
*/ |
59
|
|
|
public function __construct($environment = 'devel', $debug = true) |
60
|
|
|
{ |
61
|
|
|
$this->environment = (string)$environment; |
62
|
|
|
$this->debug = (bool)$debug; |
63
|
|
|
$this->loaded = false; |
64
|
|
|
|
65
|
|
|
ini_set('display_errors', $this->debug); |
66
|
|
|
|
67
|
|
|
/** @TODO: add handler for fatal errors... */ |
68
|
|
|
|
69
|
|
|
if ($this->debug) { |
70
|
|
|
$this->startTime = microtime(true); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Clone application |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function __clone() |
82
|
|
|
{ |
83
|
|
|
if ($this->debug) { // start new timer |
84
|
|
|
$this->startTime = microtime(true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->loaded = false; // deactivate loaded |
88
|
|
|
$this->container = null; // remove configured container |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Initialization container |
93
|
|
|
* |
94
|
|
|
* @access protected |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
protected function initializeContainer() |
98
|
|
|
{ |
99
|
|
|
$class = $this->getContainerClass(); |
100
|
|
|
|
101
|
|
|
if ($class) { |
102
|
|
|
$class = new $class; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->container = ($class instanceof IContainer) ? $class : new Container; |
106
|
|
|
|
107
|
|
|
$this->container->kernel = $this; |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->container->load($this->getConfig()); |
110
|
|
|
|
111
|
|
|
if (false === $this->container->dispatcher) { |
|
|
|
|
112
|
|
|
$this->container->dispatcher = new Dispatcher; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->addListener('kernel.kill', function(array $params) { |
116
|
|
|
if ($params['container']->kernel->isDebug() && !$params['container']->request->isCli()) { |
117
|
|
|
// Add timer into page |
118
|
|
|
echo '<div class=debug_timer>', (microtime(true) - $this->getStartTime()), '</div>'; |
119
|
|
|
} |
120
|
|
|
}); |
121
|
|
|
|
122
|
|
|
$this->container->dispatcher->signal('kernel.boot', ['container' => $this->container]); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Running application |
127
|
|
|
* |
128
|
|
|
* @access public |
129
|
|
|
* |
130
|
|
|
* @param IRequest $request Request object |
131
|
|
|
* |
132
|
|
|
* @return Response |
133
|
|
|
* @throws \Exception |
134
|
|
|
*/ |
135
|
|
|
public function run(IRequest $request) |
136
|
|
|
{ |
137
|
|
|
if (!$this->loaded) { |
138
|
|
|
$this->initializeContainer(); |
139
|
|
|
|
140
|
|
|
$this->loaded = true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->container->request = $request; |
|
|
|
|
144
|
|
|
|
145
|
|
|
$this->container->dispatcher->signal('kernel.request', ['container' => $this->container]); |
|
|
|
|
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
return $this->doRun(); // run application |
149
|
|
|
} catch (\Exception $e) { // if not caught exception |
150
|
|
|
if ($this->debug) { |
151
|
|
|
$this->container->dispatcher->signal('kernel.exception', ['exception' => $e]); |
|
|
|
|
152
|
|
|
throw $e; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->doException($e); // run exception |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Starting ... |
161
|
|
|
* |
162
|
|
|
* @access private |
163
|
|
|
* |
164
|
|
|
* @return \Micro\web\IResponse |
165
|
|
|
* @throws \Micro\base\Exception |
166
|
|
|
*/ |
167
|
|
|
private function doRun() |
168
|
|
|
{ |
169
|
|
|
$resolver = $this->getResolver(); |
170
|
|
|
$this->container->dispatcher->signal('kernel.router', ['resolver' => $resolver]); |
|
|
|
|
171
|
|
|
|
172
|
|
|
$app = $resolver->getApplication(); |
173
|
|
|
$this->container->dispatcher->signal('kernel.controller', ['application' => $app]); |
|
|
|
|
174
|
|
|
|
175
|
|
|
$output = $app->action($resolver->getAction()); |
176
|
|
|
if (!$output instanceof IOutput) { |
177
|
|
|
$response = $this->container->response ?: new Response; |
|
|
|
|
178
|
|
|
$response->setBody($output); |
179
|
|
|
$output = $response; |
180
|
|
|
} |
181
|
|
|
$this->container->dispatcher->signal('kernel.response', ['output' => $output]); |
|
|
|
|
182
|
|
|
|
183
|
|
|
return $output; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Do exception |
188
|
|
|
* |
189
|
|
|
* @access private |
190
|
|
|
* |
191
|
|
|
* @param \Exception $e Exception |
192
|
|
|
* |
193
|
|
|
* @return IOutput |
194
|
|
|
* @throws \Micro\base\Exception |
195
|
|
|
*/ |
196
|
|
|
private function doException(\Exception $e) |
197
|
|
|
{ |
198
|
|
|
$output = $this->container->request->isCli() ? new DefaultConsoleCommand([]) : new Response(); |
|
|
|
|
199
|
|
|
|
200
|
|
|
if ($this->container->request->isCli()) { |
|
|
|
|
201
|
|
|
$output->data = '"Error #' . $e->getCode() . ' - ' . $e->getMessage() . '"'; |
202
|
|
|
$output->execute(); |
|
|
|
|
203
|
|
|
|
204
|
|
|
return $output; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (!$this->container->errorController) { |
|
|
|
|
208
|
|
|
$output->setBody('Option `errorController` not configured'); |
|
|
|
|
209
|
|
|
|
210
|
|
|
return $output; |
211
|
|
|
} |
212
|
|
|
if (!$this->container->errorAction) { |
|
|
|
|
213
|
|
|
$output->setBody('Option `errorAction` not configured'); |
214
|
|
|
|
215
|
|
|
return $output; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$controller = $this->container->errorController; |
|
|
|
|
219
|
|
|
$action = $this->container->errorAction; |
|
|
|
|
220
|
|
|
|
221
|
|
|
$this->container->request->setPost('error', $e); |
|
|
|
|
222
|
|
|
|
223
|
|
|
/** @var \Micro\mvc\controllers\IController $result */ |
224
|
|
|
$result = new $controller($this->container, false); |
225
|
|
|
$result = $result->action($action); |
226
|
|
|
|
227
|
|
|
if ($result instanceof IOutput) { |
228
|
|
|
return $result; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$output->setBody($result); |
|
|
|
|
232
|
|
|
|
233
|
|
|
return $output; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Terminate application |
238
|
|
|
* |
239
|
|
|
* @access public |
240
|
|
|
* |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
public function terminate() |
244
|
|
|
{ |
245
|
|
|
$this->container->dispatcher->signal('kernel.kill', ['container' => $this->container]); |
|
|
|
|
246
|
|
|
|
247
|
|
|
$this->unloader(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Add listener on event |
252
|
|
|
* |
253
|
|
|
* @access public |
254
|
|
|
* |
255
|
|
|
* @param string $listener listener name |
256
|
|
|
* @param mixed $event ['Object', 'method'] or callable |
257
|
|
|
* @param int|null $prior priority |
258
|
|
|
* |
259
|
|
|
* @return void |
260
|
|
|
*/ |
261
|
|
|
protected function addListener($listener, $event, $prior = null) |
262
|
|
|
{ |
263
|
|
|
if (!is_string($listener) || !$this->container) { |
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$this->container->dispatcher->addListener($listener, $event, $prior); |
|
|
|
|
268
|
|
|
|
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
// Methods for components |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Default config path |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
protected function getConfig() |
280
|
|
|
{ |
281
|
|
|
return $this->getAppDir() . '/configs/main.php'; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get full class name |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
protected function getContainerClass() |
289
|
|
|
{ |
290
|
|
|
return ''; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get resolver |
295
|
|
|
* |
296
|
|
|
* @access protected |
297
|
|
|
* |
298
|
|
|
* @param bool|false $isCli CLI or Web |
|
|
|
|
299
|
|
|
* |
300
|
|
|
* @return ConsoleResolver|HMVCResolver |
301
|
|
|
*/ |
302
|
|
|
protected function getResolver() |
303
|
|
|
{ |
304
|
|
|
if ($this->container->request->isCli()) { |
|
|
|
|
305
|
|
|
return new ConsoleResolver($this->container); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return new HMVCResolver($this->container); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get status of debug |
313
|
|
|
* |
314
|
|
|
* @access public |
315
|
|
|
* |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
public function isDebug() |
319
|
|
|
{ |
320
|
|
|
return $this->debug; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Get start time |
325
|
|
|
* |
326
|
|
|
* @access public |
327
|
|
|
* |
328
|
|
|
* @return float|null |
329
|
|
|
*/ |
330
|
|
|
public function getStartTime() |
331
|
|
|
{ |
332
|
|
|
return $this->startTime; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Methods helpers |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Unloader subsystem |
339
|
|
|
* |
340
|
|
|
* @access public |
341
|
|
|
* |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
|
public function unloader() |
345
|
|
|
{ |
346
|
|
|
if (false === $this->loaded) { |
347
|
|
|
return; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$this->container = null; |
351
|
|
|
$this->loaded = false; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Get environment name |
356
|
|
|
* |
357
|
|
|
* @access public |
358
|
|
|
* |
359
|
|
|
* @return string |
360
|
|
|
*/ |
361
|
|
|
public function getEnvironment() |
362
|
|
|
{ |
363
|
|
|
return $this->environment; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Get components container |
368
|
|
|
* |
369
|
|
|
* @access public |
370
|
|
|
* |
371
|
|
|
* @return IContainer |
372
|
|
|
*/ |
373
|
|
|
public function getContainer() |
374
|
|
|
{ |
375
|
|
|
return $this->container; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Get character set |
380
|
|
|
* |
381
|
|
|
* @access public |
382
|
|
|
* |
383
|
|
|
* @return string |
384
|
|
|
*/ |
385
|
|
|
public function getCharset() |
386
|
|
|
{ |
387
|
|
|
return 'UTF-8'; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Get application directory |
392
|
|
|
* |
393
|
|
|
* @return string |
394
|
|
|
*/ |
395
|
|
|
public function getAppDir() |
396
|
|
|
{ |
397
|
|
|
if (!$this->appDir) { |
398
|
|
|
$this->appDir = dirname((new \ReflectionObject($this))->getFileName()); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
return $this->appDir; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Get logs directory |
406
|
|
|
* |
407
|
|
|
* @return string |
408
|
|
|
*/ |
409
|
|
|
public function getLogDir() |
410
|
|
|
{ |
411
|
|
|
return $this->getAppDir() . '/logs'; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Get cache directory |
416
|
|
|
* |
417
|
|
|
* @return string |
418
|
|
|
*/ |
419
|
|
|
public function getCacheDir() |
420
|
|
|
{ |
421
|
|
|
return $this->getAppDir() . '/cache/' . $this->getEnvironment(); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: