1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
4
|
|
|
|
5
|
|
|
class Group implements \JsonSerializable |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var ResourceCollection[] |
10
|
|
|
*/ |
11
|
|
|
protected $collections = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Pattern[] |
15
|
|
|
*/ |
16
|
|
|
protected $resolver = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $protocol; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $host; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $extends = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $defaults = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $methods; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Group constructor. |
55
|
|
|
* |
56
|
|
|
* @param null|string $prefix |
57
|
|
|
* @param callable $callback |
58
|
|
|
*/ |
59
|
|
|
public function __construct(string $prefix, callable $callback) |
60
|
|
|
{ |
61
|
|
|
$resolver = new Resolver(function (Pattern $pattern) { |
62
|
|
|
return $this->pushPattern($pattern); |
63
|
|
|
}, function (ResourceCollection $collection) { |
64
|
|
|
return $this->pushCollection($collection); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$this->path = $prefix; |
68
|
|
|
$closure = \Closure::fromCallable($callback); |
69
|
|
|
$closure->call($resolver, $resolver); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Pattern $pattern |
74
|
|
|
* |
75
|
|
|
* @return Pattern |
76
|
|
|
*/ |
77
|
|
|
protected function pushPattern($pattern): Pattern |
78
|
|
|
{ |
79
|
|
|
$this->resolver[$pattern->getName()] = $pattern; |
80
|
|
|
return $pattern; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param ResourceCollection $collection |
85
|
|
|
* |
86
|
|
|
* @return ResourceCollection |
87
|
|
|
*/ |
88
|
|
|
protected function pushCollection(ResourceCollection $collection): ResourceCollection |
89
|
|
|
{ |
90
|
|
|
$this->collections[] = $collection; |
91
|
|
|
return $collection; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function jsonSerialize(): array |
98
|
|
|
{ |
99
|
|
|
return $this->toArray(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function toArray(): array |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
$this->getName() => [ |
109
|
|
|
'type' => 'prefix', |
110
|
|
|
'protocol' => $this->getProtocol(), |
111
|
|
|
'host' => $this->getHost(), |
112
|
|
|
'path' => $this->path, |
113
|
|
|
'methods' => $this->getMethods(), |
114
|
|
|
'resolver' => $this->getResolver(), |
115
|
|
|
'defaults' => $this->getDefaults(), |
116
|
|
|
'extends' => $this->getExtends(), |
117
|
|
|
] |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getName(): string |
125
|
|
|
{ |
126
|
|
|
return $this->name ?: Helper::generateName($this->path); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setName(string $name): self |
135
|
|
|
{ |
136
|
|
|
$this->name = $name; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return null|string |
142
|
|
|
*/ |
143
|
|
|
public function getProtocol(): ?string |
144
|
|
|
{ |
145
|
|
|
return $this->protocol; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $protocol |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function setProtocol(string $protocol): self |
153
|
|
|
{ |
154
|
|
|
$this->protocol = $protocol; |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getHost(): ?string |
162
|
|
|
{ |
163
|
|
|
return $this->host; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $host |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function setHost(string $host): self |
171
|
|
|
{ |
172
|
|
|
$this->host = $host; |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return null|array |
178
|
|
|
*/ |
179
|
|
|
public function getMethods(): ?array |
180
|
|
|
{ |
181
|
|
|
return $this->methods; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param null|array $methods |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setMethods(?array $methods): self |
189
|
|
|
{ |
190
|
|
|
$this->methods = $methods; |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
protected function getResolver(): array |
198
|
|
|
{ |
199
|
|
|
$patterns = []; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @var Pattern[] $resolver |
203
|
|
|
*/ |
204
|
|
|
$resolver = \array_merge( |
205
|
|
|
$this->resolver, |
206
|
|
|
\iterator_to_array($this->patterns()) |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
foreach ($resolver as $pattern) { |
210
|
|
|
$patterns[] = $pattern->toArray(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return \array_merge(...$patterns); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return \Generator |
218
|
|
|
*/ |
219
|
|
|
protected function patterns(): \Generator |
220
|
|
|
{ |
221
|
|
|
foreach ($this->collections as $collection) { |
222
|
|
|
foreach ($collection as $pattern) { |
223
|
|
|
yield $pattern; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
public function getDefaults(): array |
232
|
|
|
{ |
233
|
|
|
return $this->defaults; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param array $defaults |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
|
|
public function setDefaults(array $defaults): self |
241
|
|
|
{ |
242
|
|
|
$this->defaults = $defaults; |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
public function getExtends(): array |
250
|
|
|
{ |
251
|
|
|
return $this->extends; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param array $extends |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setExtends(array $extends): self |
259
|
|
|
{ |
260
|
|
|
$this->extends = $extends; |
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|