1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ThallesDella\FactoryRouter; |
4
|
|
|
|
5
|
|
|
use CoffeeCode\Router\Router; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Factory Router | Class Routes [ TEMPLATE ] |
9
|
|
|
* |
10
|
|
|
* @category FactoryRouter\Template |
11
|
|
|
* @package ThallesDella\FactoryRouter |
12
|
|
|
* @author Thalles D. koester <[email protected]> |
13
|
|
|
* @license https://choosealicense.com/licenses/mit/ MIT |
14
|
|
|
* @link https://github.com/thallesdella/factory-router |
15
|
|
|
*/ |
16
|
|
|
abstract class Routes |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Router Object |
20
|
|
|
* |
21
|
|
|
* @var Router |
22
|
|
|
*/ |
23
|
|
|
protected $router; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Controller name |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $controller; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Routes constructor. |
35
|
|
|
* |
36
|
|
|
* @param Router $router Router object |
37
|
|
|
* @param string $controllerName Controller name |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Router $router, string $controllerName) |
40
|
|
|
{ |
41
|
|
|
$this->router = $router; |
42
|
|
|
$this->controller = $controllerName; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Update router object |
47
|
|
|
* |
48
|
|
|
* @return Router |
49
|
|
|
*/ |
50
|
|
|
abstract public function updateRouter(): Router; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Modify the defined namespace |
55
|
|
|
* |
56
|
|
|
* @param string|null $ns New namespace |
57
|
|
|
* |
58
|
|
|
* @return Routes |
59
|
|
|
*/ |
60
|
|
|
public function namespace(?string $ns): Routes |
61
|
|
|
{ |
62
|
|
|
$this->router->namespace($ns); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Define a routes group |
68
|
|
|
* |
69
|
|
|
* @param string|null $group Name of the group |
70
|
|
|
* |
71
|
|
|
* @return Router |
72
|
|
|
*/ |
73
|
|
|
public function group(?string $group): Router |
74
|
|
|
{ |
75
|
|
|
$this->router->group($group); |
76
|
|
|
return $this->router; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Define a method get route |
81
|
|
|
* |
82
|
|
|
* @param string $route Route |
83
|
|
|
* @param string $name Nickname to the route |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function get(string $route, string $name): void |
88
|
|
|
{ |
89
|
|
|
$this->router->get( |
90
|
|
|
$route, |
91
|
|
|
$this->getHandler($name), |
92
|
|
|
$this->getName($name) |
93
|
|
|
); |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Define a method post route |
99
|
|
|
* |
100
|
|
|
* @param string $route Route |
101
|
|
|
* @param string $name Nickname to the route |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function post(string $route, string $name): void |
106
|
|
|
{ |
107
|
|
|
$this->router->post( |
108
|
|
|
$route, |
109
|
|
|
$this->getHandler($name), |
110
|
|
|
$this->getName($name) |
111
|
|
|
); |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Define a method put route |
117
|
|
|
* |
118
|
|
|
* @param string $route Route |
119
|
|
|
* @param string $name Nickname to the route |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function put(string $route, string $name): void |
124
|
|
|
{ |
125
|
|
|
$this->router->put( |
126
|
|
|
$route, |
127
|
|
|
$this->getHandler($name), |
128
|
|
|
$this->getName($name) |
129
|
|
|
); |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Define a method delete route |
135
|
|
|
* |
136
|
|
|
* @param string $route Route |
137
|
|
|
* @param string $name Nickname to the route |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function delete(string $route, string $name): void |
142
|
|
|
{ |
143
|
|
|
$this->router->delete( |
144
|
|
|
$route, |
145
|
|
|
$this->getHandler($name), |
146
|
|
|
$this->getName($name) |
147
|
|
|
); |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get handler for the route |
153
|
|
|
* |
154
|
|
|
* @param string $name Nickname of the route |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
private function getHandler(string $name): string |
159
|
|
|
{ |
160
|
|
|
$controller = ucfirst($this->controller); |
161
|
|
|
return "{$controller}:{$name}"; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get name for the route |
166
|
|
|
* |
167
|
|
|
* @param string $name Nickname of the route |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
private function getName(string $name): string |
172
|
|
|
{ |
173
|
|
|
$controller = strtolower($this->controller); |
174
|
|
|
return "{$controller}.{$name}"; |
175
|
|
|
} |
176
|
|
|
} |