-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.class.php
165 lines (148 loc) · 3.91 KB
/
Router.class.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
<?php
namespace App\Core\Routing;
use App\Core\Exceptions\Routing\RouterException;
use App\Core\Exceptions\Routing\RouterNotFoundException;
use Closure;
class Router{
private $url;
private $routes = [];
private $namedRoutes = [];
private $path;
private $groupPattern;
private static $_instance = null;
private $current_route;
public function __construct()
{
$this->url = $_GET['url'] ?? "";
}
/**
* Singleton
*/
public static function getInstance(): Router
{
if (is_null(self::$_instance)) {
self::$_instance = new Router();
}
return self::$_instance;
}
/**
* handle GET requests
*
* @param string $path
* @param $callable
* @param string|null $name
* @return Route
*/
public function get(string $path, $callable, ?string $name=null):Route
{
return $this->add($path, $callable, $name, 'GET');
}
/**
* handle PUT requests
*
* @param string $path
* @param $callable
* @param string|null $name
* @return Route
*/
public function put(string $path, $callable, ?string $name=null):Route
{
return $this->add($path, $callable, $name, 'PUT');
}
/**
* handle DELETE requests
*
* @param string $path
* @param $callable
* @param string|null $name
* @return Route
*/
public function delete(string $path, $callable, ?string $name=null):Route
{
return $this->add($path, $callable, $name, 'DELETE');
}
/**
* handle POST requests
*
* @param string $path
* @param $callable
* @param string|null $name
* @return Route
*/
public function post(string $path, $callable, string $name=null):Route
{
return $this->add($path, $callable, $name, 'POST');
}
/**
* handle requests by dispatching them and,
* also named them;
*
* @param string $path
* @param $callable
* @param string|null $name
* @param string|null $method
* @return Route
*/
private function add(string $path, $callable, ?string $name , ?string $method): Route
{
if (!empty($this->groupPattern)) {
$path = trim($this->groupPattern) . ltrim($path);
}
$route = new Route($path, $callable);
$this->routes[$method][] = $route;
if (is_string($callable) && ($name === null)) {
$name = $callable;
}
if ($name) {
$this->namedRoutes[$name] = $route;
}
return $route;
}
/**
* Create a group to help to do sub routes with same base path
*
* @param $groupPattern
* @param callable $routes
* @return void
*/
public function group($groupPattern, callable $routes):void
{
$this->groupPattern = $groupPattern;
$routes($this);
unset($this->groupPattern);
}
/**
* Get url of a specific route name and add param to them
*
* @param string $name
* @param array $params
* @throws RouterException
* @return string
*/
public function url(string $name, array $params = []):string
{
if (!isset($this->namedRoutes[$name])) {
throw new RouterException("No route matches this name");
}
return $this->namedRoutes[$name]->getUrl($params);
}
/**
* Start the router
*
* @throws RouterException|RouterNotFoundException
*/
public function run()
{
$method= $_SERVER['REQUEST_METHOD'];
if(!isset($this->routes[$method])){
throw new RouterException('This method isn\'t allowed');
}
foreach ($this->routes[$method] as $route){
if( $route->match($this->url)){
$this->current_route = $route;
return $route->call();
}
}
throw new RouterNotFoundException();
}
}