|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\base; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Web\RequestInjector; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Kernel implements KernelInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** @const string VERSION Version framework */ |
|
11
|
|
|
const VERSION = '2.0'; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string $appDir */ |
|
14
|
|
|
protected $appDir; |
|
15
|
|
|
/** @var string $webDir */ |
|
16
|
|
|
protected $webDir; |
|
17
|
|
|
|
|
18
|
|
|
/** @var bool $debug Debug-mode flag */ |
|
19
|
|
|
private $debug = true; |
|
20
|
|
|
/** @var string $environment Application environment */ |
|
21
|
|
|
private $environment = 'devel'; |
|
22
|
|
|
/** @var float $startTime Time of start framework */ |
|
23
|
|
|
private $startTime; |
|
24
|
|
|
|
|
25
|
|
|
/** @var bool $loaded Micro loaded flag */ |
|
26
|
|
|
private $loaded; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Kernel constructor. |
|
31
|
|
|
* @param string $environment |
|
32
|
|
|
* @param bool $debug |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($environment, $debug = false) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->webDir = getenv('DOCUMENT_ROOT'); |
|
37
|
|
|
$this->environment = (string)$environment; |
|
38
|
|
|
$this->debug = (bool)$debug; |
|
39
|
|
|
$this->loaded = false; |
|
40
|
|
|
|
|
41
|
|
|
ini_set('display_errors', (integer)$this->debug); |
|
42
|
|
|
ini_set('log_errors', (integer)$this->debug); |
|
43
|
|
|
|
|
44
|
|
|
if ($this->debug) { |
|
45
|
|
|
ini_set('error_reporting', -1); |
|
46
|
|
|
$this->startTime = microtime(true); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Clone kernel |
|
52
|
|
|
* |
|
53
|
|
|
* @access public |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __clone() |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->debug) { |
|
60
|
|
|
$this->startTime = microtime(true); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->loaded = false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
final public function loadInjectorsFromCache() |
|
67
|
|
|
{ |
|
68
|
|
|
// load injectors |
|
69
|
|
|
$injectors = ['name' => 'value_object']; |
|
70
|
|
|
|
|
71
|
|
|
// insert injectors from cache |
|
72
|
|
|
$baseInjector = new Injector; |
|
73
|
|
|
foreach ($injectors as $name => $injector) { |
|
74
|
|
|
$baseInjector->addRequirement($name, $injector); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function initialize(ServerRequestInterface $request) |
|
79
|
|
|
{ |
|
80
|
|
|
$inject = new Injector($this->getConfig()); |
|
81
|
|
|
$inject->addRequirement('kernel', $this); |
|
82
|
|
|
$inject->addRequirement('request', $request); |
|
83
|
|
|
|
|
84
|
|
|
$dispatcherInjector = new DispatcherInjector; |
|
85
|
|
|
try { |
|
86
|
|
|
$dispatcher = $dispatcherInjector->build(); |
|
87
|
|
|
} catch (Exception $e) { |
|
88
|
|
|
$dispatcher = new Dispatcher; |
|
89
|
|
|
$dispatcherInjector->addRequirement('dispatcher', $dispatcher); |
|
90
|
|
|
} |
|
91
|
|
|
$dispatcher->signal('kernel.boot', ['injector' => $inject]); |
|
92
|
|
|
|
|
93
|
|
|
// Hack - killer application |
|
94
|
|
|
(new DispatcherInjector)->build()->addListener('kernel.kill', function () { |
|
95
|
|
|
$params = (new RequestInjector)->build()->getServerParams(); |
|
96
|
|
|
$isAjax = strtolower( |
|
97
|
|
|
filter_var(!empty($params['HTTP_X_REQUESTED_WITH']) ? $params['HTTP_X_REQUESTED_WITH'] : null) |
|
98
|
|
|
) === 'xmlhttprequest'; |
|
99
|
|
|
|
|
100
|
|
|
if ($this->isDebug() && !$this->isCli() && !$isAjax) { |
|
101
|
|
|
echo '<div class="debug_timer">', (microtime(true) - $this->getStartTime()), '</div>'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (false === $this->loaded) { |
|
105
|
|
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->loaded = false; |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
$this->loaded = true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getConfig() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getAppDir() . '/configs/index.php'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getAppDir() |
|
126
|
|
|
{ |
|
127
|
|
|
if (!$this->appDir) { |
|
128
|
|
|
$this->appDir = realpath(dirname((new \ReflectionObject($this))->getFileName())); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->appDir; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function isDebug() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->debug; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function isCli() |
|
146
|
|
|
{ |
|
147
|
|
|
return PHP_SAPI === 'cli'; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return mixed |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getStartTime() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->startTime; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return mixed |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getWebDir() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->webDir; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function getCacheDir() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->getAppDir() . '/cache/' . $this->getEnvironment(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getEnvironment() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->environment; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getLogDir() |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->getAppDir() . '/logs'; |
|
182
|
|
|
} |
|
183
|
|
|
} |