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 pathRouter.php
596 lines (491 loc) · 17.4 KB
/
Router.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/**
* Seed-PHP Microframework.
* @author Rogerio Taques
* @license MIT
* @see http://github.com/rogeriotaques/seed-php
*/
namespace SeedPHP;
use ErrorException;
use SeedPHP\Helper\Http;
class Router
{
/**
* @var string request method
* @see $_allowed_methods
*/
protected $_method = 'GET';
/** @var array app routes */
protected $_routes = [];
/** @var array<string> app allowed methods */
protected $_allowed_methods = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'];
/** @var array<string> app allowed headers */
protected $_allowed_headers = ['Origin', 'Content-Type', 'X-Requested-With'];
/** @var string app allowed origin */
protected $_allowed_origin = '*';
/** @var boolean app page cache */
protected $_cache = true;
/** @var integer app page cache max age */
protected $_cache_max_age = 3600; // default is one hour
/** @var string app page language */
protected $_language = 'en'; // english
/** @var string app page charset */
protected $_charset = 'utf8';
/** @var string requested url */
protected $_uri = '';
/** @var string define the default output type */
protected $_output_type = 'json';
/** @var false|function the callback for when an error is held */
protected $_error_handler = false;
/** @var string name for the response status property */
private $_response_property_status = 'status';
/** @var string name for the response message property */
private $_response_property_message = 'message';
/** @var string name for the response data property */
private $_response_property_data = 'data';
/** @var string name for the response error property */
private $_response_property_error = 'error';
/** @var array<key => value> additional properties to be returned alognside default properties in a response */
private $_additional_response_properties = [];
// ~~~ PUBLIC ~~~
/**
* Set routes for the app.
*
* @param string $route
* @param callable $callback - Optional
* @param callable $before - Optional. A hook to be executed before the callback.
* @param callable $after - Optional. A hook to be executed after the callback.
* @return Router
*/
public function route(string $route = 'GET /', ?callable $callback = null, ?callable $before = null, callable $after = null) : self
{
$method = ['GET'];
if (strpos($route, ' ') !== false) {
list($method, $route) = explode(' ', $route);
$method = explode('|', $method);
}
foreach ($method as $m) {
// is there a route set for given method?
if (!isset($this->_routes[$m])) {
$this->_routes[$m] = [];
}
// NOTE:
// Coz unicorn exists, this url sanitization was placed here!
// Damn, it was breaking the routing system for multiples methods and that's why it is commented now!
// sanitize the regular expression
// $route = str_replace(['/', '.', '@'], ['\/', '\.', '\@'], $route);
// add new route
$this->_routes[$m][] = (object)[
'uri' => $route,
'callback' => $callback,
'before' => $before,
'after' => $after
];
}
// make it chainable
return $this;
} // route
/**
* Complete the flow and send a response to the browser.
*
* @param integer $code
* @param array $response
* @param string $output either json or xml
* @return mixed json|xml strings
*/
public function response(int $code = 200, array $response = [], ?string $output = null)
{
if (is_null($output)) {
$output = $this->_output_type;
}
// identify the http status code
try {
$status = Http::getHTTPStatus($code);
} catch (\Throwable $th) {
// Fallback to "Internal Server Error" with the original message.
$status = Http::getHTTPStatus(Http::_INTERNAL_SERVER_ERROR);
$status['message'] = $th->getMessage();
}
$result = [
"{$this->_response_property_status}" => $status['code'], // aka code
"{$this->_response_property_message}" => $status['message']
];
// if it's an error merge with error data
if ($code >= Http::_BAD_REQUEST) {
$result[ $this->_response_property_error ] = true;
}
// $response should be an array. Whenever it isn't, try to convert it.
// If impossible to convert, just ignores it.
if (is_object($response)) {
$response = (array) $response;
} elseif (is_string($response)) {
$response = [ $response ];
} elseif (!is_array($response)) {
$response = [];
}
// merge response and result
$result = array_merge($result, $this->_additional_response_properties, $response);
// allow enduser to customize the return structure for status
if (isset($_GET['_router_status']) && !empty($_GET['_router_status'])) {
if (isset($result[ $this->_response_property_status ])) {
$result[$_GET['_router_status']] = $result[ $this->_response_property_status ];
unset($result[ $this->_response_property_status ]);
}
}
// allow enduser to customize the return structure for message
if (isset($_GET['_router_message']) && !empty($_GET['_router_message'])) {
if (isset($result[ $this->_response_property_message ])) {
$result[$_GET['_router_message']] = $result[ $this->_response_property_message ];
unset($result[ $this->_response_property_message ]);
}
}
// allow enduser to customize the return structure for data
if (isset($_GET['_router_data']) && !empty($_GET['_router_data'])) {
if (isset($result[ $this->_response_property_data ])) {
$result[$_GET['_router_data']] = $result[ $this->_response_property_data ];
unset($result[ $this->_response_property_data ]);
}
}
// is output required?
if ($output !== false) {
header("{$status['protocol']} {$status['code']} {$status['message']}");
if ($this->_cache === true) {
header('ETag: ' . md5(!is_string($result) ? json_encode($result) : $result)); // this help on caching
}
}
// what kind of output is expected?
switch (strtolower($output)) {
case 'xml':
header("Content-Type: application/xml");
// translate json into xml object
$xml = new \SimpleXMLElement('<response />');
$xml = $this->json2xml($xml, $result);
// finally convert it to string for proper replacements
echo $xml->asXML();
break;
case 'json':
// set proper headers
header("{$status['protocol']} {$status['code']} {$status['message']}");
header("Content-Type: application/json");
// convert result into string
$result = json_encode($result);
echo $result;
break;
default:
// anything else, including "false"
// do nothing. do not output, just return it.
}
// return as object the response
return $result;
} // response
/**
* Set an allowed method.
*
* @param string|array<string> $method
* @param bool $merge when true, resets the list of allowed methods
* @return Router
*/
public function setAllowedMethod($method = '', bool $merge = true) : self
{
if (!empty($method)) {
if (!$merge) {
$this->_allowed_methods = [];
}
if (is_array($method)) {
$this->_allowed_methods = array_merge($this->_allowed_methods, $method);
} else {
$this->_allowed_methods[] = $method;
}
}
return $this;
} // setAllowedMethod
/**
* Set an allowed header.
*
* @param string|array<string> $header
* @param bool $merge
* @return Router
*/
public function setAllowedHeader($header = '', bool $merge = true) : self
{
if (!empty($header)) {
if (!$merge) {
$this->_allowed_headers = [];
}
if (is_array($header)) {
$this->_allowed_headers = array_merge($this->_allowed_headers, $header);
} else {
$this->_allowed_headers[] = $header;
}
}
return $this;
} // setAllowedHeader
/**
* Set an allowed origin. Usually a single origin.
*
* @param string $origin
* @return Router
*/
public function setAllowedOrigin(string $origin = '') : self
{
if (!empty($origin)) {
$this->_allowed_origin = $origin;
}
return $this;
} // setAllowedOrigin
/**
* Define a custom cache setting.
*
* @param boolean $flag
* @param integer $max_age
* @return Router
*/
public function setCache(bool $flag = true, int $max_age = 3600) : self
{
$this->_cache = $flag;
$this->_cache_max_age = $max_age;
return $this;
} // setFlag
/**
* Set a custom language. Defaults to 'en'.
*
* @param string $lang
* @return Router
*/
public function setLanguage(string $lang = 'en') : self
{
if (!empty($lang)) {
$this->_language = $lang;
}
return $this;
} // set Language
/**
* Set a custom charset. Defaults to 'utf8'.
*
* @param string $charset
* @return Router
*/
public function setCharset(string $charset = 'utf8'): self
{
if (!empty($charset)) {
$this->_charset = $charset;
}
return $this;
} // setCharset
/**
* Define what format the response will be given. Defaults to JSON.
*
* @param string $type
* @return Router
*/
public function setOutputType(string $type = 'json'): self
{
if (!empty($type)) {
$this->_output_type = $type;
}
return $this;
} //setOutputType
/**
* Set a custom error handler.
*
* @param mixed $callback Accepts function|false|null
* @return Router
*/
public function onFail(?callable $callback = null): self
{
if (!empty($callback) && is_callable($callback)) {
$this->_error_handler = $callback;
}
return $this;
} // onFail
/**
* Defines a custom property name to return the response http code.
*
* @param string $name
* @return Router
*/
public function setCustomPropertyStatus(string $name = 'status'): self
{
if (!empty($name)) {
$this->_response_property_status = $name;
}
return $this;
} // setCustomPropertyStatus
/**
* Defines a custom property name to return the response message.
*
* @param string $name
* @return Router
*/
public function setCustomPropertyMessage(string $name = 'message'): self
{
if (!empty($name)) {
$this->_response_property_message = $name;
}
return $this;
} // setCustomPropertyMessage
/**
* Defines a custom property name to return the response data.
*
* @param string $name
* @return Router
*/
public function setCustomPropertyData(string $name = 'data'): self
{
if (!empty($name)) {
$this->_response_property_data = $name;
}
return $this;
} // setCustomPropertyData
/**
* Defines a custom property name to return the error status.
*
* @param string $name
* @return Router
*/
public function setCustomPropertyError(string $name = 'error'): self
{
if (!empty($name)) {
$this->_response_property_error = $name;
}
return $this;
} // setCustomPropertyError
/**
* Append global additional properties to be returned alongside any response by default.
*
* @param string $key
* @param mixed $value Accepts string|integer|boolean|function
* @param bool $reset when true, resets the list of additional properties
* @return Router
*/
public function setAdditionalResponseProperty(string $key = '', $value = '', bool $reset = false): self
{
if (!empty($key)) {
if ($reset) {
$this->_additional_response_properties = [];
}
$this->_additional_response_properties[$key] = $value;
}
return $this;
} // setAdditionalResponseProperty
// ~~~ PROTECTED ~~~
/**
* Require all route files from a given path.
*
* @param string $path
* @return void
*/
protected function readRoutesFrom(string $path = ''): void
{
if (empty($path)) {
throw new ErrorException("SeedPHP :: Router : Path must be given.", Http::_BAD_REQUEST);
}
$path = preg_replace('/\/$/', '', $path) . '/*.php';
// Include all files from the given path.
// This removes the need to include them one bye one.
// @since 1.7.0
foreach (glob($path) as $file) {
require_once $file;
}
} // readRoutesFrom
/**
* Dispatches the router actions.
*
* @param array $args
* @return mixed bool|null|string
*/
protected function dispatch(array $args = [])
{
$matches = [];
$matched_callback = null;
$matched_hook_before = null;
$matched_hook_after = null;
// echo "METHOD ", $this->_method, "<br />\n";
// is there a matching route?
if (isset($this->_routes[$this->_method])) {
// echo "Found routes for ", $this->_method, "\n";
foreach ($this->_routes[$this->_method] as $route) {
// echo $route->uri, ' ::: ', $this->_uri, "<br />\n";
if (@preg_match("@^{$route->uri}$@", $this->_uri, $matches)) {
// echo " -> MATCHED";
$matched_callback = $route->callback;
$matched_hook_before = $route->before;
$matched_hook_after = $route->after;
break;
}
// echo "<br />\n";
}
}
// echo '<pre>', var_dump($matches), '</pre><br >', "\n";
// die;
if (count($matches) === 0) {
if ($this->_error_handler !== false) {
return call_user_func($this->_error_handler, (object) Http::getHTTPStatus(Http::_NOT_IMPLEMENTED));
}
return $this->response(Http::_NOT_IMPLEMENTED);
}
if (!empty($matched_callback) && is_callable($matched_callback)) {
// Run the before hook
!empty($matched_hook_before) && is_callable($matched_hook_before) && @call_user_func($matched_hook_before, $args);
// Run the call
$called = call_user_func($matched_callback, $args);
// Run the after hook
!empty($matched_hook_after) && is_callable($matched_hook_after) && @call_user_func($matched_hook_after, $args + [ $called ]);
if (is_null($called)) {
return true;
}
return $called;
}
return true;
} // dispatch
// ~~~ PRIVATE ~~~
/**
* Converts a JSON into XML.
*
* @param object &$xml
* @param array $data
* @return xml
*/
private function json2xml(&$xml, $data)
{
// exit when data is empty.
if (is_null($data)) {
return $xml;
}
// runs thru data to build xml
foreach ($data as $dk => $dv) {
// node data can be an array/ object
if (is_array($dv)) {
// is the key a string?
if (!is_numeric($dk)) {
// eventually it's possible that nodes have properties
// whenever it has, isolate properties for post use.
if (strpos($dk, ' ') !== false) {
$props = explode(' ', $dk);
$dk = array_shift($props);
}
// create a new node
$node = $xml->addChild($dk);
// new node should have attributes? appends it ...
if (isset($props) && count($props) > 0) {
foreach ($props as $prop) {
$prop = explode('=', $prop);
$prop[1] = strpos($prop[1], '"') === 0 ? substr($prop[1], 1, strlen($prop[1]) - 2) : $prop[1];
$node->addAttribute($prop[0], $prop[1]);
}
}
// recursive call for subnodes
// giving the most recent node created
$this->json2xml($node, $dv);
} else {
// recursive call for subnodes
$this->json2xml($xml, $dv);
}
} else {
$xml->addChild($dk, htmlspecialchars($dv));
}
}
// return xml object
return $xml;
} // json2xml
} // class