1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Preetender\Routing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Preetender\Routing\Contracts\RouterVerbs; |
7
|
|
|
use Preetender\Routing\Exceptions\MethodNotAllowedException; |
8
|
|
|
use Preetender\Routing\Exceptions\UnregisteredRouteException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Route |
12
|
|
|
* @package Preetender\Routing |
13
|
|
|
*/ |
14
|
|
|
class Router implements RouterVerbs |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
protected $routeStack = []; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $routesGroupByName = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Routing GET call |
24
|
|
|
* |
25
|
|
|
* @param $path |
26
|
|
|
* @param $callable |
27
|
|
|
* @param null $name |
28
|
|
|
* @return Route |
29
|
|
|
*/ |
30
|
|
|
public function get($path, $callable, $name = null) |
31
|
|
|
{ |
32
|
|
|
return $this->addRoute($path, $callable, 'GET', $name); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Routing POST call |
37
|
|
|
* |
38
|
|
|
* @param $path |
39
|
|
|
* @param $callable |
40
|
|
|
* @param null $name |
41
|
|
|
* @return Route |
42
|
|
|
*/ |
43
|
|
|
public function post($path, $callable, $name = null) |
44
|
|
|
{ |
45
|
|
|
return $this->addRoute($path, $callable, 'POST', $name); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Routing PUT call |
50
|
|
|
* |
51
|
|
|
* @param $path |
52
|
|
|
* @param $callable |
53
|
|
|
* @param null $name |
54
|
|
|
* @return Route |
55
|
|
|
*/ |
56
|
|
|
public function put($path, $callable, $name = null) |
57
|
|
|
{ |
58
|
|
|
return $this->addRoute($path, $callable, 'PUT', $name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Routing PATCH call |
63
|
|
|
* |
64
|
|
|
* @param $path |
65
|
|
|
* @param $callable |
66
|
|
|
* @param null $name |
67
|
|
|
* @return Route |
68
|
|
|
*/ |
69
|
|
|
public function patch($path, $callable, $name = null) |
70
|
|
|
{ |
71
|
|
|
return $this->addRoute($path, $callable, 'PATCH', $name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Routing DELETE call |
76
|
|
|
* |
77
|
|
|
* @param $path |
78
|
|
|
* @param $callable |
79
|
|
|
* @param null $name |
80
|
|
|
* @return Route |
81
|
|
|
*/ |
82
|
|
|
public function delete($path, $callable, $name = null) |
83
|
|
|
{ |
84
|
|
|
return $this->addRoute($path, $callable, 'DELETE', $name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Obtem metodo resiquisato. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function method() : string |
93
|
|
|
{ |
94
|
|
|
return $_SERVER['REQUEST_METHOD']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get url request and delete queryString |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function uri() : string |
102
|
|
|
{ |
103
|
|
|
return trim(strtok( $_SERVER['REQUEST_URI'], '?'), '/'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if call contains png,jpg,jpeg,gif,css,js |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
protected function hasRouteValid() |
112
|
|
|
{ |
113
|
|
|
return preg_match('/\.(?:png|jpg|jpeg|gif|css|js)$/', $_SERVER["REQUEST_URI"]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* To check for potential problems before running |
118
|
|
|
* @return bool |
119
|
|
|
* @throws MethodNotAllowedException |
120
|
|
|
*/ |
121
|
|
|
protected function checkCommonProblems() |
122
|
|
|
{ |
123
|
|
|
if(self::hasRouteValid()) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
if(empty( $this->routeStack)) { |
127
|
|
|
throw new \InvalidArgumentException('Routing empty'); |
128
|
|
|
} |
129
|
|
|
if(!isset($this->routeStack[self::method()])) { |
130
|
|
|
throw new MethodNotAllowedException('Requested method was not scaled.'); |
131
|
|
|
} |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Register route |
137
|
|
|
* |
138
|
|
|
* @param $path |
139
|
|
|
* @param $callable |
140
|
|
|
* @param string $method |
141
|
|
|
* @param null $name |
142
|
|
|
* @return Route |
143
|
|
|
*/ |
144
|
|
|
protected function addRoute($path, $callable, $method = 'GET', $name = null) |
145
|
|
|
{ |
146
|
|
|
$route = new Route($path, $callable); |
147
|
|
|
$this->routeStack[$method][] = $route; |
148
|
|
|
if($name) { |
149
|
|
|
$this->routesGroupByName[$name] = $route; |
150
|
|
|
} |
151
|
|
|
return $route; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Obtain the requested method in the call and create the requested route. |
156
|
|
|
* @return mixed |
157
|
|
|
* @throws UnregisteredRouteException |
158
|
|
|
*/ |
159
|
|
|
protected function executeRoute() |
160
|
|
|
{ |
161
|
|
|
foreach ($this->routeStack[self::method()] as $route) { |
162
|
|
|
/** @var $route Route */ |
163
|
|
|
if($route->extractParameters(self::uri())) { |
164
|
|
|
return $route->run(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
throw new UnregisteredRouteException('Unregistered route '); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* ... go to route by name |
172
|
|
|
* |
173
|
|
|
* @param $name |
174
|
|
|
* @param array $params |
175
|
|
|
* @return mixed |
176
|
|
|
* @throws UnregisteredRouteException |
177
|
|
|
*/ |
178
|
|
|
public function goUrl($name, $params = []) |
179
|
|
|
{ |
180
|
|
|
if(!isset($this->routesGroupByName[$name])) { |
181
|
|
|
throw new UnregisteredRouteException('Unregistered route by name ' . $name); |
182
|
|
|
} |
183
|
|
|
/** @var Route $route */ |
184
|
|
|
$route = $this->routesGroupByName[$name]; |
185
|
|
|
return $route->requestUrlCall($params); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Execute |
190
|
|
|
* |
191
|
|
|
* @return bool|mixed |
192
|
|
|
* @throws MethodNotAllowedException |
193
|
|
|
*/ |
194
|
|
|
public function run() |
195
|
|
|
{ |
196
|
|
|
$this->checkCommonProblems(); |
197
|
|
|
|
198
|
|
|
return $this->executeRoute(); |
199
|
|
|
} |
200
|
|
|
} |