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