2 declare(strict_types
= 1);
3 namespace TYPO3\CMS\Core\Http
;
6 * This file is part of the TYPO3 CMS project.
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use Psr\Http\Message\ResponseInterface
;
19 use Psr\Http\Message\ServerRequestInterface
;
20 use Psr\Http\Server\MiddlewareInterface
;
21 use Psr\Http\Server\RequestHandlerInterface
;
22 use TYPO3\CMS\Core\Utility\GeneralUtility
;
25 * MiddlewareDispatcher
27 * This class manages and dispatches a PSR-15 middleware stack.
29 class MiddlewareDispatcher
implements RequestHandlerInterface
32 * Tip of the middleware call stack
34 * @var RequestHandlerInterface
36 protected $tip = null;
39 * @param RequestHandlerInterface $kernel
40 * @param array $middlewares
42 public function __construct(
43 RequestHandlerInterface
$kernel,
44 array $middlewares = []
46 $this->seedMiddlewareStack($kernel);
48 foreach ($middlewares as $middleware) {
49 if (is_string($middleware)) {
50 $this->lazy($middleware);
52 $this->add($middleware);
58 * Invoke the middleware stack
60 * @param ServerRequestInterface $request
61 * @return ResponseInterface
63 public function handle(ServerRequestInterface
$request): ResponseInterface
65 return $this->tip
->handle($request);
69 * Seed the middleware stack with the inner request handler
71 * @param RequestHandlerInterface $kernel
73 protected function seedMiddlewareStack(RequestHandlerInterface
$kernel)
79 * Add a new middleware to the stack
81 * Middlewares are organized as a stack. That means middlewares
82 * that have been added before will be executed after the newly
83 * added one (last in, first out).
85 * @param MiddlewareInterface $middleware
87 public function add(MiddlewareInterface
$middleware)
90 $this->tip
= new class($middleware, $next) implements RequestHandlerInterface
{
94 public function __construct(MiddlewareInterface
$middleware, RequestHandlerInterface
$next)
96 $this->middleware
= $middleware;
100 public function handle(ServerRequestInterface
$request): ResponseInterface
102 return $this->middleware
->process($request, $this->next
);
108 * Add a new middleware by class name
110 * Middlewares are organized as a stack. That means middlewares
111 * that have been added before will be executed after the newly
112 * added one (last in, first out).
114 * @param string $middleware
116 public function lazy(string $middleware)
119 $this->tip
= new class($middleware, $next) implements RequestHandlerInterface
{
123 public function __construct(string $middleware, RequestHandlerInterface
$next)
125 $this->middleware
= $middleware;
129 public function handle(ServerRequestInterface
$request): ResponseInterface
131 $middleware = GeneralUtility
::makeInstance($this->middleware
);
133 if (!$middleware instanceof MiddlewareInterface
) {
134 throw new \
InvalidArgumentException(get_class($middleware) . ' does not implement ' . MiddlewareInterface
::class, 1516821342);
136 return $middleware->process($request, $this->next
);