|
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
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* RouterBuilder |
|
23
|
|
|
* |
|
24
|
|
|
* @since 2.9.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
final class RouterBuilder |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ContainerInterface|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $container = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var CacheInterface|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $cache = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string|null |
|
41
|
|
|
*/ |
|
42
|
|
|
private $cacheKey = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array<string, string[]>|null |
|
46
|
|
|
*/ |
|
47
|
|
|
private $hosts = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var MiddlewareInterface[]|null |
|
51
|
|
|
*/ |
|
52
|
|
|
private $middlewares = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Loader\ConfigLoader|null |
|
56
|
|
|
*/ |
|
57
|
|
|
private $configLoader = null; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Loader\DescriptorLoader|null |
|
61
|
|
|
*/ |
|
62
|
|
|
private $descriptorLoader = null; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets the given container to the builder |
|
66
|
|
|
* |
|
67
|
|
|
* @param ContainerInterface|null $container |
|
68
|
|
|
* |
|
69
|
|
|
* @return self |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function setContainer(?ContainerInterface $container) : self |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->container = $container; |
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the given cache to the builder |
|
80
|
|
|
* |
|
81
|
|
|
* @param CacheInterface|null $cache |
|
82
|
|
|
* |
|
83
|
|
|
* @return self |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function setCache(?CacheInterface $cache) : self |
|
86
|
|
|
{ |
|
87
|
1 |
|
$this->cache = $cache; |
|
88
|
|
|
|
|
89
|
1 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Sets the given cache key to the builder |
|
94
|
|
|
* |
|
95
|
|
|
* @param string|null $cacheKey |
|
96
|
|
|
* |
|
97
|
|
|
* @return self |
|
98
|
|
|
* |
|
99
|
|
|
* @since 2.10.0 |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function setCacheKey(?string $cacheKey) : self |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->cacheKey = $cacheKey; |
|
104
|
|
|
|
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Uses the config loader when building |
|
110
|
|
|
* |
|
111
|
|
|
* @param string[] $resources |
|
112
|
|
|
* |
|
113
|
|
|
* @return self |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function useConfigLoader(array $resources) : self |
|
116
|
|
|
{ |
|
117
|
1 |
|
$this->configLoader = new Loader\ConfigLoader(); |
|
118
|
1 |
|
$this->configLoader->attachArray($resources); |
|
119
|
|
|
|
|
120
|
1 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Uses the descriptor loader when building |
|
125
|
|
|
* |
|
126
|
|
|
* @param string[] $resources |
|
127
|
|
|
* |
|
128
|
|
|
* @return self |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function useDescriptorLoader(array $resources) : self |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->descriptorLoader = new Loader\DescriptorLoader(); |
|
133
|
1 |
|
$this->descriptorLoader->attachArray($resources); |
|
134
|
|
|
|
|
135
|
1 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Uses the metadata loader when building |
|
140
|
|
|
* |
|
141
|
|
|
* Alias to the useDescriptorLoader method. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string[] $resources |
|
144
|
|
|
* |
|
145
|
|
|
* @return self |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function useMetadataLoader(array $resources) : self |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->useDescriptorLoader($resources); |
|
150
|
|
|
|
|
151
|
1 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Sets the given hosts to the builder |
|
156
|
|
|
* |
|
157
|
|
|
* @param array<string, string[]>|null $hosts |
|
158
|
|
|
* |
|
159
|
|
|
* @return self |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function setHosts(?array $hosts) : self |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->hosts = $hosts; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Sets the given middlewares to the builder |
|
170
|
|
|
* |
|
171
|
|
|
* @param MiddlewareInterface[]|null $middlewares |
|
172
|
|
|
* |
|
173
|
|
|
* @return self |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function setMiddlewares(?array $middlewares) : self |
|
176
|
|
|
{ |
|
177
|
1 |
|
$this->middlewares = $middlewares; |
|
178
|
|
|
|
|
179
|
1 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Builds the router |
|
184
|
|
|
* |
|
185
|
|
|
* @return Router |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function build() : Router |
|
188
|
|
|
{ |
|
189
|
1 |
|
$router = new Router(); |
|
190
|
|
|
|
|
191
|
1 |
|
if (isset($this->configLoader)) { |
|
192
|
1 |
|
$this->configLoader->setContainer($this->container); |
|
|
|
|
|
|
193
|
1 |
|
$router->load($this->configLoader); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
if (isset($this->descriptorLoader)) { |
|
197
|
1 |
|
$this->descriptorLoader->setContainer($this->container); |
|
|
|
|
|
|
198
|
1 |
|
$this->descriptorLoader->setCache($this->cache); |
|
199
|
1 |
|
$this->descriptorLoader->setCacheKey($this->cacheKey); |
|
200
|
1 |
|
$router->load($this->descriptorLoader); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
if (!empty($this->hosts)) { |
|
204
|
1 |
|
foreach ($this->hosts as $alias => $hostnames) { |
|
205
|
1 |
|
$router->addHost($alias, ...$hostnames); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
1 |
|
if (!empty($this->middlewares)) { |
|
210
|
1 |
|
$router->addMiddleware(...$this->middlewares); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
1 |
|
return $router; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
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.