Skip to content

Commit 9b2d641

Browse files
committed
调整路由map的参数顺序,添加load加载路由配置方法
1 parent 38b40a2 commit 9b2d641

File tree

3 files changed

+75
-28
lines changed

3 files changed

+75
-28
lines changed

src/Soli/Router.php

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,68 @@ class Router extends Component
2626
*/
2727
protected $dispatcher;
2828

29+
/**
30+
* Load routes config.
31+
*/
32+
public function load(array $routesConfig)
33+
{
34+
foreach ($routesConfig as $route) {
35+
$pattern = $route[0];
36+
$handler = $route[1] ?? null;
37+
$methods = $route[2] ?? '*';
38+
$this->map($pattern, $handler, $methods);
39+
}
40+
return $this->routes;
41+
}
42+
2943
/**
3044
* Add route.
3145
*
32-
* @param string|string[] $methods 'GET' or ['GET', 'POST']
3346
* @param string $pattern
34-
* @param array $handler
47+
* @param array|string $handler
48+
* @param string|string[] $methods 'GET' or ['GET', 'POST']
3549
*/
36-
public function map($methods, $pattern, array $handler)
50+
public function map($pattern, $handler = null, $methods = '*')
3751
{
38-
$this->routes[] = [
39-
'methods' => $methods,
40-
'pattern' => $pattern,
52+
$this->routes[$pattern] = [
4153
'handler' => $handler,
54+
'methods' => $methods,
4255
];
4356
}
4457

4558
public function get($pattern, $handler)
4659
{
47-
$this->map('GET', $pattern, $handler);
60+
$this->map($pattern, $handler, 'GET');
4861
}
4962

5063
public function post($pattern, $handler)
5164
{
52-
$this->map('POST', $pattern, $handler);
65+
$this->map($pattern, $handler, 'POST');
5366
}
5467

5568
public function put($pattern, $handler)
5669
{
57-
$this->map('PUT', $pattern, $handler);
70+
$this->map($pattern, $handler, 'PUT');
5871
}
5972

6073
public function delete($pattern, $handler)
6174
{
62-
$this->map('DELETE', $pattern, $handler);
75+
$this->map($pattern, $handler, 'DELETE');
6376
}
6477

6578
public function head($pattern, $handler)
6679
{
67-
$this->map('HEAD', $pattern, $handler);
80+
$this->map($pattern, $handler, 'HEAD');
6881
}
6982

7083
public function trace($pattern, $handler)
7184
{
72-
$this->map('TRACE', $pattern, $handler);
85+
$this->map($pattern, $handler, 'TRACE');
7386
}
7487

7588
public function options($pattern, $handler)
7689
{
77-
$this->map('OPTIONS', $pattern, $handler);
90+
$this->map($pattern, $handler, 'OPTIONS');
7891
}
7992

8093
public function setDefaults(array $defaults)
@@ -136,8 +149,12 @@ public function handle($uri = null)
136149
protected function createDispatcher()
137150
{
138151
return $this->dispatcher ?: \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
139-
foreach ($this->routes as $route) {
140-
$r->addRoute($route['methods'], $route['pattern'], $route['handler']);
152+
// 默认路由
153+
if (!isset($this->routes['/'])) {
154+
$this->map('/', [], '*');
155+
}
156+
foreach ($this->routes as $pattern => $route) {
157+
$r->addRoute($route['methods'], $pattern, $route['handler']);
141158
}
142159
});
143160
}
@@ -170,27 +187,57 @@ protected function handleDispatcherResponse($routeInfo)
170187
*/
171188
protected function handleFoundRoute($routeInfo)
172189
{
190+
$routeInfo = $this->formatRouteInfo($routeInfo);
191+
173192
$handler = $routeInfo[1];
174193
$params = $routeInfo[2];
175194

176-
$handler = array_merge($params, $handler);
177-
178195
// 存储控制器、方法及参数
179-
if (isset($handler['namespace'])) {
196+
if ($handler['namespace']) {
180197
$this->namespaceName = $handler['namespace'];
181198
}
182199

183-
if (isset($handler['controller'])) {
200+
if ($handler['controller']) {
184201
$this->controllerName = $handler['controller'];
185202
}
186203

187-
if (isset($handler['action'])) {
204+
if ($handler['action']) {
188205
$this->actionName = $handler['action'];
189206
}
190207

191208
$this->params = $params;
192209
}
193210

211+
protected function formatRouteInfo($routeInfo)
212+
{
213+
$handler = $routeInfo[1];
214+
$params = $routeInfo[2];
215+
216+
// $router->map('PUT', '/users/{id}', 'user::update');
217+
// $router->map('PUT', '/users/{id}', 'App\Controllers\User::update');
218+
if (is_string($handler)) {
219+
$tmp = explode('::', $handler);
220+
$handler = [
221+
'controller' => $tmp[0] ?? null,
222+
'action' => $tmp[1] ?? null,
223+
];
224+
}
225+
226+
$routeInfo[1] = [
227+
'namespace' => $handler['namespace'] ?? $params['namespace'] ?? null,
228+
'controller' => $handler['controller'] ?? $params['controller'] ?? null,
229+
'action' => $handler['action'] ?? $params['action'] ?? null,
230+
];
231+
232+
unset($params['namespace']);
233+
unset($params['controller']);
234+
unset($params['action']);
235+
236+
$routeInfo[2] = $params;
237+
238+
return $routeInfo;
239+
}
240+
194241
protected function getRewriteUri()
195242
{
196243
$uri = $_GET['_uri'] ?? rawurldecode($_SERVER['REQUEST_URI']);

tests/ApplicationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ protected function createApplication()
2929
'params' => []
3030
]);
3131

32-
$router->map('TEST', 'index/responseInstance', ['action' => 'responseInstance']);
33-
$router->map('TEST', 'index/hello/{name}', ['action' => 'hello']);
34-
$router->map('TEST', 'index/responseFalse', ['action' => 'responseFalse']);
32+
$router->map('index/responseInstance', ['action' => 'responseInstance'], 'TEST');
33+
$router->map('index/hello/{name}', ['action' => 'hello'], 'TEST');
34+
$router->map('index/responseFalse', ['action' => 'responseFalse'], 'TEST');
3535

3636
return $router;
3737
});

tests/RouterTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ protected function initRouter()
3838
{
3939
$this->router = $this->container->get(Router::class);
4040

41-
$this->router->map('GET', '/hello/{name}', $this->routeData());
41+
$this->router->map('/hello/{name}', $this->routeHandler(), 'GET');
4242
}
4343

44-
protected function routeData()
44+
protected function routeHandler()
4545
{
4646
return [
4747
'namespace' => "Soli\\Tests\\Handlers\\",
@@ -63,9 +63,9 @@ public function testGetters()
6363
$this->initRouter();
6464
$this->router->handle();
6565

66-
$this->assertEquals($this->routeData()['namespace'], $this->router->getNamespaceName());
67-
$this->assertEquals($this->routeData()['controller'], $this->router->getControllerName());
68-
$this->assertEquals($this->routeData()['action'], $this->router->getActionName());
66+
$this->assertEquals($this->routeHandler()['namespace'], $this->router->getNamespaceName());
67+
$this->assertEquals($this->routeHandler()['controller'], $this->router->getControllerName());
68+
$this->assertEquals($this->routeHandler()['action'], $this->router->getActionName());
6969

7070
$this->assertEquals('soliphp', $this->router->getParams()['name']);
7171
}

0 commit comments

Comments
 (0)