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
|
|
|
* entityName -> /users |
142
|
|
|
* |
143
|
|
|
* GET users.index /users |
144
|
|
|
* GET users.create /users/create |
145
|
|
|
* POST users.store /users |
146
|
|
|
* GET users.show /users/{id} |
147
|
|
|
* GET users.edit /users/{id}/edit |
148
|
|
|
* PUT/PATCH users.update /users/{id}/edit |
149
|
|
|
* DELETE users.destroy /users/{id} |
150
|
|
|
* |
151
|
|
|
* @param string $entityName |
152
|
|
|
* @param null|string $name |
153
|
|
|
* @param null|string $id |
154
|
|
|
* |
155
|
|
|
* @return ResourceCollection |
156
|
|
|
*/ |
157
|
|
|
public function resource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection |
158
|
|
|
{ |
159
|
|
|
$entityName = \rtrim($entityName, '/'); |
160
|
|
|
$name = $name ?: \ltrim($entityName, '/'); |
161
|
|
|
$id = $id ?: $name; |
162
|
|
|
|
163
|
|
|
$collection = new ResourceCollection(); |
164
|
|
|
|
165
|
|
|
$index = 'index'; |
166
|
|
|
$collection[$index] = $this |
167
|
|
|
->pattern($entityName, $name . '.' . $index) |
168
|
|
|
->get(); |
169
|
|
|
|
170
|
|
|
$index = 'create'; |
171
|
|
|
$collection[$index] = $this |
172
|
|
|
->pattern($entityName . '/create', $name . '.' . $index) |
173
|
|
|
->get(); |
174
|
|
|
|
175
|
|
|
$index = 'store'; |
176
|
|
|
$collection[$index] = $this |
177
|
|
|
->pattern($entityName, $name . '.' . $index) |
178
|
|
|
->post(); |
179
|
|
|
|
180
|
|
|
$index = 'show'; |
181
|
|
|
$collection[$index] = $this |
182
|
|
|
->pattern($entityName . '/<' . $id . '>', $name . '.' . $index) |
183
|
|
|
->get(); |
184
|
|
|
|
185
|
|
|
$index = 'edit'; |
186
|
|
|
$collection[$index] = $this |
187
|
|
|
->pattern($entityName . '/<' . $id . '>/edit', $name . '.' . $index) |
188
|
|
|
->get(); |
189
|
|
|
|
190
|
|
|
$index = 'update'; |
191
|
|
|
$collection[$index] = $this |
192
|
|
|
->pattern($entityName . '/<' . $id . '>/edit', $name . '.' . $index) |
193
|
|
|
->setMethods(['PUT', 'PATCH']); |
194
|
|
|
|
195
|
|
|
$index = 'destroy'; |
196
|
|
|
$collection[$index] = $this |
197
|
|
|
->pattern($entityName . '/<' . $id . '>', $name . '.' . $index) |
198
|
|
|
->delete(); |
199
|
|
|
|
200
|
|
|
return $this->pushCollection($collection); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|