-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
60 lines (51 loc) · 1.67 KB
/
index.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
<?php
session_start();
require_once 'lib/constants.php';
require_once 'lib/utils.php';
/*
* ROUTER
*/
$base_url = explode('?' , $_SERVER['REQUEST_URI'])[0];
$base_url = rtrim($base_url, "/");
$routes = json_decode(file_get_contents('routes.json', 'r'), true);
$routes = sortRoutesByURLLength($routes);
foreach ($routes as $route) {
$url_match = str_contains($base_url, $route['url']);
$method_match = $route['method'] === $_SERVER['REQUEST_METHOD'];
if ( $url_match && $method_match ) {
$given_parameters = splitGivenParameters($base_url, $route);
$params = extract_parameters($given_parameters, $route['expected_parameters']);
include $route['file'];
break;
}
}
function splitGivenParameters($base_url, $route) {
if ($route['method'] === 'GET') {
$trimmed_parameters = str_replace($route['url'] , '', $base_url);
$trimmed_parameters = ltrim($trimmed_parameters, '/');
if ($trimmed_parameters !== "") {
return explode( '/', $trimmed_parameters);
}
}
return [];
}
function sortRoutesByURLLength($routes) {
function longestRoute($a, $b) {
if(strlen($a['url']) == strlen($b['url'])) {
return 0 ;
}
return (strlen($a['url']) > strlen($b['url'])) ? -1 : 1;
}
usort($routes, 'longestRoute');
return $routes;
}
function extract_parameters($parameters, $expected_parameters) {
$params = [];
if (count($parameters) != count ($expected_parameters)) {
throw new Error('Error : number of parameters is wrong');
}
foreach ($expected_parameters as $pos => $key) {
$params[$key] = $parameters[$pos];
}
return $params;
}