-
Notifications
You must be signed in to change notification settings - Fork 22
/
router.php
116 lines (100 loc) · 3.72 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
<?php
require_once 'logger.php';
class Router
{
public static function exec()
{
$url = $_SERVER['REQUEST_URI'];
$path = preg_replace('/\/(?=\/)/', '', parse_url($url, PHP_URL_PATH));
$path_parts = explode('/', $path);
if (strlen($path_parts[1]) === 0) {
self::view_top_page_contents();
exit(1);
}
// $frontend_files = self::get_frontend_filenames();
// foreach($frontend_files as $frontend_file) {
// if($path === $frontend_file) {
// echo file_get_contents(getcwd() . '/frontend/dist' . $frontend_file);
// exit(0);
// }
// }
if (count($path_parts) < 3) {
self::view_top_page_contents();
exit(1);
}
/*
$url => 'http://api.ektotal.com/api/submit/status?id=100'
$api_endpoint => 'api'
$function => 'submit'
$arguments => 'status'
$queries => ['id' => '100']
*/
$method = strtolower($_SERVER['REQUEST_METHOD']);
$api_endpoint = strtolower($path_parts[1]);
if ($api_endpoint !== 'api') {
throw new Exception('Unsupported API endpoint: ' . $api_endpoint);
}
$function = strtolower($path_parts[2]);
$arguments = implode('/', array_slice($path_parts, 3));
$queries = self::parse_queries($url);
if (!in_array($function, self::get_function_names())) {
throw new Exception('Unsupported function: ' . $function);
}
require_once getcwd() . '/api/' . $function . '.php';
if (!method_exists($function, $method)) {
throw new Exception('Unsupported method: ' . $function . '::' . $method);
}
$function::$method($arguments, $queries);
}
public static function view_top_page_contents()
{
$html = file_get_contents(getcwd() . '/frontend/dist/index.html');
echo $html;
}
public static function parse_queries(string $url)
{
$queries = [];
$queries_str = parse_url($url, PHP_URL_QUERY);
if (strlen($queries_str) > 1) {
$queries_parts = explode('&', $queries_str);
foreach ($queries_parts as $queries_part) {
$queries_arr = explode('=', $queries_part);
if (count($queries_arr) > 1) {
$queries[strtolower($queries_arr[0])] = strtolower(implode('=', array_slice($queries_arr, 1)));
} else {
$queries[strtolower($queries_arr[0])] = null;
}
}
}
return $queries;
}
public static function get_function_names()
{
foreach (glob(getcwd() . '/api/*.php') as $file) {
if (is_file($file)) {
$supported_functions[] = pathinfo($file)['filename'];
}
}
return $supported_functions;
}
// private static function get_frontend_filenames(): array
// {
// $dir = getcwd() . '/frontend/dist';
// $iterator = new RecursiveIteratorIterator
// (
// new RecursiveDirectoryIterator
// (
// $dir,
// FilesystemIterator::SKIP_DOTS |
// FilesystemIterator::KEY_AS_PATHNAME |
// FilesystemIterator::CURRENT_AS_FILEINFO
// ),
// RecursiveIteratorIterator::LEAVES_ONLY
// );
// $list = [];
// foreach ($iterator as $pathname => $info) {
// $list[] = str_replace($dir, '', $pathname);
// }
// return $list;
// }
}