|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HnrAzevedo\Router; |
|
6
|
|
|
|
|
7
|
|
|
use HnrAzevedo\Http\Uri; |
|
8
|
|
|
|
|
9
|
|
|
trait DefinitionsTrait |
|
10
|
|
|
{ |
|
11
|
|
|
use Helper; |
|
12
|
|
|
|
|
13
|
|
|
public static function get(string $uri, $closure): RouterInterface |
|
14
|
|
|
{ |
|
15
|
|
|
return self::set('get',$uri,$closure); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public static function post(string $uri, $closure): RouterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
return self::set('post',$uri,$closure); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function ajax(string $uri, $closure): RouterInterface |
|
24
|
|
|
{ |
|
25
|
|
|
return self::set('ajax',$uri,$closure); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function delete(string $uri, $closure): RouterInterface |
|
29
|
|
|
{ |
|
30
|
|
|
return self::set('delete',$uri,$closure); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function put(string $uri, $closure): RouterInterface |
|
34
|
|
|
{ |
|
35
|
|
|
return self::set('put',$uri,$closure); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function patch(string $uri, $closure): RouterInterface |
|
39
|
|
|
{ |
|
40
|
|
|
return self::set('patch',$uri,$closure); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function match(string $method, string $uri, $closure): RouterInterface |
|
44
|
|
|
{ |
|
45
|
|
|
self::set($method, $uri, $closure); |
|
46
|
|
|
return self::getInstance(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function any(string $uri, $closure): RouterInterface |
|
50
|
|
|
{ |
|
51
|
|
|
return self::set('*',$uri,$closure); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function set(string $method, string $uri, $closure): RouterInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
57
|
|
|
|
|
58
|
|
|
self::checkDuplicity($uri,$method); |
|
59
|
|
|
|
|
60
|
|
|
$routes = self::getInstance()->getRoutes(); |
|
61
|
|
|
|
|
62
|
|
|
$index = sha1(date('d/m/Y H:i:s').count($routes)); |
|
63
|
|
|
|
|
64
|
|
|
while(array_key_exists($index,$routes)){ |
|
65
|
|
|
$index = sha1(date('d/m/Y H:i:s').rand(count($routes))); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$routes[$index] = [ |
|
69
|
|
|
'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
70
|
|
|
'action' => $closure, |
|
71
|
|
|
'method' => strtoupper($method), |
|
72
|
|
|
'middlewares' => [], |
|
73
|
|
|
'where' => [], |
|
74
|
|
|
'before' => [], |
|
75
|
|
|
'after' => [], |
|
76
|
|
|
'group' => self::getInstance()->getGroup(), |
|
77
|
|
|
'response' => null, |
|
78
|
|
|
'name' => $index |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
self::getInstance()->setRoutes($routes); |
|
82
|
|
|
|
|
83
|
|
|
return self::getInstance(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private static function checkDuplicity(string $uri, string $method): void |
|
87
|
|
|
{ |
|
88
|
|
|
foreach(self::getInstance()->getRoutes() as $route){ |
|
89
|
|
|
if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
90
|
|
|
throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|