14
14
use Closure ;
15
15
use FastRoute \Dispatcher ;
16
16
use Hyperf \Context \Context ;
17
+ use Hyperf \Di \Annotation \AnnotationCollector ;
18
+ use Hyperf \Di \Annotation \MultipleAnnotation ;
17
19
use Hyperf \Di \ReflectionManager ;
18
20
use Hyperf \HttpServer \Router \Dispatched ;
19
21
use Hyperf \Server \Exception \ServerException ;
22
+ use Hyperf \Validation \Annotation \Scene ;
20
23
use Hyperf \Validation \Contract \ValidatesWhenResolved ;
24
+ use Hyperf \Validation \Request \FormRequest ;
21
25
use Hyperf \Validation \UnauthorizedException ;
22
26
use Psr \Container \ContainerInterface ;
23
27
use Psr \Http \Message \ResponseInterface ;
@@ -58,10 +62,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
58
62
if ($ parameter ->getType () === null ) {
59
63
continue ;
60
64
}
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
+ }
65
72
$ formRequest ->validateResolved ();
66
73
}
67
74
}
@@ -74,13 +81,35 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
74
81
return $ handler ->handle ($ request );
75
82
}
76
83
77
- public function isImplementedValidatesWhenResolved (string $ classname ): bool
84
+ public function isImplementedValidatesWhenResolved (string $ className ): bool
78
85
{
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
+ }
82
112
}
83
- return $ this ->implements [$ classname ] ?? false ;
84
113
}
85
114
86
115
/**
0 commit comments