Skip to content

Commit a623a66

Browse files
huangdijiaawenganlimingxinleo
authored
Added Annotation Scene which use scene in FormRequest easily. (#4866)
Co-authored-by: awengan <goto8848@qq.com> Co-authored-by: 李铭昕 <715557344@qq.com>
1 parent f96db7a commit a623a66

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

src/Annotation/Scene.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Validation\Annotation;
13+
14+
use Attribute;
15+
use Hyperf\Di\Annotation\AbstractMultipleAnnotation;
16+
17+
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
18+
class Scene extends AbstractMultipleAnnotation
19+
{
20+
public function __construct(public string $scene, public ?string $argument = null)
21+
{
22+
}
23+
}

src/Middleware/ValidationMiddleware.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
use Closure;
1515
use FastRoute\Dispatcher;
1616
use Hyperf\Context\Context;
17+
use Hyperf\Di\Annotation\AnnotationCollector;
18+
use Hyperf\Di\Annotation\MultipleAnnotation;
1719
use Hyperf\Di\ReflectionManager;
1820
use Hyperf\HttpServer\Router\Dispatched;
1921
use Hyperf\Server\Exception\ServerException;
22+
use Hyperf\Validation\Annotation\Scene;
2023
use Hyperf\Validation\Contract\ValidatesWhenResolved;
24+
use Hyperf\Validation\Request\FormRequest;
2125
use Hyperf\Validation\UnauthorizedException;
2226
use Psr\Container\ContainerInterface;
2327
use Psr\Http\Message\ResponseInterface;
@@ -58,10 +62,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5862
if ($parameter->getType() === null) {
5963
continue;
6064
}
61-
$classname = $parameter->getType()->getName();
62-
if ($this->isImplementedValidatesWhenResolved($classname)) {
63-
/** @var \Hyperf\Validation\Contract\ValidatesWhenResolved $formRequest */
64-
$formRequest = $this->container->get($classname);
65+
$className = $parameter->getType()->getName();
66+
if ($this->isImplementedValidatesWhenResolved($className)) {
67+
/** @var ValidatesWhenResolved $formRequest */
68+
$formRequest = $this->container->get($className);
69+
if ($formRequest instanceof FormRequest) {
70+
$this->handleSceneAnnotation($formRequest, $requestHandler, $method, $parameter->getName());
71+
}
6572
$formRequest->validateResolved();
6673
}
6774
}
@@ -74,13 +81,35 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7481
return $handler->handle($request);
7582
}
7683

77-
public function isImplementedValidatesWhenResolved(string $classname): bool
84+
public function isImplementedValidatesWhenResolved(string $className): bool
7885
{
79-
if (! isset($this->implements[$classname]) && class_exists($classname)) {
80-
$implements = class_implements($classname);
81-
$this->implements[$classname] = in_array(ValidatesWhenResolved::class, $implements, true);
86+
if (! isset($this->implements[$className]) && class_exists($className)) {
87+
$implements = class_implements($className);
88+
$this->implements[$className] = in_array(ValidatesWhenResolved::class, $implements, true);
89+
}
90+
return $this->implements[$className] ?? false;
91+
}
92+
93+
protected function handleSceneAnnotation(FormRequest $request, string $class, string $method, string $argument): void
94+
{
95+
/** @var null|MultipleAnnotation $scene */
96+
$scene = AnnotationCollector::getClassMethodAnnotation($class, $method)[Scene::class] ?? null;
97+
if (! $scene) {
98+
return;
99+
}
100+
101+
$annotations = $scene->toAnnotations();
102+
if (empty($annotations)) {
103+
return;
104+
}
105+
106+
/** @var Scene $annotation */
107+
foreach ($annotations as $annotation) {
108+
if ($annotation->argument === null || $annotation->argument === $argument) {
109+
$request->scene($annotation->scene);
110+
return;
111+
}
82112
}
83-
return $this->implements[$classname] ?? false;
84113
}
85114

86115
/**

0 commit comments

Comments
 (0)