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