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 null|ContainerInterface |
31
|
|
|
*/ |
32
|
|
|
private $container = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var null|CacheInterface |
36
|
|
|
*/ |
37
|
|
|
private $cache = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var null|array<string, string[]> |
41
|
|
|
*/ |
42
|
|
|
private $hosts = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null|MiddlewareInterface[] |
46
|
|
|
*/ |
47
|
|
|
private $middlewares = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var null|Loader\CollectableFileLoader |
51
|
|
|
*/ |
52
|
|
|
private $configLoader = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var null|Loader\DescriptorDirectoryLoader |
56
|
|
|
*/ |
57
|
|
|
private $metadataLoader = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param null|ContainerInterface $container |
61
|
|
|
* |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
|
|
public function setContainer(?ContainerInterface $container) : self |
65
|
|
|
{ |
66
|
|
|
$this->container = $container; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param null|CacheInterface $cache |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
|
|
public function setCache(?CacheInterface $cache) : self |
77
|
|
|
{ |
78
|
|
|
$this->cache = $cache; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string[] $resources |
85
|
|
|
* |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
|
|
public function useConfigLoader(array $resources) : self |
89
|
|
|
{ |
90
|
|
|
$this->configLoader = new Loader\CollectableFileLoader(); |
91
|
|
|
$this->configLoader->attachArray($resources); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string[] $resources |
98
|
|
|
* |
99
|
|
|
* @return self |
100
|
|
|
*/ |
101
|
|
|
public function useMetadataLoader(array $resources) : self |
102
|
|
|
{ |
103
|
|
|
$this->metadataLoader = new Loader\DescriptorDirectoryLoader(); |
104
|
|
|
$this->metadataLoader->attachArray($resources); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param null|array<string, string[]> $hosts |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
|
|
public function setHosts(?array $hosts) : self |
115
|
|
|
{ |
116
|
|
|
$this->hosts = $hosts; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param null|MiddlewareInterface[] $middlewares |
123
|
|
|
* |
124
|
|
|
* @return self |
125
|
|
|
*/ |
126
|
|
|
public function setMiddlewares(?array $middlewares) : self |
127
|
|
|
{ |
128
|
|
|
$this->middlewares = $middlewares; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Router |
135
|
|
|
*/ |
136
|
|
|
public function build() : Router |
137
|
|
|
{ |
138
|
|
|
$router = new Router(); |
139
|
|
|
|
140
|
|
|
if (isset($this->configLoader)) { |
141
|
|
|
$this->configLoader->setContainer($this->container); |
|
|
|
|
142
|
|
|
$router->load($this->configLoader); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (isset($this->metadataLoader)) { |
146
|
|
|
$this->metadataLoader->setContainer($this->container); |
|
|
|
|
147
|
|
|
$this->metadataLoader->setCache($this->cache); |
148
|
|
|
$router->load($this->metadataLoader); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!empty($this->hosts)) { |
152
|
|
|
foreach ($this->hosts as $alias => $hostnames) { |
153
|
|
|
$router->addHost($alias, ...$hostnames); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!empty($this->middlewares)) { |
158
|
|
|
$router->addMiddleware(...$this->middlewares); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $router; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.