xref: /webtrees/app/Http/Middleware/LoadRoutes.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
171378461SGreg Roach<?php
271378461SGreg Roach
371378461SGreg Roach/**
471378461SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
671378461SGreg Roach * This program is free software: you can redistribute it and/or modify
771378461SGreg Roach * it under the terms of the GNU General Public License as published by
871378461SGreg Roach * the Free Software Foundation, either version 3 of the License, or
971378461SGreg Roach * (at your option) any later version.
1071378461SGreg Roach * This program is distributed in the hope that it will be useful,
1171378461SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1271378461SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1371378461SGreg Roach * GNU General Public License for more details.
1471378461SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1671378461SGreg Roach */
17fcfa147eSGreg Roach
1871378461SGreg Roachdeclare(strict_types=1);
1971378461SGreg Roach
2071378461SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
2171378461SGreg Roach
2271378461SGreg Roachuse Aura\Router\RouterContainer;
232279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\ApiRoutes;
242279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes;
25*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2771378461SGreg Roachuse Psr\Http\Message\ResponseInterface;
2871378461SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2971378461SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
3071378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3171378461SGreg Roach
3271378461SGreg Roachuse function parse_url;
3371378461SGreg Roach
3471378461SGreg Roachuse const PHP_URL_PATH;
3571378461SGreg Roach
3671378461SGreg Roach/**
3771378461SGreg Roach * Load the routing table.
3871378461SGreg Roach */
3971378461SGreg Roachclass LoadRoutes implements MiddlewareInterface
4071378461SGreg Roach{
41c4943cffSGreg Roach    private ApiRoutes $api_routes;
422279b4f3SGreg Roach
43c4943cffSGreg Roach    private WebRoutes $web_routes;
442279b4f3SGreg Roach
452279b4f3SGreg Roach    /**
462279b4f3SGreg Roach     * @param ApiRoutes $api_routes
472279b4f3SGreg Roach     * @param WebRoutes $web_routes
482279b4f3SGreg Roach     */
492279b4f3SGreg Roach    public function __construct(ApiRoutes $api_routes, WebRoutes $web_routes)
502279b4f3SGreg Roach    {
512279b4f3SGreg Roach        $this->api_routes = $api_routes;
522279b4f3SGreg Roach        $this->web_routes = $web_routes;
532279b4f3SGreg Roach    }
542279b4f3SGreg Roach
5571378461SGreg Roach    /**
5671378461SGreg Roach     * @param ServerRequestInterface  $request
5771378461SGreg Roach     * @param RequestHandlerInterface $handler
5871378461SGreg Roach     *
5971378461SGreg Roach     * @return ResponseInterface
6071378461SGreg Roach     */
6171378461SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
6271378461SGreg Roach    {
63b55cbc6bSGreg Roach        $base_url         = Validator::attributes($request)->string('base_url');
6498116b24SGreg Roach        $base_path        = parse_url($base_url, PHP_URL_PATH);
6571378461SGreg Roach        $router_container = new RouterContainer($base_path);
6671378461SGreg Roach
672279b4f3SGreg Roach        // Load the core routing tables. Modules will load their own routes later.
682279b4f3SGreg Roach        $map = $router_container->getMap();
692279b4f3SGreg Roach        $this->api_routes->load($map);
702279b4f3SGreg Roach        $this->web_routes->load($map);
712279b4f3SGreg Roach
7271378461SGreg Roach        // Save the router in the container, as we'll need it to generate URLs.
73*d35568b4SGreg Roach        Registry::container()->set(RouterContainer::class, $router_container);
7471378461SGreg Roach
7571378461SGreg Roach        return $handler->handle($request);
7671378461SGreg Roach    }
7771378461SGreg Roach}
78