Skip to content

Commit

Permalink
增加Pattern注解
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Feb 16, 2023
1 parent 2058be8 commit 57e2e65
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/InteractsWithRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use think\annotation\route\Group;
use think\annotation\route\Middleware;
use think\annotation\route\Model;
use think\annotation\route\Pattern;
use think\annotation\route\Resource;
use think\annotation\route\Route;
use think\annotation\route\Validate;
Expand Down Expand Up @@ -101,9 +102,18 @@ protected function scanDir($dir, $options = [])

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

//变量规则
if (!empty($patternsAnn = $this->reader->getAnnotations($refMethod, Pattern::class))) {
foreach ($patternsAnn as $patternAnn) {
$rule->pattern([$patternAnn->name => $patternAnn->value]);
}
}

//中间件
if ($middlewareAnn = $this->reader->getAnnotation($refMethod, Middleware::class)) {
$rule->middleware($middlewareAnn->value);
if (!empty($middlewaresAnn = $this->reader->getAnnotations($refMethod, Middleware::class))) {
foreach ($middlewaresAnn as $middlewareAnn) {
$rule->middleware($middlewareAnn->value, ...$middlewareAnn->params);
}
}

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

$group->option($groupOptions);

if ($middlewareAnn = $this->reader->getAnnotation($refClass, Middleware::class)) {
$group->middleware($middlewareAnn->value);
//变量规则
if (!empty($patternsAnn = $this->reader->getAnnotations($refClass, Pattern::class))) {
foreach ($patternsAnn as $patternAnn) {
$group->pattern([$patternAnn->name => $patternAnn->value]);
}
}

//中间件
if (!empty($middlewaresAnn = $this->reader->getAnnotations($refClass, Middleware::class))) {
foreach ($middlewaresAnn as $middlewareAnn) {
$group->middleware($middlewareAnn->value, ...$middlewareAnn->params);
}
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/route/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Middleware
{
public function __construct(public $value)
public array $params;

public function __construct(public $value, ...$params)
{
$this->params = $params;
}
}
13 changes: 13 additions & 0 deletions src/route/Pattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace think\annotation\route;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class Pattern
{
public function __construct(public string $name, public string $value)
{
}
}

0 comments on commit 57e2e65

Please sign in to comment.