Completed
Push — master ( 0e916b...709211 )
by Kanto
24s queued 11s
created

Controller::getUserAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     */
104
    protected function handleError($e)
105
    {
106
        Log::error($e->getMessage());
107
    }
108
109
    /**
110
     * Get the http method
111
     *
112
     * @return string|null http method
113
     */
114
    protected function getMethod()
115
    {
116
        return $this->getServer('REQUEST_METHOD');
117
    }
118
119
    /**
120
     * Get the User Agent
121
     *
122
     * @return string|null User Agent
123
     */
124
    protected function getUserAgent()
125
    {
126
        return $this->getServer('HTTP_USER_AGENT');
127
    }
128
129
    /**
130
     * Get the http header
131
     *
132
     * @param string $key header key
133
     * @param mixed $default default value if there is no value specified in the key
134
     * @return string|null http header
135
     */
136
    protected function getHeader($key, $default = null)
137
    {
138
        $headerName = 'HTTP_' . str_replace('-', '_', strtoupper($key));
139
        return $this->getServer($headerName, $default);
140
    }
141
142
    /**
143
     * Get the query parameters
144
     *
145
     * If no key is specified, all query parameters are returned.
146
     * @param string|null $key query parameters key
147
     * @param mixed $default default value if there is no value specified in the key
148
     * @return string|array<string>|null query parameters
149
     */
150
    protected function getQuery($key = null, $default = null)
151
    {
152
        if ($key === null) {
153
            return $_GET;
154
        }
155
        if (!isset($_GET[$key])) {
156
            return $default;
157
        }
158
        return $_GET[$key];
159
    }
160
161
    /**
162
     * Get the post parameters
163
     *
164
     * If no key is specified, all post parameters are returned.
165
     * @param string|null $key post parameters key
166
     * @param mixed $default default value if there is no value specified in the key
167
     * @return string|array<string>|null post parameters
168
     */
169
    protected function getPost($key = null, $default = null)
170
    {
171
        if ($key === null) {
172
            return $_POST;
173
        }
174
        if (!isset($_POST[$key])) {
175
            return $default;
176
        }
177
        return $_POST[$key];
178
    }
179
180
    /**
181
     * Get the body data
182
     *
183
     * @return string body data
184
     */
185
    protected function getBody()
186
    {
187
        $body = file_get_contents('php://input');
188
        $body = $body !== false ? $body : '';
189
        return $body;
190
    }
191
192
    /**
193
     * Get the body data in json format
194
     *
195
     * @return array<mixed> body data
196
     */
197
    protected function getJsonBody()
198
    {
199
        $body = json_decode($this->getBody(), true);
200
        return $body;
201
    }
202
203
    /**
204
     * Get the cookie parameters
205
     *
206
     * If no key is specified, all cookie parameters are returned.
207
     * @param string|null $key cookie parameters key
208
     * @param mixed $default default value if there is no value specified in the key
209
     * @return string|array<string>|null cookie parameters
210
     */
211
    protected function getCookie($key = null, $default = null)
212
    {
213
        if ($key === null) {
214
            return $_COOKIE;
215
        }
216
        if (!isset($_COOKIE[$key])) {
217
            return $default;
218
        }
219
        return $_COOKIE[$key];
220
    }
221
222
    /**
223
     * Get the server parameter
224
     *
225
     * @param string|null $key server parameter key
226
     * @param mixed $default default value if there is no value specified in the key
227
     * @return string|null server parameter
228
     */
229
    protected function getServer($key = null, $default = null)
230
    {
231
        if (!isset($_SERVER[$key])) {
232
            return $default;
233
        }
234
        return $_SERVER[$key];
235
    }
236
 
237
    /**
238
     * Get the path arguments
239
     *
240
     * If no key is specified, all path arguments are returned.
241
     * @param string|null $key path arguments key
242
     * @param mixed $default default value if there is no value specified in the key
243
     * @return string|array<string>|null path arguments
244
     */
245
    protected function getArg($key = null, $default = null)
246
    {
247
        if ($key === null) {
248
            return $this->args;
249
        }
250
        if (!isset($this->args[$key])) {
251
            return $default;
252
        }
253
        return $this->args[$key];
254
    }
255
256
    /**
257
     * Respond in view format
258
     *
259
     * @param string $path view file path
260
     * @param array<mixed> $data response data
261
     * @param int $responseCode http status code, the default is 200
262
     * @return void
263
     */
264
    protected function respondView($path, $data=array(), $responseCode = 200)
265
    {
266
        http_response_code($responseCode);
267
        require(VIEWS_DIR.'/'.$path.'.php');
268
    }
269
270
    /**
271
     * Extract the view
272
     *
273
     * @param string $path view file path
274
     * @param array<mixed> $data response data
275
     * @return string|false view
276
     */
277
    protected function extractView($path, $data=array())
278
    {
279
        ob_start();
280
        $this->respondView($path, $data);
281
        $buffer = ob_get_contents();
282
        ob_end_clean();
283
        return $buffer;
284
    }
285
286
    /**
287
     * Respond in json format
288
     *
289
     * @param array<mixed> $data response data
290
     * @param int $responseCode http status code, the default is 200
291
     * @return void
292
     */
293
    protected function respondJson($data=array(), $responseCode = 200)
294
    {
295
        $json = json_encode($data);
296
        http_response_code($responseCode);
297
        header('Content-Type: application/json');
298
        echo $json;
299
    }
300
301
    /**
302
     * Redirect
303
     *
304
     * @param string $url redirect url
305
     * @param int $responseCode http status code, the default is 302
306
     * @return void
307
     */
308
    protected function redirect($url, $responseCode = 302)
309
    {
310
        header("Location: $url", true, $responseCode);
311
    }
312
}
313