This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCore.php
441 lines (367 loc) · 12.1 KB
/
Core.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* Seed-PHP Microframework.
* @author Rogerio Taques
* @license MIT
* @see http://github.com/rogeriotaques/seed-php
*/
namespace SeedPHP;
use SeedPHP\Router;
use SeedPHP\Helper\Http;
class Core extends Router
{
/** @var object an object containing the request data */
private $_request;
/** @var object */
private static $instance;
// ~~~ MAGIC METHODS ~~~
/**
* The constructor.
*/
public function __construct()
{
// Nothing to construct
}
// ~~~ PUBLIC METHODS ~~~
/**
* Returns the singleton instance.
*
* @return Core
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new Core();
}
return self::$instance;
} // getInstance
/**
* Starts the working flow.
*
* @return bool
*/
public function run()
{
$this->setPageHeaders();
$this->buildRequest();
// is it an OPTIONS request?
// it's used to confirm if app accept CORS calls
if ($this->_method === 'OPTIONS') {
return $this->response(Http::_OK); // do accept it
}
try {
return $this->dispatch($this->_request->args);
} catch (\Throwable $th1) {
$_status = null;
// Get the most appropriated HTTP status code.
try {
$_status = Http::getHTTPStatus($th1->getCode());
} catch (\Throwable $th2) {
$_status = Http::getHTTPStatus(500);
}
// Add the real error message to the error object.
$_status['_message'] = $th1->getMessage();
// Always an error handler was set, use it.
if ($this->_error_handler !== false) {
return call_user_func($this->_error_handler, (object) $_status);
}
return $this->response(
$_status['code'],
[ "message" => $th1->getMessage() ]
);
}
} // run
/**
* Retrieve the request headers.
* When $key is given, returns a string or false when the key is not found.
*
* @param [string] $key
* @return array<string>|string|false
*/
public function header($key = null)
{
$headers = getallheaders();
if ($key === null) {
return $headers;
}
// Tries the header case-sensitive
if (isset($headers[$key]) !== false) {
return $headers[$key];
}
// Tries the header case-insensitive
// @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
if (isset($headers[strtolower($key)]) !== false) {
return $headers[strtolower($key)];
}
// Otherwise, error
return false;
} // header
/**
* Retrieve posted data.
* When $key is given, returns a string or false when the key is not found.
*
* @param [string] $key
* @return array<string>|string|false
*/
public function post($key = null)
{
// The 'always_populate_raw_post_data' is deprecated since [email protected].
// So, check if auto_populate_post_data is enabled, if not enabled, use php://input instead.
$post_data = [];
if ((!isset($_POST) || count($_POST) === 0) &&
intval(ini_get('always_populate_raw_post_data')) < 1) {
$input = file_get_contents("php://input");
if (is_null($post_data = json_decode($input, true))) {
// Sometimes input cannot be decoded from json, then try to parse it from string.
parse_str(file_get_contents("php://input"), $post_data);
}
} else {
$post_data = $_POST;
}
if ($key === null) {
return $post_data;
}
return (isset($post_data[$key]) ? $post_data[$key] : false);
} // post
/**
* Retrieve data passed as query-string.
* When $key is given, returns a string or false when the key is not found.
*
* @param [string] $key
* @return array<string>|string|false
*/
public function get($key = null)
{
if ($key === null) {
return $_GET;
}
return (isset($_GET[$key]) ? $_GET[$key] : false);
} // get
/**
* Retrieve submited files.
* When $key is given, returns a string or false when the key is not found.
*
* @param [string] $key
* @return array<file>|file|false
*/
public function file($key = null)
{
if ($key === null) {
return $_FILES;
}
return (isset($_FILES[$key]) ? $_FILES[$key] : false);
} // file
/**
* Alias for file().
* @param string [$key]
* @return string|array
*/
public function files($key = null)
{
return $this->file($key);
} // files
public function cookie($key = null)
{
if ($key === null) {
return $_COOKIE;
}
return (isset($_COOKIE[$key]) ? $_COOKIE[$key] : false);
} // cookie
/**
* Retrieve data passed in the put method.
* When $key is given, returns a string or false when the key is not found.
*
* @param [string] $key
* @return array<string>|string|false
*/
public function put($key = null)
{
$php_input = file_get_contents("php://input");
// Try parsing it from JSON
$_PUT = json_decode($php_input, true);
if (!$_PUT) {
// Fallback to parsing it from URL encoded string
parse_str($php_input, $_PUT);
}
if ($key === null) {
return $_PUT;
}
return (isset($_PUT[$key]) ? $_PUT[$key] : false);
} // put
/**
* Return the request object.
*
* @return object
*/
public function request()
{
return $this->_request;
}
/**
* Load helpers and make them available through the App instance.
*
* @param string $component
* @param array $config
* @param string $alias
* @return Core
*/
public function load($component = '', $config = [], $alias = ''): Core
{
if (empty($component)) {
return false;
}
if (strtolower($component) === 'router') {
parent::readRoutesFrom($config[ 'path' ] ?? '');
return $this;
}
$class = "\\SeedPHP\\Helper\\" . $this->camelfy($component);
$alias = (!empty($alias) && is_string($alias) ? $alias : $component);
$this->$alias = new $class($config);
if ($component !== $alias) {
$this->$component = $alias; // register the name chosen as alias in the original helper
}
return $this;
} // load
// ~~~ PRIVATE METHODS ~~~
/**
* Return an URL safe string.
*
* @param [type] $str
* @param boolean $first_lower
* @return string
*/
private function camelfy($str, $first_lower = false)
{
if (empty($str)) {
return '';
}
$worldCounter = 0;
return implode(
'',
array_map(
function ($el) use (&$worldCounter, $first_lower) {
return $first_lower === true && $worldCounter++ === 0
? $el
: ucfirst($el);
},
explode('-', $str)
)
);
} // camelfy
/**
* Write the page headers (used before returning a response).
*
* @return void
*/
private function setPageHeaders()
{
header("Access-Control-Allow-Origin: {$this->_allowed_origin}");
header("Content-language: {$this->_language}");
if (count($this->_allowed_methods) > 0) {
header("Access-Control-Allow-Methods: " . implode(', ', $this->_allowed_methods));
}
if (count($this->_allowed_headers) > 0) {
header("Access-Control-Allow-Headers: " . implode(', ', $this->_allowed_headers));
}
// is cache allowed
if ($this->_cache === true) {
header("Cache-Control: max-age={$this->_cache_max_age}");
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $this->_cache_max_age) . ' GMT');
}
// when cache is not allowed, adds necessary headers to force browser ignore caching
else {
header("Cache-Control: max-age=0, no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: Mon, 1 Jan 1900 05:00:00 GMT");
}
} // setPageHeaders
/**
* Build the request object which will be returned with $this->request().
*
* @return void
*/
private function buildRequest()
{
// Define base path
$patt = str_replace(array('\\', ' '), array('/', '%20'), dirname($_SERVER['SCRIPT_NAME']));
// Retrieve requested uri
$this->_uri = isset($_SERVER['REQUEST_URI'])
? $_SERVER['REQUEST_URI']
: '/';
// Remove query-string (if any)
if (strpos($this->_uri, '?') !== false) {
$this->_uri = substr($this->_uri, 0, strpos($this->_uri, '?'));
}
// Remove base path from uri
$this->_uri = preg_replace('@^' . $patt . '@', '', $this->_uri);
// Identify the request method
$this->_method = (isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET');
// Remove trailing slashes to easily match paths.
// It must be done before adding a "root" slash, otherwise it breaks the router for (hidden) index pages.
$this->_uri = preg_replace('/\/$/', '', $this->_uri);
// Add a initial (root) slash case it's not present
if (!preg_match('/^\//', $this->_uri)) {
$this->_uri = "/{$this->_uri}";
}
// Explode arguments
$args = explode('/', $this->_uri);
// Filter empty arguments
// If there's empty argument, it means the uri has finished in a slash
$args = array_filter($args, function ($el) {
return !empty($el) && !is_null($el);
});
// Has args only one element and is it empty?
// Means it's the start point (root)
if (count($args) === 1 && empty($args[0])) {
$args[0] = '/';
}
// Extracts uri parts
$endpoint = array_shift($args);
$verb = array_shift($args);
$id = count($args) ? array_shift($args) : null;
// Is verb an ID?
if (is_numeric($verb)) {
$_verb = $id;
$id = $verb;
$verb = $_verb;
}
// Do arguments can be set in pairs?
if (count($args) % 2 === 0) {
// Fetches keys from args
$args_keys = array_filter($args, function ($k) use (&$args) {
$k = key($args);
next($args);
return !($k & 1);
});
// Fetches values from args
$args_values = array_filter($args, function ($k) use (&$args) {
$k = key($args);
next($args);
return $k & 1;
});
// Avoid matching in pairs when ID is fetched in the args.
// This prevents unexpected behaviors and values in the args array
// and also keeps the backward compatibility with previous implementations.
foreach ($args_keys as $k => $v) {
if (is_numeric($v)) {
unset($args_keys[$k], $args_values[$k]);
}
}
$args_combined = [];
if (count($args_keys) === count($args_values)) {
$args_combined = @array_combine($args_keys, $args_values);
}
// Makes it an assossiative array
$args = array_merge($args, $args_combined);
}
// Finally returns
$this->_request = (object)[
'base' => Http::getBaseUrl(),
'method' => $this->_method,
'endpoint' => $endpoint,
'verb' => $verb,
'id' => $id,
'args' => $args
];
} // buildRequest
} // class