1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Container\ContainerInterface; |
18
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
19
|
|
|
use Psr\SimpleCache\CacheInterface; |
20
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* RouterBuilder |
24
|
|
|
* |
25
|
|
|
* @since 2.9.0 |
26
|
|
|
*/ |
27
|
|
|
final class RouterBuilder |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var EventDispatcherInterface|null |
32
|
|
|
* |
33
|
|
|
* @since 2.14.0 |
34
|
|
|
*/ |
35
|
|
|
private $eventDispatcher = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ContainerInterface|null |
39
|
|
|
*/ |
40
|
|
|
private $container = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CacheInterface|null |
44
|
|
|
*/ |
45
|
|
|
private $cache = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|null |
49
|
|
|
*/ |
50
|
|
|
private $cacheKey = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array<string, string>|null |
54
|
|
|
*/ |
55
|
|
|
private $patterns = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array<string, string[]>|null |
59
|
|
|
*/ |
60
|
|
|
private $hosts = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var MiddlewareInterface[]|null |
64
|
|
|
*/ |
65
|
|
|
private $middlewares = null; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var Loader\ConfigLoader|null |
69
|
|
|
*/ |
70
|
|
|
private $configLoader = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var Loader\DescriptorLoader|null |
74
|
|
|
*/ |
75
|
|
|
private $descriptorLoader = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the given event dispatcher to the builder |
79
|
|
|
* |
80
|
|
|
* @param EventDispatcherInterface|null $eventDispatcher |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
* |
84
|
|
|
* @since 2.14.0 |
85
|
|
|
*/ |
86
|
1 |
|
public function setEventDispatcher(?EventDispatcherInterface $eventDispatcher) : self |
87
|
|
|
{ |
88
|
1 |
|
$this->eventDispatcher = $eventDispatcher; |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets the given container to the builder |
95
|
|
|
* |
96
|
|
|
* @param ContainerInterface|null $container |
97
|
|
|
* |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
1 |
|
public function setContainer(?ContainerInterface $container) : self |
101
|
|
|
{ |
102
|
1 |
|
$this->container = $container; |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the given cache to the builder |
109
|
|
|
* |
110
|
|
|
* @param CacheInterface|null $cache |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
1 |
|
public function setCache(?CacheInterface $cache) : self |
115
|
|
|
{ |
116
|
1 |
|
$this->cache = $cache; |
117
|
|
|
|
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets the given cache key to the builder |
123
|
|
|
* |
124
|
|
|
* @param string|null $cacheKey |
125
|
|
|
* |
126
|
|
|
* @return self |
127
|
|
|
* |
128
|
|
|
* @since 2.10.0 |
129
|
|
|
*/ |
130
|
1 |
|
public function setCacheKey(?string $cacheKey) : self |
131
|
|
|
{ |
132
|
1 |
|
$this->cacheKey = $cacheKey; |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Uses the config loader when building |
139
|
|
|
* |
140
|
|
|
* @param string[] $resources |
141
|
|
|
* |
142
|
|
|
* @return self |
143
|
|
|
*/ |
144
|
1 |
|
public function useConfigLoader(array $resources) : self |
145
|
|
|
{ |
146
|
1 |
|
$this->configLoader = new Loader\ConfigLoader(); |
147
|
1 |
|
$this->configLoader->attachArray($resources); |
148
|
|
|
|
149
|
1 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Uses the descriptor loader when building |
154
|
|
|
* |
155
|
|
|
* @param string[] $resources |
156
|
|
|
* |
157
|
|
|
* @return self |
158
|
|
|
*/ |
159
|
1 |
|
public function useDescriptorLoader(array $resources) : self |
160
|
|
|
{ |
161
|
1 |
|
$this->descriptorLoader = new Loader\DescriptorLoader(); |
162
|
1 |
|
$this->descriptorLoader->attachArray($resources); |
163
|
|
|
|
164
|
1 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Uses the metadata loader when building |
169
|
|
|
* |
170
|
|
|
* Alias to the useDescriptorLoader method. |
171
|
|
|
* |
172
|
|
|
* @param string[] $resources |
173
|
|
|
* |
174
|
|
|
* @return self |
175
|
|
|
*/ |
176
|
1 |
|
public function useMetadataLoader(array $resources) : self |
177
|
|
|
{ |
178
|
1 |
|
$this->useDescriptorLoader($resources); |
179
|
|
|
|
180
|
1 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Sets the given patterns to the builder |
185
|
|
|
* |
186
|
|
|
* @param array<string, string>|null $patterns |
187
|
|
|
* |
188
|
|
|
* @return self |
189
|
|
|
* |
190
|
|
|
* @since 2.11.0 |
191
|
|
|
*/ |
192
|
1 |
|
public function setPatterns(?array $patterns) : self |
193
|
|
|
{ |
194
|
1 |
|
$this->patterns = $patterns; |
195
|
|
|
|
196
|
1 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Sets the given hosts to the builder |
201
|
|
|
* |
202
|
|
|
* @param array<string, string[]>|null $hosts |
203
|
|
|
* |
204
|
|
|
* @return self |
205
|
|
|
*/ |
206
|
1 |
|
public function setHosts(?array $hosts) : self |
207
|
|
|
{ |
208
|
1 |
|
$this->hosts = $hosts; |
209
|
|
|
|
210
|
1 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Sets the given middlewares to the builder |
215
|
|
|
* |
216
|
|
|
* @param MiddlewareInterface[]|null $middlewares |
217
|
|
|
* |
218
|
|
|
* @return self |
219
|
|
|
*/ |
220
|
1 |
|
public function setMiddlewares(?array $middlewares) : self |
221
|
|
|
{ |
222
|
1 |
|
$this->middlewares = $middlewares; |
223
|
|
|
|
224
|
1 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Builds the router |
229
|
|
|
* |
230
|
|
|
* @return Router |
231
|
|
|
*/ |
232
|
1 |
|
public function build() : Router |
233
|
|
|
{ |
234
|
1 |
|
$router = new Router(); |
235
|
|
|
|
236
|
1 |
|
if (isset($this->eventDispatcher)) { |
237
|
1 |
|
$router->setEventDispatcher($this->eventDispatcher); |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
if (isset($this->configLoader)) { |
241
|
1 |
|
$this->configLoader->setContainer($this->container); |
|
|
|
|
242
|
1 |
|
$router->load($this->configLoader); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
1 |
|
if (isset($this->descriptorLoader)) { |
246
|
1 |
|
$this->descriptorLoader->setContainer($this->container); |
|
|
|
|
247
|
1 |
|
$this->descriptorLoader->setCache($this->cache); |
248
|
1 |
|
$this->descriptorLoader->setCacheKey($this->cacheKey); |
249
|
1 |
|
$router->load($this->descriptorLoader); |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
if (!empty($this->patterns)) { |
253
|
1 |
|
$router->addPatterns($this->patterns); |
254
|
|
|
} |
255
|
|
|
|
256
|
1 |
|
if (!empty($this->hosts)) { |
257
|
1 |
|
$router->addHosts($this->hosts); |
258
|
|
|
} |
259
|
|
|
|
260
|
1 |
|
if (!empty($this->middlewares)) { |
261
|
1 |
|
$router->addMiddleware(...$this->middlewares); |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
return $router; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.