Skip to content

Commit 57e2e65

Browse files
committed
增加Pattern注解
1 parent 2058be8 commit 57e2e65

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/InteractsWithRoute.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use think\annotation\route\Group;
99
use think\annotation\route\Middleware;
1010
use think\annotation\route\Model;
11+
use think\annotation\route\Pattern;
1112
use think\annotation\route\Resource;
1213
use think\annotation\route\Route;
1314
use think\annotation\route\Validate;
@@ -101,9 +102,18 @@ protected function scanDir($dir, $options = [])
101102

102103
$rule->option($routeAnn->options);
103104

105+
//变量规则
106+
if (!empty($patternsAnn = $this->reader->getAnnotations($refMethod, Pattern::class))) {
107+
foreach ($patternsAnn as $patternAnn) {
108+
$rule->pattern([$patternAnn->name => $patternAnn->value]);
109+
}
110+
}
111+
104112
//中间件
105-
if ($middlewareAnn = $this->reader->getAnnotation($refMethod, Middleware::class)) {
106-
$rule->middleware($middlewareAnn->value);
113+
if (!empty($middlewaresAnn = $this->reader->getAnnotations($refMethod, Middleware::class))) {
114+
foreach ($middlewaresAnn as $middlewareAnn) {
115+
$rule->middleware($middlewareAnn->value, ...$middlewareAnn->params);
116+
}
107117
}
108118

109119
//绑定模型,支持多个
@@ -143,8 +153,18 @@ protected function scanDir($dir, $options = [])
143153

144154
$group->option($groupOptions);
145155

146-
if ($middlewareAnn = $this->reader->getAnnotation($refClass, Middleware::class)) {
147-
$group->middleware($middlewareAnn->value);
156+
//变量规则
157+
if (!empty($patternsAnn = $this->reader->getAnnotations($refClass, Pattern::class))) {
158+
foreach ($patternsAnn as $patternAnn) {
159+
$group->pattern([$patternAnn->name => $patternAnn->value]);
160+
}
161+
}
162+
163+
//中间件
164+
if (!empty($middlewaresAnn = $this->reader->getAnnotations($refClass, Middleware::class))) {
165+
foreach ($middlewaresAnn as $middlewareAnn) {
166+
$group->middleware($middlewareAnn->value, ...$middlewareAnn->params);
167+
}
148168
}
149169
};
150170
}

src/route/Middleware.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
88
final class Middleware
99
{
10-
public function __construct(public $value)
10+
public array $params;
11+
12+
public function __construct(public $value, ...$params)
1113
{
14+
$this->params = $params;
1215
}
1316
}

src/route/Pattern.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace think\annotation\route;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
8+
final class Pattern
9+
{
10+
public function __construct(public string $name, public string $value)
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)