-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRouterManager.php
More file actions
313 lines (272 loc) · 6.67 KB
/
RouterManager.php
File metadata and controls
313 lines (272 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php declare(strict_types=1);
/**
* Created by PhpStorm.
* User: inhere
* Date: 2018-02-09
* Time: 10:03
*/
namespace Inhere\Route;
use InvalidArgumentException;
use function in_array;
use function is_string;
/**
* Class RouterManager
* @package Inhere\Route
*/
class RouterManager
{
public const DEFAULT_ROUTER = 'default';
/**
* @var self
*/
private static $_instance;
/**
* @var Router[]
* [
* 'main-site' => Object(Router),
* ... ...
* ]
*/
private $routers = [];
/**
* @var array Available router driver
*/
private $drivers = [
'default' => Router::class,
'cached' => CachedRouter::class,
'preMatch' => PreMatchRouter::class,
'server' => ServerRouter::class,
];
/**
* @var array[]
* [
* 'default' => 'main-site', // this is default router.
* 'main-site' => [
* 'driver' => 'default',
* 'conditions' => [
* 'domains' => 'abc.com',
* 'schemes' => ['https'],
* ],
* 'options' => [
* // some setting for router.
* 'name' => 'value'
* ],
* ],
* 'doc-site' => [
* 'driver' => 'cached',
* 'conditions' => [
* 'domains' => 'docs.abc.com',
* ],
* 'options' => [
* 'cacheFile' => '/path/to/routes-cache.php',
* 'cacheEnable' => true,
* ],
* ],
* ]
*/
private $configs;
/**
* @var array
* [
* 'main-site' => [
* 'domains' => 'abc.com',
* 'schemes' => ['https'],
* ],
* 'doc-site' => [
* 'domains' => 'docs.abc.com',
* 'schemes' => ['https'],
* ],
* 'th3-site' => [
* 'domains' => 'th3.abc.com',
* ],
* ]
*/
private $conditions = [];
/**
* @var array
*/
private $supportedConditions = [
'domains' => 'domain',
'schemes' => 'scheme',
];
/**
* @var array
*/
// private $onCollected = [
// 'cached' => 'completed',
// 'server' => 'flattenStatics'
// ];
/**
* @return RouterManager
*/
public static function instance(): RouterManager
{
return self::$_instance;
}
/**
* RouterManager constructor.
*
* @param array $configs
*/
public function __construct(array $configs = [])
{
self::$_instance = $this;
if ($configs) {
$this->configs($configs);
}
}
// match by $_SERVER info.
/**
* get router by condition
*
* @param array|string $condition
* array:
* [
* 'domain' => 'abc.com',
* 'scheme' => 'https',
* ]
* string:
* get by name. same of call getByName()
*
* @return Router |RouterInterface
* @throws InvalidArgumentException
*/
public function get($condition = null): Router
{
if (!$condition) {
return $this->getDefault();
}
// alias of getByName()
if (is_string($condition)) {
return $this->getByName($condition);
}
$useName = self::DEFAULT_ROUTER;
foreach ($this->conditions as $name => $cond) {
if ($this->compareArray($cond, $condition)) {
$useName = $name;
break;
}
}
return $this->getByName($useName);
}
/**
* @param array $define
* @param array $input
*
* @return bool
*/
protected function compareArray(array $define, array $input): bool
{
$match = true;
foreach ($this->supportedConditions as $def => $key) {
if (isset($define[$def], $input[$key])) {
$defValues = (array)$define[$def];
if (!in_array($input[$key], $defValues, true)) {
$match = false;
break;
}
}
}
return $match;
}
/**
* @param string $name
*
* @return Router
* @throws InvalidArgumentException
*/
public function getByName(string $name): Router
{
if (!isset($this->configs[$name])) {
throw new InvalidArgumentException("The named router '$name' does not exists!");
}
// if created
if (isset($this->routers[$name])) {
return $this->routers[$name];
}
// create
$config = $this->configs[$name];
if (is_string($config)) {
if (!isset($this->configs[$config])) {
throw new InvalidArgumentException("The reference config '$config' does not exists of the '$name'!");
}
$config = $this->configs[$config];
}
return ($this->routers[$name] = $this->createRouter($config, $name));
}
/**
* @return Router
* @throws InvalidArgumentException
*/
public function getDefault(): Router
{
return $this->getByName(self::DEFAULT_ROUTER);
}
/**
* @param array $config
* @param string $name
*
* @return Router
* @throws InvalidArgumentException
*/
private function createRouter(array $config, string $name = ''): Router
{
$driver = $config['driver'] ?? self::DEFAULT_ROUTER;
$options = $config['options'] ?? [];
if (!$class = $this->drivers[$driver] ?? null) {
throw new InvalidArgumentException("The router driver name '$driver' does not exists!");
}
if ($name && !isset($options['name'])) {
$options['name'] = $name;
}
return new $class($options);
}
/**
* @param string $name
* @param string $class
*/
public function setDriver(string $name, string $class): void
{
$this->drivers[$name] = $class;
}
/**
* @return Router[]
*/
public function getRouters(): array
{
return $this->routers;
}
/**
* @return array
*/
public function getDrivers(): array
{
return $this->drivers;
}
/**
* @return array[]
*/
public function getConfigs(): array
{
return $this->configs;
}
/**
* @param array[] $configs
*/
public function configs(array $configs): void
{
$this->configs = $configs;
foreach ($configs as $name => $config) {
if (isset($config['conditions'])) {
$this->conditions[$name] = $config['conditions'];
}
}
}
/**
* @return array
*/
public function getConditions(): array
{
return $this->conditions;
}
}