1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Router; |
3
|
|
|
|
4
|
|
|
trait RoutesSet |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Mapping of routes to their execution functions for GET requests |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $getRoutes = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Mapping of routes to their execution functions for GET requests |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $postRoutes = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Mapping of routes to their execution functions for PUT requests |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $putRoutes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Mapping of routes to their execution functions for DELETE requests |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $deleteRoutes = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Mapping of routes to their execution functions for OPTION requests |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $optionRoutes = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This flag rises when we add route / * / |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $universalRouteWasAdded = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Method returns list of routes for the HTTP method. |
51
|
|
|
* |
52
|
|
|
* @param string $method |
53
|
|
|
* HTTP Method |
54
|
|
|
* @return array Routes |
55
|
|
|
*/ |
56
|
|
|
public function &getRoutesForMethod(string $method): array |
57
|
|
|
{ |
58
|
|
|
switch ($method) { |
59
|
|
|
case ('GET'): |
60
|
|
|
$result = &$this->getRoutes; |
61
|
|
|
break; |
62
|
|
|
|
63
|
|
|
case ('POST'): |
64
|
|
|
$result = &$this->postRoutes; |
65
|
|
|
break; |
66
|
|
|
|
67
|
|
|
case ('PUT'): |
68
|
|
|
$result = &$this->putRoutes; |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case ('DELETE'): |
72
|
|
|
$result = &$this->deleteRoutes; |
73
|
|
|
break; |
74
|
|
|
|
75
|
|
|
case ('OPTION'): |
76
|
|
|
$result = &$this->deleteRoutes; |
77
|
|
|
break; |
78
|
|
|
|
79
|
|
|
default: |
80
|
|
|
throw (new \Exception('Unsupported request method')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Method clears router data. |
88
|
|
|
*/ |
89
|
|
|
public function clear() |
90
|
|
|
{ |
91
|
|
|
$this->getRoutes = []; |
92
|
|
|
|
93
|
|
|
$this->postRoutes = []; |
94
|
|
|
|
95
|
|
|
$this->putRoutes = []; |
96
|
|
|
|
97
|
|
|
$this->deleteRoutes = []; |
98
|
|
|
|
99
|
|
|
$this->optionRoutes = []; |
100
|
|
|
|
101
|
|
|
$this->universalRouteWasAdded = false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Method returns true if the router exists |
106
|
|
|
* |
107
|
|
|
* @param string $route |
108
|
|
|
* checking route |
109
|
|
|
* @return bool true if the router exists, false otherwise |
110
|
|
|
*/ |
111
|
|
|
public function routeExists(string $route): bool |
112
|
|
|
{ |
113
|
|
|
$allRoutes = array_merge($this->deleteRoutes, $this->putRoutes, $this->postRoutes, $this->getRoutes, $this->optionRoutes); |
114
|
|
|
|
115
|
|
|
return isset($allRoutes[$route]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Method rturns all available routes |
120
|
|
|
*/ |
121
|
|
|
public function getAllRoutesTrace() |
122
|
|
|
{ |
123
|
|
|
return (count($this->getRoutes) ? 'GET:' . implode(', ', array_keys($this->getRoutes)) . '; ' : '') . |
124
|
|
|
(count($this->postRoutes) ? 'POST:' . implode(', ', array_keys($this->postRoutes)) . '; ' : '') . |
125
|
|
|
(count($this->putRoutes) ? 'PUT:' . implode(', ', array_keys($this->putRoutes)) . '; ' : '') . |
126
|
|
|
(count($this->deleteRoutes) ? 'DELETE:' . implode(', ', array_keys($this->deleteRoutes)) : '') . |
127
|
|
|
(count($this->optionRoutes) ? 'OPTION:' . implode(', ', array_keys($this->optionRoutes)) : ''); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Additing route for GET request |
132
|
|
|
* |
133
|
|
|
* @param string $route route |
134
|
|
|
* @param object $object callback object |
135
|
|
|
* @param string $method callback method |
136
|
|
|
*/ |
137
|
|
|
public function addGetRoute(string $route, object $object, string $method):void{ |
138
|
|
|
$this->getRoutes["/$route/"] = [ |
139
|
|
|
$object, |
140
|
|
|
$method |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Additing route for GET request |
146
|
|
|
* |
147
|
|
|
* @param string $route route |
148
|
|
|
* @param object $object callback object |
149
|
|
|
* @param string $method callback method |
150
|
|
|
*/ |
151
|
|
|
public function addPostRoute(string $route, object $object, string $method):void{ |
152
|
|
|
$this->postRoutes["/$route/"] = [ |
153
|
|
|
$object, |
154
|
|
|
$method |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |