for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Yiisoft\Yii\Web\RequestHandler;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Yii\Web\Event\AfterMiddleware;
use Yiisoft\Yii\Web\Event\BeforeMiddleware;
final class MiddlewareHandler implements RequestHandlerInterface
{
private MiddlewareInterface $middleware;
private RequestHandlerInterface $handler;
private EventDispatcherInterface $dispatcher;
public function __construct(
MiddlewareInterface $middleware,
RequestHandlerInterface $handler,
EventDispatcherInterface $dispatcher
) {
$this->middleware = $middleware;
$this->handler = $handler;
$this->dispatcher = $dispatcher;
}
public function handle(ServerRequestInterface $request): ResponseInterface
$this->dispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
try {
return $response = $this->middleware->process($request, $this->handler);
} finally {
$this->dispatcher->dispatch(new AfterMiddleware($this->middleware, $response));