0 follower

Final Class Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher

InheritanceYiisoft\Middleware\Dispatcher\MiddlewareDispatcher

Public Methods

Hide inherited 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

Hide inherited methods

__construct() public method

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() public method

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);
}

            
hasMiddlewares() public method

public boolean hasMiddlewares ( )
return boolean

Whether there are middleware defined in the dispatcher.

                public function hasMiddlewares(): bool
{
    return $this->middlewareDefinitions !== [];
}

            
withMiddlewares() public method

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:

  • A name of PSR-15 middleware class. The middleware instance will be obtained from container executed.
  • A callable with function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface signature.
  • A controller handler action in format [TestController::class, 'index']. TestController instance will be created and index() method will be executed.
  • A function returning a middleware. The middleware returned will be executed.
  • An array definition of middleware ({@link https://github.com/yiisoft/definitions#arraydefinition}).

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;
}