|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EdsonOnildo\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Router |
|
9
|
|
|
* @package Router |
|
10
|
|
|
*/ |
|
11
|
|
|
class Router |
|
12
|
|
|
{ |
|
13
|
|
|
private $request; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $routes = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Router constructor. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->request = Request::createFromGlobals(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $route |
|
30
|
|
|
*/ |
|
31
|
|
|
public function add(array $route): void |
|
32
|
|
|
{ |
|
33
|
|
|
$request = Request::create( |
|
34
|
|
|
$route['uri'], |
|
35
|
|
|
strtoupper($route['method']) |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$request->attributes->set('callback', $route['callback']); |
|
39
|
|
|
|
|
40
|
|
|
self::$routes[] = $request; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return Request|null |
|
45
|
|
|
*/ |
|
46
|
|
|
public function handle(): ?Request |
|
47
|
|
|
{ |
|
48
|
|
|
foreach (self::$routes as $route) { |
|
49
|
|
|
if ($this->checkVerb($route) && $this->checkPath($route)) { |
|
50
|
|
|
return $route; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Request $route |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
private function checkVerb(Request $route): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->request->isMethod('get') == $route->isMethod('get'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Request $route |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
private function checkPath(Request $route): bool |
|
70
|
|
|
{ |
|
71
|
|
|
// if (count($route->getUri()) != count($this->server->getUri())) { |
|
72
|
|
|
// return false; |
|
73
|
|
|
// } |
|
74
|
|
|
return $this->parsePath($route) |
|
75
|
|
|
&& $this->request->getPathInfo() == $route->getPathInfo(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Route $route |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
private function parsePath(Request $route): bool |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
// for ($i = 0; $i < count($routeUri); $i++) { |
|
85
|
|
|
// $serverUri[$i] = preg_replace('/\@.*/', $this->server->getUri()[$i], $routeUri[$i]); |
|
86
|
|
|
// if ($serverUri[$i] != $routeUri[$i]) { |
|
87
|
|
|
// // Regular expression named parameter matching |
|
88
|
|
|
// if (preg_match('/\@([\w]+):(.*)/', $routeUri[$i], $match)) { |
|
89
|
|
|
// if (preg_match('/'.$match[2].'/', $serverUri[$i], $match[2])) { |
|
90
|
|
|
// if ($serverUri[$i] != $match[2][0]) { |
|
91
|
|
|
// return false; |
|
92
|
|
|
// } |
|
93
|
|
|
// $route->setArg($match[1], $match[2][0]); |
|
94
|
|
|
// } else { |
|
95
|
|
|
// return false; |
|
96
|
|
|
// } |
|
97
|
|
|
// } else { |
|
98
|
|
|
// $routeUri[$i] = str_replace('@', '', $routeUri[$i]); |
|
99
|
|
|
// $route->setArg(str_replace(':', '', $routeUri[$i]), $serverUri[$i]); |
|
100
|
|
|
// } |
|
101
|
|
|
// } |
|
102
|
|
|
// } |
|
103
|
|
|
|
|
104
|
|
|
// $serverUri = implode('/', $serverUri); |
|
105
|
|
|
// $route->setUri($serverUri); |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.