1 | <?php |
||
2 | |||
3 | /** |
||
4 | * webtrees: online genealogy |
||
5 | * Copyright (C) 2025 webtrees development team |
||
6 | * This program is free software: you can redistribute it and/or modify |
||
7 | * it under the terms of the GNU General Public License as published by |
||
8 | * the Free Software Foundation, either version 3 of the License, or |
||
9 | * (at your option) any later version. |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU General Public License for more details. |
||
14 | * You should have received a copy of the GNU General Public License |
||
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
16 | */ |
||
17 | |||
18 | declare(strict_types=1); |
||
19 | |||
20 | namespace Fisharebest\Webtrees\Http; |
||
21 | |||
22 | use Fisharebest\Webtrees\Http\RequestHandlers\NotFound; |
||
23 | use Fisharebest\Webtrees\Registry; |
||
24 | use LogicException; |
||
25 | use Psr\Http\Message\ResponseInterface; |
||
26 | use Psr\Http\Message\ServerRequestInterface; |
||
27 | use Psr\Http\Server\MiddlewareInterface; |
||
28 | use Psr\Http\Server\RequestHandlerInterface; |
||
29 | |||
30 | use function array_reduce; |
||
31 | use function array_reverse; |
||
32 | use function is_string; |
||
33 | |||
34 | readonly class Dispatcher |
||
35 | { |
||
36 | /** |
||
37 | * @param list<class-string|MiddlewareInterface> $middleware |
||
38 | */ |
||
39 | public static function dispatch(array $middleware, ServerRequestInterface $request): ResponseInterface |
||
40 | { |
||
41 | $pipeline = array_reduce( |
||
42 | array: array_reverse(array: $middleware), |
||
43 | callback: self::reduceMiddleware(...), |
||
44 | initial: new NotFound(), |
||
45 | ); |
||
46 | |||
47 | return $pipeline->handle(request: $request); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param class-string|MiddlewareInterface $item |
||
52 | */ |
||
53 | private static function reduceMiddleware( |
||
54 | RequestHandlerInterface $carry, |
||
55 | string|MiddlewareInterface $item, |
||
56 | ): RequestHandlerInterface { |
||
57 | return new readonly class (carry: $carry, item: $item) implements RequestHandlerInterface { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
58 | /** |
||
59 | * @param class-string|MiddlewareInterface $item |
||
60 | */ |
||
61 | public function __construct( |
||
62 | private RequestHandlerInterface $carry, |
||
63 | private string|MiddlewareInterface $item, |
||
64 | ) { |
||
65 | } |
||
66 | |||
67 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
68 | { |
||
69 | $item = $this->item; |
||
70 | |||
71 | if (is_string(value: $item)) { |
||
72 | $item = Registry::container()->get(id: $item); |
||
73 | } |
||
74 | |||
75 | if ($item instanceof MiddlewareInterface) { |
||
76 | return $item->process(request: $request, handler: $this->carry); |
||
77 | } |
||
78 | |||
79 | throw new LogicException(message: 'Invalid or undefined middleware'); |
||
80 | } |
||
81 | }; |
||
82 | } |
||
83 | } |
||
84 |