171378461SGreg Roach<?php 271378461SGreg Roach 371378461SGreg Roach/** 471378461SGreg Roach * webtrees: online genealogy 571378461SGreg Roach * Copyright (C) 2019 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 1571378461SGreg Roach * along with this program. If not, see <http://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; 23*2279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\ApiRoutes; 24*2279b4f3SGreg Roachuse Fisharebest\Webtrees\Http\Routes\WebRoutes; 2571378461SGreg Roachuse Psr\Http\Message\ResponseInterface; 2671378461SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 2771378461SGreg Roachuse Psr\Http\Server\MiddlewareInterface; 2871378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 2971378461SGreg Roach 3071378461SGreg Roachuse function app; 3171378461SGreg Roachuse function parse_url; 3271378461SGreg Roach 3371378461SGreg Roachuse const PHP_URL_PATH; 3471378461SGreg Roach 3571378461SGreg Roach/** 3671378461SGreg Roach * Load the routing table. 3771378461SGreg Roach */ 3871378461SGreg Roachclass LoadRoutes implements MiddlewareInterface 3971378461SGreg Roach{ 40*2279b4f3SGreg Roach /** @var ApiRoutes */ 41*2279b4f3SGreg Roach private $api_routes; 42*2279b4f3SGreg Roach 43*2279b4f3SGreg Roach /** @var WebRoutes */ 44*2279b4f3SGreg Roach private $web_routes; 45*2279b4f3SGreg Roach 46*2279b4f3SGreg Roach /** 47*2279b4f3SGreg Roach * @param ApiRoutes $api_routes 48*2279b4f3SGreg Roach * @param WebRoutes $web_routes 49*2279b4f3SGreg Roach */ 50*2279b4f3SGreg Roach public function __construct(ApiRoutes $api_routes, WebRoutes $web_routes) 51*2279b4f3SGreg Roach { 52*2279b4f3SGreg Roach $this->api_routes = $api_routes; 53*2279b4f3SGreg Roach $this->web_routes = $web_routes; 54*2279b4f3SGreg Roach } 55*2279b4f3SGreg Roach 5671378461SGreg Roach /** 5771378461SGreg Roach * @param ServerRequestInterface $request 5871378461SGreg Roach * @param RequestHandlerInterface $handler 5971378461SGreg Roach * 6071378461SGreg Roach * @return ResponseInterface 6171378461SGreg Roach */ 6271378461SGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 6371378461SGreg Roach { 6471378461SGreg Roach $base_url = $request->getAttribute('base_url'); 6571378461SGreg Roach $base_path = parse_url($base_url, PHP_URL_PATH) ?? ''; 6671378461SGreg Roach $router_container = new RouterContainer($base_path); 6771378461SGreg Roach 68*2279b4f3SGreg Roach // Load the core routing tables. Modules will load their own routes later. 69*2279b4f3SGreg Roach $map = $router_container->getMap(); 70*2279b4f3SGreg Roach $this->api_routes->load($map); 71*2279b4f3SGreg Roach $this->web_routes->load($map); 72*2279b4f3SGreg Roach 7371378461SGreg Roach // Save the router in the container, as we'll need it to generate URLs. 7471378461SGreg Roach app()->instance(RouterContainer::class, $router_container); 7571378461SGreg Roach 7671378461SGreg Roach return $handler->handle($request); 7771378461SGreg Roach } 7871378461SGreg Roach} 79