| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Yiisoft\Middleware\Dispatcher; |
||
| 6 | |||
| 7 | use Closure; |
||
| 8 | use Psr\EventDispatcher\EventDispatcherInterface; |
||
| 9 | use Psr\Http\Message\ResponseInterface; |
||
| 10 | use Psr\Http\Message\ServerRequestInterface; |
||
| 11 | use Psr\Http\Server\MiddlewareInterface; |
||
| 12 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 13 | |||
| 14 | final class MiddlewareDispatcher |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Contains a middleware pipeline handler. |
||
| 18 | * |
||
| 19 | * @var MiddlewareStack|null The middleware stack. |
||
| 20 | */ |
||
| 21 | private ?MiddlewareStack $stack = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array[]|callable[]|string[] |
||
| 25 | */ |
||
| 26 | private array $middlewareDefinitions = []; |
||
| 27 | |||
| 28 | 10 | public function __construct( |
|
| 29 | private MiddlewareFactory $middlewareFactory, |
||
| 30 | private ?EventDispatcherInterface $eventDispatcher = null |
||
| 31 | ) { |
||
| 32 | 10 | } |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Dispatch request through middleware to get response. |
||
| 36 | * |
||
| 37 | * @param ServerRequestInterface $request Request to pass to middleware. |
||
| 38 | * @param RequestHandlerInterface $fallbackHandler Handler to use in case no middleware produced response. |
||
| 39 | */ |
||
| 40 | 7 | public function dispatch( |
|
| 41 | ServerRequestInterface $request, |
||
| 42 | RequestHandlerInterface $fallbackHandler |
||
| 43 | ): ResponseInterface { |
||
| 44 | 7 | if ($this->stack === null) { |
|
| 45 | 7 | $this->stack = new MiddlewareStack($this->buildMiddlewares(), $fallbackHandler, $this->eventDispatcher); |
|
| 46 | } |
||
| 47 | |||
| 48 | 7 | return $this->stack->handle($request); |
|
|
0 ignored issues
–
show
|
|||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns new instance with middleware handlers replaced with the ones provided. |
||
| 53 | * Last specified handler will be executed first. |
||
| 54 | * |
||
| 55 | * @param array[]|callable[]|string[] $middlewareDefinitions Each array element is: |
||
| 56 | * |
||
| 57 | * - A name of PSR-15 middleware class. The middleware instance will be obtained from container executed. |
||
| 58 | * - A callable with `function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface` |
||
| 59 | * signature. |
||
| 60 | * - A controller handler action in format `[TestController::class, 'index']`. `TestController` instance will |
||
| 61 | * be created and `index()` method will be executed. |
||
| 62 | * - A function returning a middleware. The middleware returned will be executed. |
||
| 63 | * - An array definition of middleware ({@link https://github.com/yiisoft/definitions#arraydefinition}). |
||
| 64 | * |
||
| 65 | * For handler action and callable |
||
| 66 | * typed parameters are automatically injected using dependency injection container. |
||
| 67 | * Current request and handler could be obtained by type-hinting for {@see ServerRequestInterface} |
||
| 68 | * and {@see RequestHandlerInterface}. |
||
| 69 | */ |
||
| 70 | 10 | public function withMiddlewares(array $middlewareDefinitions): self |
|
| 71 | { |
||
| 72 | 10 | $new = clone $this; |
|
| 73 | 10 | $new->middlewareDefinitions = array_reverse($middlewareDefinitions); |
|
| 74 | |||
| 75 | // Fixes a memory leak. |
||
| 76 | 10 | unset($new->stack); |
|
| 77 | 10 | $new->stack = null; |
|
| 78 | |||
| 79 | 10 | return $new; |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return bool Whether there are middleware defined in the dispatcher. |
||
| 84 | */ |
||
| 85 | 2 | public function hasMiddlewares(): bool |
|
| 86 | { |
||
| 87 | 2 | return $this->middlewareDefinitions !== []; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return Closure[] |
||
| 92 | */ |
||
| 93 | 7 | private function buildMiddlewares(): array |
|
| 94 | { |
||
| 95 | 7 | $middlewares = []; |
|
| 96 | 7 | $factory = $this->middlewareFactory; |
|
| 97 | |||
| 98 | 7 | foreach ($this->middlewareDefinitions as $middlewareDefinition) { |
|
| 99 | 7 | $middlewares[] = static fn (): MiddlewareInterface => $factory->create($middlewareDefinition); |
|
| 100 | } |
||
| 101 | |||
| 102 | 7 | return $middlewares; |
|
| 103 | } |
||
| 104 | } |
||
| 105 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.