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
|
|
|
* @var Router |
20
|
|
|
*/ |
21
|
|
|
private $_router; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $_controller; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Routes constructor. |
31
|
|
|
* |
32
|
|
|
* @param Router $router |
33
|
|
|
* @param string $className |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Router $router, string $className) |
36
|
|
|
{ |
37
|
|
|
$this->_router = $router; |
38
|
|
|
|
39
|
|
|
$buf = explode('\\', $className); |
40
|
|
|
$this->_controller = end($buf); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Router |
45
|
|
|
*/ |
46
|
|
|
public function updateRouter(): Router |
47
|
|
|
{ |
48
|
|
|
return $this->_router; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string|null $ns |
53
|
|
|
* |
54
|
|
|
* @return Routes |
55
|
|
|
*/ |
56
|
|
|
public function namespace(?string $ns): Routes |
57
|
|
|
{ |
58
|
|
|
$this->_router->namespace($ns); |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string|null $group |
64
|
|
|
* |
65
|
|
|
* @return Router |
66
|
|
|
*/ |
67
|
|
|
public function group(?string $group): Router |
68
|
|
|
{ |
69
|
|
|
$this->_router->group($group); |
70
|
|
|
return $this->_router; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $route |
75
|
|
|
* @param string $name |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function get(string $route, string $name): void |
80
|
|
|
{ |
81
|
|
|
$this->_router->get( |
82
|
|
|
$route, |
83
|
|
|
$this->_getHandler($name), |
84
|
|
|
$this->_getName($name) |
85
|
|
|
); |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $route |
91
|
|
|
* @param string $name |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function post(string $route, string $name): void |
96
|
|
|
{ |
97
|
|
|
$this->_router->post( |
98
|
|
|
$route, |
99
|
|
|
$this->_getHandler($name), |
100
|
|
|
$this->_getName($name) |
101
|
|
|
); |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $route |
107
|
|
|
* @param string $name |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function put(string $route, string $name): void |
112
|
|
|
{ |
113
|
|
|
$this->_router->put( |
114
|
|
|
$route, |
115
|
|
|
$this->_getHandler($name), |
116
|
|
|
$this->_getName($name) |
117
|
|
|
); |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $route |
123
|
|
|
* @param string $name |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function delete(string $route, string $name): void |
128
|
|
|
{ |
129
|
|
|
$this->_router->delete( |
130
|
|
|
$route, |
131
|
|
|
$this->_getHandler($name), |
132
|
|
|
$this->_getName($name) |
133
|
|
|
); |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
private function _getHandler(string $name): string |
143
|
|
|
{ |
144
|
|
|
$controller = ucfirst($this->_controller); |
145
|
|
|
return "{$controller}:{$name}"; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $name |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
private function _getName(string $name): string |
154
|
|
|
{ |
155
|
|
|
$controller = strtolower($this->_controller); |
156
|
|
|
return "{$controller}.{$name}"; |
157
|
|
|
} |
158
|
|
|
} |