Final Class Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher
| Inheritance | Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher | |
| dispatch() | Dispatch request through middleware to get response. | Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher |
| hasMiddlewares() | Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher | |
| withMiddlewares() | Returns new instance with middleware handlers replaced with the ones provided. | Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher |
Method Details
| public mixed __construct ( Yiisoft\Middleware\Dispatcher\MiddlewareFactory $middlewareFactory, \Psr\EventDispatcher\EventDispatcherInterface|null $eventDispatcher = null ) | ||
| $middlewareFactory | Yiisoft\Middleware\Dispatcher\MiddlewareFactory | |
| $eventDispatcher | \Psr\EventDispatcher\EventDispatcherInterface|null | |
public function __construct(
private MiddlewareFactory $middlewareFactory,
private ?EventDispatcherInterface $eventDispatcher = null
) {
}
Dispatch request through middleware to get response.
| public \Psr\Http\Message\ResponseInterface dispatch ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $fallbackHandler ) | ||
| $request | \Psr\Http\Message\ServerRequestInterface |
Request to pass to middleware. |
| $fallbackHandler | \Psr\Http\Server\RequestHandlerInterface |
Handler to use in case no middleware produced response. |
public function dispatch(
ServerRequestInterface $request,
RequestHandlerInterface $fallbackHandler
): ResponseInterface {
if ($this->stack === null) {
$this->stack = new MiddlewareStack($this->buildMiddlewares(), $fallbackHandler, $this->eventDispatcher);
}
return $this->stack->handle($request);
}
| public boolean hasMiddlewares ( ) | ||
| return | boolean |
Whether there are middleware defined in the dispatcher. |
|---|---|---|
public function hasMiddlewares(): bool
{
return $this->middlewareDefinitions !== [];
}
Returns new instance with middleware handlers replaced with the ones provided.
Last specified handler will be executed first.
| public self withMiddlewares ( array[]|callable[]|string[] $middlewareDefinitions ) | ||
| $middlewareDefinitions | array[]|callable[]|string[] |
Each array element is:
For handler action and callable typed parameters are automatically injected using dependency injection container. Current request and handler could be obtained by type-hinting for {@see \Psr\Http\Message\ServerRequestInterface} and {@see \Psr\Http\Server\RequestHandlerInterface}. |
public function withMiddlewares(array $middlewareDefinitions): self
{
$new = clone $this;
$new->middlewareDefinitions = array_reverse($middlewareDefinitions);
// Fixes a memory leak.
unset($new->stack);
$new->stack = null;
return $new;
}
Signup or Login in order to comment.