1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-kernel |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Kernel |
8
|
|
|
* @subpackage Apparat\Kernel\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Kernel\Ports; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Domain\Contract\ModuleInterface; |
40
|
|
|
use Apparat\Kernel\Infrastructure\DiceAdapter; |
41
|
|
|
use Apparat\Kernel\Infrastructure\Logger; |
42
|
|
|
use Psr\Log\LogLevel; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Kernel facade |
46
|
|
|
* |
47
|
|
|
* @package Apparat\Kernel |
48
|
|
|
* @subpackage Apparat\Kernel\Ports |
49
|
|
|
*/ |
50
|
|
|
class Kernel |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Kernel instance |
54
|
|
|
* |
55
|
|
|
* @var \Apparat\Kernel\Domain\Model\Kernel |
56
|
|
|
*/ |
57
|
|
|
protected static $kernel = null; |
58
|
|
|
|
59
|
|
|
/******************************************************************************* |
60
|
|
|
* PUBLIC METHODS |
61
|
|
|
*******************************************************************************/ |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Register an apparat module |
65
|
|
|
* |
66
|
|
|
* @param ModuleInterface $module Apparat module |
67
|
|
|
*/ |
68
|
|
|
public static function register(ModuleInterface $module) |
69
|
|
|
{ |
70
|
|
|
self::getKernel()->register($module); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create an object instance |
75
|
|
|
* |
76
|
|
|
* @param string $name Object class name |
77
|
|
|
* @param array $args Object constructor arguments |
78
|
|
|
* @return object Object instance |
79
|
|
|
*/ |
80
|
|
|
public static function create($name, array $args = []) |
81
|
|
|
{ |
82
|
|
|
return self::getKernel()->create($name, $args); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* System is unusable |
87
|
|
|
* |
88
|
|
|
* @param string $message |
89
|
|
|
* @param array $context |
90
|
|
|
* @return null |
91
|
|
|
*/ |
92
|
|
|
public static function emergency($message, array $context = array()) |
93
|
|
|
{ |
94
|
|
|
self::getKernel()->log(LogLevel::EMERGENCY, $message, $context); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Action must be taken immediately |
99
|
|
|
* |
100
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
101
|
|
|
* trigger the SMS alerts and wake you up. |
102
|
|
|
* |
103
|
|
|
* @param string $message |
104
|
|
|
* @param array $context |
105
|
|
|
* @return null |
106
|
|
|
*/ |
107
|
|
|
public static function alert($message, array $context = array()) |
108
|
|
|
{ |
109
|
|
|
self::getKernel()->log(LogLevel::ALERT, $message, $context); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Critical conditions |
114
|
|
|
* |
115
|
|
|
* Example: Application component unavailable, unexpected exception. |
116
|
|
|
* |
117
|
|
|
* @param string $message |
118
|
|
|
* @param array $context |
119
|
|
|
* @return null |
120
|
|
|
*/ |
121
|
|
|
public static function critical($message, array $context = array()) |
122
|
|
|
{ |
123
|
|
|
self::getKernel()->log(LogLevel::CRITICAL, $message, $context); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Runtime errors that do not require immediate action but should typically |
128
|
|
|
* be logged and monitored |
129
|
|
|
* |
130
|
|
|
* @param string $message |
131
|
|
|
* @param array $context |
132
|
|
|
* @return null |
133
|
|
|
*/ |
134
|
|
|
public static function error($message, array $context = array()) |
135
|
|
|
{ |
136
|
|
|
self::getKernel()->log(LogLevel::ERROR, $message, $context); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Exceptional occurrences that are not errors |
141
|
|
|
* |
142
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
143
|
|
|
* that are not necessarily wrong. |
144
|
|
|
* |
145
|
|
|
* @param string $message |
146
|
|
|
* @param array $context |
147
|
|
|
* @return null |
148
|
|
|
*/ |
149
|
|
|
public static function warning($message, array $context = array()) |
150
|
|
|
{ |
151
|
|
|
self::getKernel()->log(LogLevel::WARNING, $message, $context); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Normal but significant events |
156
|
|
|
* |
157
|
|
|
* @param string $message |
158
|
|
|
* @param array $context |
159
|
|
|
* @return null |
160
|
|
|
*/ |
161
|
|
|
public static function notice($message, array $context = array()) |
162
|
|
|
{ |
163
|
|
|
self::getKernel()->log(LogLevel::NOTICE, $message, $context); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Interesting events |
168
|
|
|
* |
169
|
|
|
* Example: User logs in, SQL logs. |
170
|
|
|
* |
171
|
|
|
* @param string $message |
172
|
|
|
* @param array $context |
173
|
|
|
* @return null |
174
|
|
|
*/ |
175
|
|
|
public static function info($message, array $context = array()) |
176
|
|
|
{ |
177
|
|
|
self::getKernel()->log(LogLevel::INFO, $message, $context); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Detailed debug information |
182
|
|
|
* |
183
|
|
|
* @param string $message |
184
|
|
|
* @param array $context |
185
|
|
|
* @return null |
186
|
|
|
*/ |
187
|
|
|
public static function debug($message, array $context = array()) |
188
|
|
|
{ |
189
|
|
|
self::getKernel()->log(LogLevel::DEBUG, $message, $context); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Logs with an arbitrary level |
194
|
|
|
* |
195
|
|
|
* @param mixed $level |
196
|
|
|
* @param string $message |
197
|
|
|
* @param array $context |
198
|
|
|
* @return null |
199
|
|
|
*/ |
200
|
|
|
public static function log($level, $message, array $context = array()) |
201
|
|
|
{ |
202
|
|
|
self::getKernel()->log($level, $message, $context); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/******************************************************************************* |
206
|
|
|
* PRIVATE METHODS |
207
|
|
|
*******************************************************************************/ |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return the kernel instance |
211
|
|
|
* |
212
|
|
|
* @return \Apparat\Kernel\Domain\Model\Kernel Kernel instance |
213
|
|
|
*/ |
214
|
|
|
protected static function getKernel() |
215
|
|
|
{ |
216
|
|
|
if (self::$kernel === null) { |
217
|
|
|
self::$kernel = new \Apparat\Kernel\Domain\Model\Kernel(new DiceAdapter(), new Logger()); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return self::$kernel; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|