1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Kore : Simple And Minimal Framework |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Kore; |
8
|
|
|
|
9
|
|
|
use Kore\Log; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Controller class |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
abstract class Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* controller namespace |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $controller; |
23
|
|
|
/** |
24
|
|
|
* path arguments |
25
|
|
|
* |
26
|
|
|
* @var array<string> |
27
|
|
|
*/ |
28
|
|
|
protected $args = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Action |
32
|
|
|
* |
33
|
|
|
* The action is implemented in subclasses. |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
abstract protected function action(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Main Processing |
40
|
|
|
* |
41
|
|
|
* @param string $controller controller namespace |
42
|
|
|
* @param array<string> $args path arguments |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function main($controller, $args) |
46
|
|
|
{ |
47
|
|
|
$this->controller = $controller; |
48
|
|
|
$this->args = $args; |
49
|
|
|
Log::init($this->moduleName(), $this->logLevel()); |
50
|
|
|
|
51
|
|
|
Log::debug(sprintf('[START]%s', $this->controller)); |
52
|
|
|
try { |
53
|
|
|
$this->preaction(); |
54
|
|
|
$this->action(); |
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
$this->handleError($e); |
57
|
|
|
} |
58
|
|
|
Log::debug(sprintf('[END]%s', $this->controller)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the module name |
63
|
|
|
* |
64
|
|
|
* The default is 'app'. |
65
|
|
|
* If you need to customize, please override it with subclasses. |
66
|
|
|
* @return string module name |
67
|
|
|
*/ |
68
|
|
|
protected function moduleName() |
69
|
|
|
{ |
70
|
|
|
return 'app'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the log level |
75
|
|
|
* |
76
|
|
|
* The default is Log::LEVEL_DEBUG. |
77
|
|
|
* If you need to customize, please override it with subclasses. |
78
|
|
|
* @return int log level |
79
|
|
|
* @see \Kore\Log |
80
|
|
|
*/ |
81
|
|
|
protected function logLevel() |
82
|
|
|
{ |
83
|
|
|
return Log::LEVEL_DEBUG; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Preprocessing of the action |
88
|
|
|
* |
89
|
|
|
* If you need to customize, please override it with subclasses. |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
protected function preaction() |
93
|
|
|
{ |
94
|
|
|
// Override if necessary |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Handling Errors |
99
|
|
|
* |
100
|
|
|
* If you need to customize the handling of errors, please override it with subclasses. |
101
|
|
|
* @param \Exception $e errors |
102
|
|
|
* @return void |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
protected function handleError($e) |
106
|
|
|
{ |
107
|
|
|
Log::error($e->getMessage()); |
108
|
|
|
throw $e; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the http method |
113
|
|
|
* |
114
|
|
|
* @return mixed http method |
115
|
|
|
*/ |
116
|
|
|
protected function getMethod() |
117
|
|
|
{ |
118
|
|
|
return $this->getServer('REQUEST_METHOD'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the User Agent |
123
|
|
|
* |
124
|
|
|
* @return mixed User Agent |
125
|
|
|
*/ |
126
|
|
|
protected function getUserAgent() |
127
|
|
|
{ |
128
|
|
|
return $this->getServer('HTTP_USER_AGENT'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the http header |
133
|
|
|
* |
134
|
|
|
* @param string $key header key |
135
|
|
|
* @param mixed $default default value if there is no value specified in the key |
136
|
|
|
* @return mixed http header |
137
|
|
|
*/ |
138
|
|
|
protected function getHeader($key, $default = null) |
139
|
|
|
{ |
140
|
|
|
$headerName = 'HTTP_' . str_replace('-', '_', strtoupper($key)); |
141
|
|
|
return $this->getServer($headerName, $default); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the query parameters |
146
|
|
|
* |
147
|
|
|
* If no key is specified, all query parameters are returned. |
148
|
|
|
* @param string|null $key query parameter key |
149
|
|
|
* @param mixed $default default value if there is no value specified in the key |
150
|
|
|
* @return mixed query parameters |
151
|
|
|
*/ |
152
|
|
|
protected function getQuery($key = null, $default = null) |
153
|
|
|
{ |
154
|
|
|
return $this->getFromArray($_GET, $key, $default); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the post parameters |
159
|
|
|
* |
160
|
|
|
* If no key is specified, all post parameters are returned. |
161
|
|
|
* @param string|null $key post parameter key |
162
|
|
|
* @param mixed $default default value if there is no value specified in the key |
163
|
|
|
* @return mixed post parameters |
164
|
|
|
*/ |
165
|
|
|
protected function getPost($key = null, $default = null) |
166
|
|
|
{ |
167
|
|
|
return $this->getFromArray($_POST, $key, $default); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the body data |
172
|
|
|
* |
173
|
|
|
* @return string body data |
174
|
|
|
*/ |
175
|
|
|
protected function getBody() |
176
|
|
|
{ |
177
|
|
|
$body = file_get_contents('php://input'); |
178
|
|
|
$body = $body !== false ? $body : ''; |
179
|
|
|
return $body; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the body data in json format |
184
|
|
|
* |
185
|
|
|
* @return array<mixed> body data |
186
|
|
|
*/ |
187
|
|
|
protected function getJsonBody() |
188
|
|
|
{ |
189
|
|
|
$body = json_decode($this->getBody(), true); |
190
|
|
|
return $body; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the cookie parameters |
195
|
|
|
* |
196
|
|
|
* If no key is specified, all cookie parameters are returned. |
197
|
|
|
* @param string|null $key cookie parameter key |
198
|
|
|
* @param mixed $default default value if there is no value specified in the key |
199
|
|
|
* @return mixed cookie parameters |
200
|
|
|
*/ |
201
|
|
|
protected function getCookie($key = null, $default = null) |
202
|
|
|
{ |
203
|
|
|
return $this->getFromArray($_COOKIE, $key, $default); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the server parameters |
208
|
|
|
* |
209
|
|
|
* If no key is specified, all server parameters are returned. |
210
|
|
|
* @param string|null $key server parameter key |
211
|
|
|
* @param mixed $default default value if there is no value specified in the key |
212
|
|
|
* @return mixed server parameters |
213
|
|
|
*/ |
214
|
|
|
protected function getServer($key = null, $default = null) |
215
|
|
|
{ |
216
|
|
|
return $this->getFromArray($_SERVER, $key, $default); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the path arguments |
221
|
|
|
* |
222
|
|
|
* If no key is specified, all path arguments are returned. |
223
|
|
|
* @param string|null $key path argument key |
224
|
|
|
* @param mixed $default default value if there is no value specified in the key |
225
|
|
|
* @return mixed path arguments |
226
|
|
|
*/ |
227
|
|
|
protected function getArg($key = null, $default = null) |
228
|
|
|
{ |
229
|
|
|
return $this->getFromArray($this->args, $key, $default); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get from array |
234
|
|
|
* |
235
|
|
|
* If no key is specified, array is returned. |
236
|
|
|
* @param array<mixed> $array array |
237
|
|
|
* @param string|null $key path key |
238
|
|
|
* @param mixed $default default value if there is no value specified in the key |
239
|
|
|
* @return mixed value |
240
|
|
|
*/ |
241
|
|
|
protected function getFromArray($array, $key, $default) |
242
|
|
|
{ |
243
|
|
|
if ($key === null) { |
244
|
|
|
return $array; |
245
|
|
|
} |
246
|
|
|
if (!isset($array[$key])) { |
247
|
|
|
return $default; |
248
|
|
|
} |
249
|
|
|
return $array[$key]; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Respond in view format |
254
|
|
|
* |
255
|
|
|
* @param string $path view file path |
256
|
|
|
* @param array<mixed> $data response data |
257
|
|
|
* @param int $responseCode http status code, the default is 200 |
258
|
|
|
* @return void |
259
|
|
|
*/ |
260
|
|
|
protected function respondView($path, $data=array(), $responseCode = 200) |
261
|
|
|
{ |
262
|
|
|
http_response_code($responseCode); |
263
|
|
|
require(VIEWS_DIR.'/'.$path.'.php'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Extract the view |
268
|
|
|
* |
269
|
|
|
* @param string $path view file path |
270
|
|
|
* @param array<mixed> $data response data |
271
|
|
|
* @return string|false view |
272
|
|
|
*/ |
273
|
|
|
protected function extractView($path, $data=array()) |
274
|
|
|
{ |
275
|
|
|
ob_start(); |
276
|
|
|
$this->respondView($path, $data); |
277
|
|
|
$buffer = ob_get_contents(); |
278
|
|
|
ob_end_clean(); |
279
|
|
|
return $buffer; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Respond in json format |
284
|
|
|
* |
285
|
|
|
* @param array<mixed> $data response data |
286
|
|
|
* @param int $responseCode http status code, the default is 200 |
287
|
|
|
* @return void |
288
|
|
|
*/ |
289
|
|
|
protected function respondJson($data=array(), $responseCode = 200) |
290
|
|
|
{ |
291
|
|
|
$json = json_encode($data); |
292
|
|
|
http_response_code($responseCode); |
293
|
|
|
header('Content-Type: application/json'); |
294
|
|
|
echo $json; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Redirect |
299
|
|
|
* |
300
|
|
|
* @param string $url redirect url |
301
|
|
|
* @param int $responseCode http status code, the default is 302 |
302
|
|
|
* @return void |
303
|
|
|
*/ |
304
|
|
|
protected function redirect($url, $responseCode = 302) |
305
|
|
|
{ |
306
|
|
|
header("Location: $url", true, $responseCode); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|