Skip to content

Commit

Permalink
修复在配置文件中启用 autoEndSlash 后,路由重复检测误报 (#707)
Browse files Browse the repository at this point in the history
* 修复在配置文件中启用 autoEndSlash 后,路由重复检测误报

* 修复
  • Loading branch information
Yurunsoft authored Jul 17, 2024
1 parent fb2f7fd commit 0c21a40
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Components/swoole/tests/unit/HttpServer/config/beans.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
'tasks' => [
],
],
'HttpRoute' => [
'autoEndSlash' => true,
],
];
3 changes: 1 addition & 2 deletions src/Server/Http/Listener/HttpRouteInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ protected function parseAnnotations(): void
$context['server'] = $server;
/** @var HttpRoute $route */
$route = $server->getBean('HttpRoute');
$autoEndSlash = $route->getAutoEndSlash();
foreach ($controllerParser->getByServer($name) as $className => $classItem)
{
/** @var \Imi\Server\Http\Route\Annotation\Controller $classAnnotation */
Expand Down Expand Up @@ -173,7 +172,7 @@ protected function parseAnnotations(): void
'extractData' => $extractData,
];
$route->addRuleAnnotation($routeItem, $routeCallable, $options);
if (($routeItem->autoEndSlash || ($autoEndSlash && null === $routeItem->autoEndSlash)) && !str_ends_with($routeItem->url, '/'))
if ($routeItem->autoEndSlash && !str_ends_with($routeItem->url, '/'))
{
$routeItem = clone $routeItem;
$routeItem->url .= '/';
Expand Down
23 changes: 16 additions & 7 deletions src/Server/Http/Route/HttpRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,33 @@ public function addRule(string $path, $callable, RouteAnnotation $annotation = n
{
$routeItem->wsConfig = $options['wsConfig'];
}
$data = [
'routeItem' => $routeItem,
];
$ignoreCase = $annotation->ignoreCase ?? $this->ignoreCase;
if (!$ignoreCase && self::isStaticPath($path))
{
$this->router->addStatic($path, $callable, $annotation->method, $ignoreCase, $checkCallables, $data);
$this->router->addStatic($path, $callable, $annotation->method, $ignoreCase, $checkCallables, [
'routeItem' => $routeItem,
]);
if ($this->autoEndSlash && !str_ends_with($path, '/'))
{
$this->router->addStatic($path . '/', $callable, $annotation->method, $ignoreCase, $checkCallables, $data);
$routeItem = clone $routeItem;
$routeItem->annotation->url .= '/';
$this->router->addStatic($path . '/', $callable, $annotation->method, $ignoreCase, $checkCallables, [
'routeItem' => $routeItem,
]);
}
}
else
{
$this->router->add($path, $callable, $annotation->method, $ignoreCase, $checkCallables, $data);
$this->router->add($path, $callable, $annotation->method, $ignoreCase, $checkCallables, [
'routeItem' => $routeItem,
]);
if ($this->autoEndSlash && !str_ends_with($path, '/'))
{
$this->router->add($path . '/', $callable, $annotation->method, $ignoreCase, $checkCallables, $data);
$routeItem = clone $routeItem;
$routeItem->annotation->url .= '/';
$this->router->add($path . '/', $callable, $annotation->method, $ignoreCase, $checkCallables, [
'routeItem' => $routeItem,
]);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Server/Http/Route/RouteItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public function __construct(Route $annotation, callable $callable, View $view, ?
$this->viewOption = $viewOption;
$this->options = $options;
}

public function __clone()
{
if ($this->annotation)
{
$this->annotation = clone $this->annotation;
}
}
}

0 comments on commit 0c21a40

Please sign in to comment.