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