/
CGI.php
199 lines (178 loc) · 5.18 KB
/
CGI.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
<?php
/**
* CGI Controller.
*
* @author Jia Huang
*
* @version $Id$
*
* @copyright Genee, 2014-02-08
**/
/**
* Define DocBlock.
**/
namespace Gini\Controller;
use Gini\CGI\Response;
abstract class CGI
{
/**
* Current action.
*
* @var string
*/
public $action;
/**
* Parameters passed to current action.
*
* @var array
*/
public $params;
/**
* current app module info.
*
* @var array
*/
public $app;
/**
* Environmental variables such as $_GET/$_POST/$_FILES/route passed to current controller.
*
* @var array
*/
public $env;
/**
* contains all middlware instances for current controller
*
* @var array
*/
public $middlewares = [];
/**
* Function called right before action is being executed.
*
* @param string $action
* @param string $params
*/
protected function __preAction($action, &$params)
{
}
/**
* Function called right after action was executed.
*
* @param string $action
* @param string $params
* @param string $response
*/
protected function __postAction($action, &$params, $response)
{
}
protected function normalizeActionParams()
{
$params = (array) $this->params;
$methodName = $this->action;
if ($methodName) {
$actionName = preg_replace('/^action/i', '', $methodName);
} else {
$actionName = strtr($params[0], ['-' => '', '_' => '']);
if (
$actionName && $actionName[0] != '_'
&& method_exists($this, 'action' . $actionName)
) {
array_shift($params);
} elseif (method_exists($this, '__index')) {
$actionName = null;
} else {
throw new Response\Exception(null, 404);
}
$methodName = $actionName ? 'action' . $actionName : '__index';
}
return [$actionName, $methodName, $params];
}
public function errorResponse($e)
{
return new Response\HTML(V('error/http', [
'code' => $e->getCode(),
'message' => $e->getMessage()
]), $e->getCode());
}
/**
* Execute current action with parameters.
*/
public function execute()
{
try {
list($actionName, $methodName, $params) = $this->normalizeActionParams();
// 1. pass through all middlewares
if (is_array($this->middlewares)) {
foreach ($this->middlewares as $middleware) {
if (is_string($middleware)) {
$middleware = \Gini\CGI\Middleware::of($middleware);
}
$middleware->process($this, $actionName, $params);
}
}
// 2. preAction/action/postAction
$response = $this->__preAction($actionName, $params);
if ($response !== false) {
$response = \Gini\CGI::executeAction([$this, $methodName], $params, $this->form());
}
$response = $this->__postAction($actionName, $params, $response) ?: $response;
} catch (Response\Exception $e) {
$response = $this->errorResponse($e);
}
return $response ?: new Response\Nothing();
}
/**
* Return POST/GET/FILES or JSON content in array.
*
* @param string $mode 'get': $_GET, 'post': $_POST, 'files': $_FILES, default: $_POST + $_GET
*
* @return array Return corresponding form data
*/
public function form($mode = '*', $data = null, $overwrite = false)
{
if ($data === null) {
switch ($mode) {
case 'get':
return $this->env['get'] ?? [];
case 'post':
return $this->env['post'] ?? [];
case 'files':
return $this->env['files'] ?? [];
default:
return array_merge($this->env['get'] ?? [], $this->env['post'] ?? []);
}
} else {
switch ($mode) {
case 'get':
$this->env['get'] = $this->env['get'] ?? [];
if ($overwrite) {
$this->env['get'] = $data;
} else {
array_merge($this->env['get'], $data);
}
case 'post':
$this->env['post'] = $this->env['post'] ?? [];
if ($overwrite) {
$this->env['post'] = $data;
} else {
array_merge($this->env['post'], $data);
}
}
}
}
/**
* Redirect to other path.
*
* @param string $url URL to redirect
* @param array|string|null $query Query parameters need to include in URL
*/
public function redirect()
{
$args = func_get_args();
call_user_func_array('\Gini\CGI::redirect', $args);
}
public function header()
{
$args = func_get_args();
call_user_func_array('\Gini\CGI::header', $args);
}
}