Skip to content

Commit 1fe9b49

Browse files
random-ragevjik
andauthored
Optimize MiddlewareFactory performance (#75)
* Optimize MiddlewareFactory performance * Add line to changelog (#74) * Update CHANGELOG.md --------- Co-authored-by: Sergei Predvoditelev <[email protected]>
1 parent de7c204 commit 1fe9b49

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 5.0.1 under development
44

5-
- no changes in this release.
5+
- Enh #75: Optimize `MiddlewareFactory` performance (@random-rage)
66

77
## 5.0.0 January 09, 2023
88

src/MiddlewareFactory.php

+13-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Psr\Http\Server\RequestHandlerInterface;
1313
use ReflectionClass;
1414
use ReflectionFunction;
15+
use ReflectionParameter;
1516
use Yiisoft\Definitions\ArrayDefinition;
1617
use Yiisoft\Definitions\Exception\InvalidConfigException;
1718
use Yiisoft\Definitions\Helpers\DefinitionValidator;
@@ -141,13 +142,17 @@ private function createCallableWrapper(callable $callback): MiddlewareInterface
141142
{
142143
return new class ($callback, $this->container, $this->parametersResolver) implements MiddlewareInterface {
143144
private $callback;
145+
/** @var ReflectionParameter[] */
146+
private array $callableParameters;
144147

145148
public function __construct(
146149
callable $callback,
147150
private ContainerInterface $container,
148151
private ?ParametersResolverInterface $parametersResolver
149152
) {
150153
$this->callback = $callback;
154+
$callback = Closure::fromCallable($callback);
155+
$this->callableParameters = (new ReflectionFunction($callback))->getParameters();
151156
}
152157

153158
public function process(
@@ -158,7 +163,7 @@ public function process(
158163
if ($this->parametersResolver !== null) {
159164
$parameters = array_merge(
160165
$parameters,
161-
$this->parametersResolver->resolve($this->getCallableParameters(), $request)
166+
$this->parametersResolver->resolve($this->callableParameters, $request)
162167
);
163168
}
164169
/** @var MiddlewareInterface|mixed|ResponseInterface $response */
@@ -176,16 +181,6 @@ public function __debugInfo(): array
176181
{
177182
return ['callback' => $this->callback];
178183
}
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-
}
189184
};
190185
}
191186

@@ -196,6 +191,9 @@ private function getCallableParameters(): array
196191
private function createActionWrapper(string $class, string $method): MiddlewareInterface
197192
{
198193
return new class ($this->container, $this->parametersResolver, $class, $method) implements MiddlewareInterface {
194+
/** @var ReflectionParameter[] */
195+
private array $actionParameters;
196+
199197
public function __construct(
200198
private ContainerInterface $container,
201199
private ?ParametersResolverInterface $parametersResolver,
@@ -204,6 +202,9 @@ public function __construct(
204202
/** @var non-empty-string */
205203
private string $method
206204
) {
205+
$this->actionParameters = (new ReflectionClass($this->class))
206+
->getMethod($this->method)
207+
->getParameters();
207208
}
208209

209210
public function process(
@@ -216,7 +217,7 @@ public function process(
216217
if ($this->parametersResolver !== null) {
217218
$parameters = array_merge(
218219
$parameters,
219-
$this->parametersResolver->resolve($this->getActionParameters(), $request)
220+
$this->parametersResolver->resolve($this->actionParameters, $request)
220221
);
221222
}
222223

@@ -229,16 +230,6 @@ public function process(
229230
throw new InvalidMiddlewareDefinitionException([$this->class, $this->method]);
230231
}
231232

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-
242233
public function __debugInfo()
243234
{
244235
return [

0 commit comments

Comments
 (0)