|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
|
4
|
|
|
|
|
5
|
|
|
class Resolver implements GroupResolution |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var callable |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $collections; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var callable |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $patterns; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Resolver constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param callable $patterns |
|
22
|
|
|
* @param callable $collections |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(callable $patterns, callable $collections) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->collections = $collections; |
|
27
|
|
|
$this->patterns = $patterns; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ResourceCollection $collection |
|
32
|
|
|
* |
|
33
|
|
|
* @return Pattern |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function pushCollection(ResourceCollection $collection): ResourceCollection |
|
36
|
|
|
{ |
|
37
|
|
|
return \call_user_func($this->collections, $collection); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Pattern $pattern |
|
42
|
|
|
* |
|
43
|
|
|
* @return Pattern |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function pushPattern(Pattern $pattern): Pattern |
|
46
|
|
|
{ |
|
47
|
|
|
return \call_user_func($this->patterns, $pattern); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $path |
|
52
|
|
|
* @param null|string $name |
|
53
|
|
|
* @return Pattern |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function pattern(string $path, ?string $name): Pattern |
|
56
|
|
|
{ |
|
57
|
|
|
return new Pattern($path, $name); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $methods |
|
62
|
|
|
* @param string $path |
|
63
|
|
|
* @param null|string $name |
|
64
|
|
|
* |
|
65
|
|
|
* @return Pattern |
|
66
|
|
|
*/ |
|
67
|
|
|
public function methods(array $methods, string $path, ?string $name): Pattern |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->pushPattern($this->pattern($path, $name))->methods($methods); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* GET|POST|PUT|PATCH|HEAD|OPTIONS|DELETE |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $path |
|
76
|
|
|
* @param null|string $name |
|
77
|
|
|
* |
|
78
|
|
|
* @return Pattern |
|
79
|
|
|
*/ |
|
80
|
|
|
public function any(string $path, ?string $name = null): Pattern |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->pushPattern($this->pattern($path, $name))->any(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $path |
|
87
|
|
|
* @param null|string $name |
|
88
|
|
|
* |
|
89
|
|
|
* @return Pattern |
|
90
|
|
|
*/ |
|
91
|
|
|
public function get(string $path, ?string $name = null): Pattern |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->pushPattern($this->pattern($path, $name))->get(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $path |
|
98
|
|
|
* @param null|string $name |
|
99
|
|
|
* |
|
100
|
|
|
* @return Pattern |
|
101
|
|
|
*/ |
|
102
|
|
|
public function post(string $path, ?string $name = null): Pattern |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->pushPattern($this->pattern($path, $name))->post(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $path |
|
109
|
|
|
* @param null|string $name |
|
110
|
|
|
* |
|
111
|
|
|
* @return Pattern |
|
112
|
|
|
*/ |
|
113
|
|
|
public function put(string $path, ?string $name = null): Pattern |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->pushPattern($this->pattern($path, $name))->put(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param null|string $name |
|
121
|
|
|
* |
|
122
|
|
|
* @return Pattern |
|
123
|
|
|
*/ |
|
124
|
|
|
public function patch(string $path, ?string $name = null): Pattern |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->pushPattern($this->pattern($path, $name))->patch(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $path |
|
131
|
|
|
* @param null|string $name |
|
132
|
|
|
* |
|
133
|
|
|
* @return Pattern |
|
134
|
|
|
*/ |
|
135
|
|
|
public function delete(string $path, ?string $name = null): Pattern |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->pushPattern($this->pattern($path, $name))->delete(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* @param string $index |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function name(string $name, string $index): string |
|
146
|
|
|
{ |
|
147
|
|
|
return $name . '.' . $index; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $entityName |
|
152
|
|
|
* @param string $action |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function action(string $entityName, string $action): string |
|
156
|
|
|
{ |
|
157
|
|
|
return $entityName . '/' . $action; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $entityName |
|
162
|
|
|
* @param string $id |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function id(string $entityName, string $id): string |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->action($entityName, '<' . $id . '>'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $entityName |
|
172
|
|
|
* @param string $id |
|
173
|
|
|
* @param string $action |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function idAction(string $entityName, string $id, string $action): string |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->action($this->id($entityName, $id), $action); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* entityName -> /users |
|
183
|
|
|
* |
|
184
|
|
|
* GET users.index /users |
|
185
|
|
|
* GET users.create /users/create |
|
186
|
|
|
* POST users.store /users |
|
187
|
|
|
* GET users.show /users/{id} |
|
188
|
|
|
* GET users.edit /users/{id}/edit |
|
189
|
|
|
* PUT/PATCH users.update /users/{id}/edit |
|
190
|
|
|
* DELETE users.destroy /users/{id} |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $entityName |
|
193
|
|
|
* @param null|string $name |
|
194
|
|
|
* @param null|string $id |
|
195
|
|
|
* |
|
196
|
|
|
* @return ResourceCollection |
|
197
|
|
|
*/ |
|
198
|
|
|
public function resource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection |
|
199
|
|
|
{ |
|
200
|
|
|
$entityName = \rtrim($entityName, '/'); |
|
201
|
|
|
$name = $name ?: \ltrim($entityName, '/'); |
|
202
|
|
|
$id = $id ?: $name; |
|
203
|
|
|
|
|
204
|
|
|
return $this->pushCollection(new ResourceCollection([ |
|
205
|
|
|
'index' => $this->pattern( |
|
206
|
|
|
$entityName, |
|
207
|
|
|
$this->name($name, 'index') |
|
208
|
|
|
)->get(), |
|
209
|
|
|
|
|
210
|
|
|
'create' => $this->pattern( |
|
211
|
|
|
$this->action($entityName, 'create'), |
|
212
|
|
|
$this->name($name, 'create') |
|
213
|
|
|
)->get(), |
|
214
|
|
|
|
|
215
|
|
|
'store' => $this->pattern( |
|
216
|
|
|
$entityName, |
|
217
|
|
|
$this->name($name, 'store') |
|
218
|
|
|
)->post(), |
|
219
|
|
|
|
|
220
|
|
|
'show' => $this->pattern( |
|
221
|
|
|
$this->id($entityName, $id), |
|
222
|
|
|
$this->name($name, 'show') |
|
223
|
|
|
)->get(), |
|
224
|
|
|
|
|
225
|
|
|
'edit' => $this->pattern( |
|
226
|
|
|
$this->idAction($entityName, $id, 'edit'), |
|
227
|
|
|
$this->name($name, 'edit') |
|
228
|
|
|
)->get(), |
|
229
|
|
|
|
|
230
|
|
|
'update' => $this->pattern( |
|
231
|
|
|
$this->idAction($entityName, $id, 'edit'), |
|
232
|
|
|
$this->name($name, 'update') |
|
233
|
|
|
)->setMethods(['PUT', 'PATCH']), |
|
234
|
|
|
|
|
235
|
|
|
'destroy' => $this->pattern( |
|
236
|
|
|
$this->id($entityName, $id), |
|
237
|
|
|
$this->name($name, 'destroy') |
|
238
|
|
|
)->delete(), |
|
239
|
|
|
])); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|