Skip to content

Commit a46d63d

Browse files
committed
class import fix. add test for RouterManager
1 parent f357a89 commit a46d63d

File tree

9 files changed

+164
-40
lines changed

9 files changed

+164
-40
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@
1313
- 支持自动匹配路由到控制器就像 Yii 一样, 请参看配置项 `autoRoute` (不推荐)
1414
- 压测对比数据请看[路由测试](#ab-test)
1515

16-
多个版本:
16+
**多个版本:**
1717

1818
> 不同的版本有稍微的区别以适应不同的场景
1919
2020
- `ORouter` 基础版本,也是后几个版本的基础类。
21-
- `SRouter` 静态类版本。 `ORouter` 的简单包装,通过静态方法使用(方便小应用快速使用)
21+
- `SRouter` 静态类版本。`ORouter` 的简单包装,通过静态方法使用(方便小应用快速使用)
2222
- `CachedRouter` 继承自`ORouter`,支持路由缓存的版本. 适合fpm使用(有缓存将会省去每次的路由收集和解析消耗)
2323
- `PreMatchRouter` 继承自`ORouter`,预匹配路由器。当应用的静态路由较多时,将拥有更快的匹配速度
2424
- fpm 应用中,实际上我们在收集路由之前,已经知道了路由path和请求动作METHOD
2525
- `ServerRouter` 继承自`ORouter`,服务器路由。内置支持动态路由临时缓存. 适合swoole等常驻内存应用使用
2626
- 最近请求过的动态路由将会缓存为一个静态路由信息,下次相同路由将会直接匹配命中
2727

28-
内置调度器:
28+
**内置调度器:**
2929

3030
- 支持事件: `found` `notFound` `execStart` `execEnd` `execError`. 当触发事件时你可以做一些事情(比如记录日志等)
3131
- 支持动态获取`action`名。支持设置方法执行器(`actionExecutor`),通过方法执行器来自定义调用真实请求方法.
3232
- 支持通过方法 `$router->dispatch($path, $method)` 手动调度一个路由
3333
- 你即使不配置任何东西, 它也能很好的工作
3434

35+
**路由器管理**
36+
37+
`RouterManager` 当需要在一个项目里处理多个域名下的请求时,方便的根据不同域名配置多个路由器
38+
3539
**[EN README](README_en.md)**
3640

3741
## 项目地址
@@ -61,8 +65,8 @@ composer require inhere/sroute
6165

6266
```bash
6367
git clone https://github.com/inhere/php-srouter.git // github
64-
git clone https://gitee.com/inhere/php-srouter.git // git@osc
6568
```
69+
6670
<a name="ab-test"></a>
6771
## 压测
6872

src/Base/AbstractRouter.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
*/
2525
abstract class AbstractRouter implements RouterInterface
2626
{
27+
/**
28+
* @var string
29+
*/
30+
private $name = '';
31+
2732
/**
2833
* some available patterns regex
2934
* $router->get('/user/{id}', 'handler');
@@ -208,7 +213,8 @@ public function setConfig(array $config)
208213
throw new \LogicException('Routing has been added, and configuration is not allowed!');
209214
}
210215

211-
static $props = [
216+
$props = [
217+
'name' => 1,
212218
'ignoreLastSlash' => 1,
213219
'tmpCacheNumber' => 1,
214220
'notAllowedAsNotFound' => 1,
@@ -267,7 +273,7 @@ public function __call($method, array $args)
267273
* @throws \LogicException
268274
* @throws \InvalidArgumentException
269275
*/
270-
public function rest($prefix, $controllerClass, array $map = [], array $opts = []): AbstractRouter
276+
public function rest(string $prefix, string $controllerClass, array $map = [], array $opts = []): AbstractRouter
271277
{
272278
$map = array_merge([
273279
'index' => ['GET'],
@@ -324,7 +330,7 @@ public function rest($prefix, $controllerClass, array $map = [], array $opts = [
324330
* @throws \LogicException
325331
* @throws \InvalidArgumentException
326332
*/
327-
public function ctrl($prefix, $controllerClass, array $map = [], array $opts = []): AbstractRouter
333+
public function ctrl(string $prefix, string $controllerClass, array $map = [], array $opts = []): AbstractRouter
328334
{
329335
foreach ($map as $action => $method) {
330336
if (!$method || !\is_string($action)) {
@@ -404,7 +410,7 @@ public function validateArguments($methods, $handler): array
404410
* @param string $route
405411
* @return bool
406412
*/
407-
public static function isStaticRoute($route): bool
413+
public static function isStaticRoute(string $route): bool
408414
{
409415
return strpos($route, '{') === false && strpos($route, '[') === false;
410416
}
@@ -414,7 +420,7 @@ public static function isStaticRoute($route): bool
414420
* @param bool $ignoreLastSlash
415421
* @return string
416422
*/
417-
protected function formatUriPath($path, $ignoreLastSlash): string
423+
protected function formatUriPath(string $path, $ignoreLastSlash): string
418424
{
419425
// clear '//', '///' => '/'
420426
if (false !== strpos($path, '//')) {
@@ -691,6 +697,22 @@ public static function getSupportedMethods(): array
691697
return self::ALLOWED_METHODS;
692698
}
693699

700+
/**
701+
* @return string
702+
*/
703+
public function getName(): string
704+
{
705+
return $this->name;
706+
}
707+
708+
/**
709+
* @param string $name
710+
*/
711+
public function setName(string $name)
712+
{
713+
$this->name = $name;
714+
}
715+
694716
/**
695717
* @param array $staticRoutes
696718
*/

src/Dispatcher/Dispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Inhere\Route\Dispatcher;
1010

11-
use Inhere\Route\RouterInterface;
11+
use Inhere\Route\Base\RouterInterface;
1212

1313
/**
1414
* Class Dispatcher

src/Dispatcher/DispatcherInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Inhere\Route\Dispatcher;
1010

11-
use Inhere\Route\RouterInterface;
11+
use Inhere\Route\Base\RouterInterface;
1212

1313
/**
1414
* Interface DispatcherInterface

src/Dispatcher/SimpleDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Inhere\Route\Dispatcher;
1010

1111
use Inhere\Route\ORouter;
12-
use Inhere\Route\RouterInterface;
12+
use Inhere\Route\Base\RouterInterface;
1313

1414
/**
1515
* Class SimpleDispatcher

src/PreMatchRouter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public function __construct(array $config = [], string $path = null, string $met
5757
*/
5858
public function setRequest(string $path = null, string $method = null)
5959
{
60-
$path = $path ?: $_SERVER['REQUEST_URI'];
60+
if (!$path) {
61+
$path = $_SERVER['REQUEST_URI'] ?? '';
62+
}
6163

6264
if (strpos($path, '?')) {
6365
$path = parse_url($path, PHP_URL_PATH);

src/RouterManager.php

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class RouterManager
4747
* @var array[]
4848
* [
4949
* 'default' => 'main-site', // this is default router.
50-
*
5150
* 'main-site' => [
5251
* 'driver' => 'default',
5352
* 'conditions' => [
54-
* 'domain' => 'domain.com',
53+
* 'domains' => 'abc.com',
54+
* 'schemes' => ['https'],
5555
* ],
5656
* 'options' => [
5757
* // some setting for router.
@@ -61,7 +61,7 @@ class RouterManager
6161
* 'doc-site' => [
6262
* 'driver' => 'cached',
6363
* 'conditions' => [
64-
* 'domain' => 'docs.domain.com',
64+
* 'domains' => 'docs.abc.com',
6565
* ],
6666
* 'options' => [
6767
* 'cacheFile' => '/path/to/routes-cache.php',
@@ -75,12 +75,29 @@ class RouterManager
7575
/**
7676
* @var array
7777
* [
78-
* 'condition ID' => 'name',
79-
* 'condition ID1' => 'main-site'
78+
* 'main-site' => [
79+
* 'domains' => 'abc.com',
80+
* 'schemes' => ['https'],
81+
* ],
82+
* 'doc-site' => [
83+
* 'domains' => 'docs.abc.com',
84+
* 'schemes' => ['https'],
85+
* ],
86+
* 'th3-site' => [
87+
* 'domains' => 'th3.abc.com',
88+
* ],
8089
* ]
8190
*/
8291
private $conditions = [];
8392

93+
/**
94+
* @var array
95+
*/
96+
private $supportedConditions = [
97+
'domains' => 'domain',
98+
'schemes' => 'scheme',
99+
];
100+
84101
/**
85102
* @var array
86103
*/
@@ -112,23 +129,61 @@ public function __construct(array $configs = [])
112129

113130
/**
114131
* get router by condition
115-
* @param array $conditions
132+
* @param array|string $condition
133+
* array:
116134
* [
117-
* 'domain' => 'domain.com'
135+
* 'domain' => 'abc.com',
136+
* 'scheme' => 'https',
118137
* ]
138+
* string:
139+
* get by name. same of call getByName()
119140
* @return AbstractRouter|RouterInterface
120141
* @throws \InvalidArgumentException
121142
*/
122-
public function get($conditions = null): AbstractRouter
143+
public function get($condition = null): AbstractRouter
123144
{
124-
if (!$conditions) {
145+
if (!$condition) {
125146
return $this->getDefault();
126147
}
127148

128-
$key = $this->genConditionID($conditions);
129-
$name = $this->conditions[$key] ?? self::DEFAULT_ROUTER;
149+
// alias of getByName()
150+
if (\is_string($condition)) {
151+
return $this->getByName($condition);
152+
}
153+
154+
$useName = self::DEFAULT_ROUTER;
155+
156+
foreach ($this->conditions as $name => $cond) {
157+
if ($this->compareArray($cond, $condition)) {
158+
$useName = $name;
159+
break;
160+
}
161+
}
130162

131-
return $this->getByName($name);
163+
return $this->getByName($useName);
164+
}
165+
166+
/**
167+
* @param array $define
168+
* @param array $input
169+
* @return bool
170+
*/
171+
protected function compareArray(array $define, array $input): bool
172+
{
173+
$match = true;
174+
175+
foreach ($this->supportedConditions as $def => $key) {
176+
if (isset($define[$def], $input[$key])) {
177+
$defValues = (array)$define[$def];
178+
179+
if (!\in_array($input[$key], $defValues, true)) {
180+
$match = false;
181+
break;
182+
}
183+
}
184+
}
185+
186+
return $match;
132187
}
133188

134189
/**
@@ -158,7 +213,7 @@ public function getByName(string $name): AbstractRouter
158213
$config = $this->configs[$config];
159214
}
160215

161-
return ($this->routers[$name] = $this->createRouter($config));
216+
return ($this->routers[$name] = $this->createRouter($config, $name));
162217
}
163218

164219
/**
@@ -169,21 +224,13 @@ public function getDefault(): AbstractRouter
169224
return $this->getByName(self::DEFAULT_ROUTER);
170225
}
171226

172-
/**
173-
* @param string|array $conditions
174-
* @return string
175-
*/
176-
private function genConditionID($conditions): string
177-
{
178-
return \md5(\is_array($conditions) ? \json_encode($conditions) : $conditions);
179-
}
180-
181227
/**
182228
* @param array $config
229+
* @param string $name
183230
* @return AbstractRouter
184231
* @throws \InvalidArgumentException
185232
*/
186-
private function createRouter(array $config): AbstractRouter
233+
private function createRouter(array $config, string $name = ''): AbstractRouter
187234
{
188235
$driver = $config['driver'] ?? self::DEFAULT_ROUTER;
189236
$options = $config['options'] ?? [];
@@ -192,6 +239,10 @@ private function createRouter(array $config): AbstractRouter
192239
throw new \InvalidArgumentException("The router driver name '$driver' does not exists!");
193240
}
194241

242+
if ($name && !isset($options['name'])) {
243+
$options['name'] = $name;
244+
}
245+
195246
return new $class($options);
196247
}
197248

@@ -237,8 +288,7 @@ public function setConfigs(array $configs)
237288

238289
foreach ($configs as $name => $config) {
239290
if (isset($config['conditions'])) {
240-
$key = $this->genConditionID($config['conditions']);
241-
$this->conditions[$key] = $name;
291+
$this->conditions[$name] = $config['conditions'];
242292
}
243293
}
244294
}

test/PreMatchRouterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Inhere\Route\Test;
1010

1111
use Inhere\Route\PreMatchRouter;
12-
use Inhere\Route\RouterInterface;
12+
use Inhere\Route\Base\RouterInterface;
1313
use PHPUnit\Framework\TestCase;
1414

1515
/**

0 commit comments

Comments
 (0)