xref: /webtrees/app/Http/Middleware/RequestHandler.php (revision 3521c6ec8d35cf7406782fc07c88c9c61fc52ae8)
1aa7265a1SGreg Roach<?php
2aa7265a1SGreg Roach
3aa7265a1SGreg Roach/**
4aa7265a1SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6aa7265a1SGreg Roach * This program is free software: you can redistribute it and/or modify
7aa7265a1SGreg Roach * it under the terms of the GNU General Public License as published by
8aa7265a1SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9aa7265a1SGreg Roach * (at your option) any later version.
10aa7265a1SGreg Roach * This program is distributed in the hope that it will be useful,
11aa7265a1SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12aa7265a1SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13aa7265a1SGreg Roach * GNU General Public License for more details.
14aa7265a1SGreg Roach * You should have received a copy of the GNU General Public License
15aa7265a1SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16aa7265a1SGreg Roach */
17aa7265a1SGreg Roach
18aa7265a1SGreg Roachdeclare(strict_types=1);
19aa7265a1SGreg Roach
20aa7265a1SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21aa7265a1SGreg Roach
22*3521c6ecSGreg Roachuse Fisharebest\Webtrees\Registry;
23b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
24aa7265a1SGreg Roachuse Psr\Http\Message\ResponseInterface;
25aa7265a1SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26aa7265a1SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
27aa7265a1SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28aa7265a1SGreg Roach
29aa7265a1SGreg Roachuse function is_string;
30aa7265a1SGreg Roach
31aa7265a1SGreg Roach/**
32aa7265a1SGreg Roach * Middleware to run a request-handler.
33aa7265a1SGreg Roach */
34aa7265a1SGreg Roachclass RequestHandler implements MiddlewareInterface
35aa7265a1SGreg Roach{
36aa7265a1SGreg Roach    /**
37aa7265a1SGreg Roach     * @param ServerRequestInterface  $request
38aa7265a1SGreg Roach     * @param RequestHandlerInterface $handler
39aa7265a1SGreg Roach     *
40aa7265a1SGreg Roach     * @return ResponseInterface
41aa7265a1SGreg Roach     */
42aa7265a1SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
43aa7265a1SGreg Roach    {
44b55cbc6bSGreg Roach        $route = Validator::attributes($request)->route();
45aa7265a1SGreg Roach
46aa7265a1SGreg Roach        $request_handler = $route->handler;
47aa7265a1SGreg Roach
48aa7265a1SGreg Roach        if (is_string($request_handler)) {
49*3521c6ecSGreg Roach            $request_handler = Registry::container()->get($request_handler);
50aa7265a1SGreg Roach        }
51aa7265a1SGreg Roach
52aa7265a1SGreg Roach        return $request_handler->handle($request);
53aa7265a1SGreg Roach    }
54aa7265a1SGreg Roach}
55