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