12
12
use Psr \Http \Server \RequestHandlerInterface ;
13
13
use ReflectionClass ;
14
14
use ReflectionFunction ;
15
+ use ReflectionParameter ;
15
16
use Yiisoft \Definitions \ArrayDefinition ;
16
17
use Yiisoft \Definitions \Exception \InvalidConfigException ;
17
18
use Yiisoft \Definitions \Helpers \DefinitionValidator ;
@@ -141,13 +142,17 @@ private function createCallableWrapper(callable $callback): MiddlewareInterface
141
142
{
142
143
return new class ($ callback , $ this ->container , $ this ->parametersResolver ) implements MiddlewareInterface {
143
144
private $ callback ;
145
+ /** @var ReflectionParameter[] */
146
+ private array $ callableParameters ;
144
147
145
148
public function __construct (
146
149
callable $ callback ,
147
150
private ContainerInterface $ container ,
148
151
private ?ParametersResolverInterface $ parametersResolver
149
152
) {
150
153
$ this ->callback = $ callback ;
154
+ $ callback = Closure::fromCallable ($ callback );
155
+ $ this ->callableParameters = (new ReflectionFunction ($ callback ))->getParameters ();
151
156
}
152
157
153
158
public function process (
@@ -158,7 +163,7 @@ public function process(
158
163
if ($ this ->parametersResolver !== null ) {
159
164
$ parameters = array_merge (
160
165
$ parameters ,
161
- $ this ->parametersResolver ->resolve ($ this ->getCallableParameters () , $ request )
166
+ $ this ->parametersResolver ->resolve ($ this ->callableParameters , $ request )
162
167
);
163
168
}
164
169
/** @var MiddlewareInterface|mixed|ResponseInterface $response */
@@ -176,16 +181,6 @@ public function __debugInfo(): array
176
181
{
177
182
return ['callback ' => $ this ->callback ];
178
183
}
179
-
180
- /**
181
- * @return \ReflectionParameter[]
182
- */
183
- private function getCallableParameters (): array
184
- {
185
- $ callback = Closure::fromCallable ($ this ->callback );
186
-
187
- return (new ReflectionFunction ($ callback ))->getParameters ();
188
- }
189
184
};
190
185
}
191
186
@@ -196,6 +191,9 @@ private function getCallableParameters(): array
196
191
private function createActionWrapper (string $ class , string $ method ): MiddlewareInterface
197
192
{
198
193
return new class ($ this ->container , $ this ->parametersResolver , $ class , $ method ) implements MiddlewareInterface {
194
+ /** @var ReflectionParameter[] */
195
+ private array $ actionParameters ;
196
+
199
197
public function __construct (
200
198
private ContainerInterface $ container ,
201
199
private ?ParametersResolverInterface $ parametersResolver ,
@@ -204,6 +202,9 @@ public function __construct(
204
202
/** @var non-empty-string */
205
203
private string $ method
206
204
) {
205
+ $ this ->actionParameters = (new ReflectionClass ($ this ->class ))
206
+ ->getMethod ($ this ->method )
207
+ ->getParameters ();
207
208
}
208
209
209
210
public function process (
@@ -216,7 +217,7 @@ public function process(
216
217
if ($ this ->parametersResolver !== null ) {
217
218
$ parameters = array_merge (
218
219
$ parameters ,
219
- $ this ->parametersResolver ->resolve ($ this ->getActionParameters () , $ request )
220
+ $ this ->parametersResolver ->resolve ($ this ->actionParameters , $ request )
220
221
);
221
222
}
222
223
@@ -229,16 +230,6 @@ public function process(
229
230
throw new InvalidMiddlewareDefinitionException ([$ this ->class , $ this ->method ]);
230
231
}
231
232
232
- /**
233
- * @throws \ReflectionException
234
- *
235
- * @return \ReflectionParameter[]
236
- */
237
- private function getActionParameters (): array
238
- {
239
- return (new ReflectionClass ($ this ->class ))->getMethod ($ this ->method )->getParameters ();
240
- }
241
-
242
233
public function __debugInfo ()
243
234
{
244
235
return [
0 commit comments