19e5d8e6fSGreg Roach<?php 29e5d8e6fSGreg Roach 39e5d8e6fSGreg Roach/** 49e5d8e6fSGreg Roach * webtrees: online genealogy 59e5d8e6fSGreg Roach * Copyright (C) 2019 webtrees development team 69e5d8e6fSGreg Roach * This program is free software: you can redistribute it and/or modify 79e5d8e6fSGreg Roach * it under the terms of the GNU General Public License as published by 89e5d8e6fSGreg Roach * the Free Software Foundation, either version 3 of the License, or 99e5d8e6fSGreg Roach * (at your option) any later version. 109e5d8e6fSGreg Roach * This program is distributed in the hope that it will be useful, 119e5d8e6fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 129e5d8e6fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 139e5d8e6fSGreg Roach * GNU General Public License for more details. 149e5d8e6fSGreg Roach * You should have received a copy of the GNU General Public License 159e5d8e6fSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 169e5d8e6fSGreg Roach */ 179e5d8e6fSGreg Roachdeclare(strict_types=1); 189e5d8e6fSGreg Roach 199e5d8e6fSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 209e5d8e6fSGreg Roach 21ee4364daSGreg Roachuse Aura\Router\RouterContainer; 229e5d8e6fSGreg Roachuse Fig\Http\Message\RequestMethodInterface; 2382e92bfaSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 24ee4364daSGreg Roachuse Fisharebest\Webtrees\Tree; 25a992e8c1SGreg Roachuse Fisharebest\Webtrees\View; 2682e92bfaSGreg Roachuse Middleland\Dispatcher; 279e5d8e6fSGreg Roachuse Psr\Http\Message\ResponseInterface; 289e5d8e6fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 299e5d8e6fSGreg Roachuse Psr\Http\Server\MiddlewareInterface; 309e5d8e6fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 319e5d8e6fSGreg Roachuse function app; 3282e92bfaSGreg Roachuse function array_map; 33*44aaa5c3SGreg Roachuse function parse_url; 34ee4364daSGreg Roachuse const PHP_URL_PATH; 359e5d8e6fSGreg Roach 369e5d8e6fSGreg Roach/** 379e5d8e6fSGreg Roach * Simple class to help migrate to a third-party routing library. 389e5d8e6fSGreg Roach */ 399e5d8e6fSGreg Roachclass Router implements MiddlewareInterface, RequestMethodInterface 409e5d8e6fSGreg Roach{ 4182e92bfaSGreg Roach /** @var ModuleService */ 4282e92bfaSGreg Roach private $module_service; 4382e92bfaSGreg Roach 4482e92bfaSGreg Roach /** 4582e92bfaSGreg Roach * Router constructor. 4682e92bfaSGreg Roach * 4782e92bfaSGreg Roach * @param ModuleService $module_service 4882e92bfaSGreg Roach */ 49*44aaa5c3SGreg Roach public function __construct(ModuleService $module_service) 5082e92bfaSGreg Roach { 5182e92bfaSGreg Roach $this->module_service = $module_service; 529e5d8e6fSGreg Roach } 539e5d8e6fSGreg Roach 549e5d8e6fSGreg Roach /** 559e5d8e6fSGreg Roach * @param ServerRequestInterface $request 569e5d8e6fSGreg Roach * @param RequestHandlerInterface $handler 579e5d8e6fSGreg Roach * 589e5d8e6fSGreg Roach * @return ResponseInterface 599e5d8e6fSGreg Roach */ 609e5d8e6fSGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 619e5d8e6fSGreg Roach { 62*44aaa5c3SGreg Roach $base_url = $request->getAttribute('base_url'); 63*44aaa5c3SGreg Roach $base_path = parse_url($base_url, PHP_URL_PATH) ?? ''; 64*44aaa5c3SGreg Roach $router_container = new RouterContainer($base_path); 65*44aaa5c3SGreg Roach 66*44aaa5c3SGreg Roach // Save the router in the container, as we'll need it to generate URLs. 67*44aaa5c3SGreg Roach app()->instance(RouterContainer::class, $router_container); 68*44aaa5c3SGreg Roach 6982e92bfaSGreg Roach // Load the routing table. 70ee4364daSGreg Roach require __DIR__ . '/../../../routes/web.php'; 719e5d8e6fSGreg Roach 72ee4364daSGreg Roach if ($request->getAttribute('rewrite_urls') !== '1') { 73*44aaa5c3SGreg Roach // Turn the ugly URL into a pretty one. 74*44aaa5c3SGreg Roach $params = $request->getQueryParams(); 75*44aaa5c3SGreg Roach $route = $params['route'] ?? ''; 76*44aaa5c3SGreg Roach unset($params['route']); 77*44aaa5c3SGreg Roach $uri = $request->getUri()->withPath($route); 78*44aaa5c3SGreg Roach $request = $request->withUri($uri)->withQueryParams($params); 79ee4364daSGreg Roach } 809e5d8e6fSGreg Roach 81a992e8c1SGreg Roach // Bind the request into the container and the layout 829e5d8e6fSGreg Roach app()->instance(ServerRequestInterface::class, $request); 83a992e8c1SGreg Roach View::share('request', $request); 849e5d8e6fSGreg Roach 85ee4364daSGreg Roach // Match the request to a route. 86*44aaa5c3SGreg Roach $route = $router_container->getMatcher()->match($request); 87ee4364daSGreg Roach 889e5d8e6fSGreg Roach // No route matched? 89ee4364daSGreg Roach if ($route === false) { 909e5d8e6fSGreg Roach return $handler->handle($request); 919e5d8e6fSGreg Roach } 929e5d8e6fSGreg Roach 9382e92bfaSGreg Roach // Firstly, apply the route middleware 94ee4364daSGreg Roach $route_middleware = $route->extras['middleware'] ?? []; 9582e92bfaSGreg Roach $route_middleware = array_map('app', $route_middleware); 969e5d8e6fSGreg Roach 9782e92bfaSGreg Roach // Secondly, apply any module middleware 9882e92bfaSGreg Roach $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); 999e5d8e6fSGreg Roach 100f1d4b4a2SGreg Roach // Add the route as attribute of the request 101ee4364daSGreg Roach $request = $request->withAttribute('route', $route->name); 102f1d4b4a2SGreg Roach 10382e92bfaSGreg Roach // Finally, run the handler using middleware 104ee4364daSGreg Roach $handler_middleware = [new WrapHandler($route->handler)]; 10582e92bfaSGreg Roach 10682e92bfaSGreg Roach $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware); 10782e92bfaSGreg Roach 108ee4364daSGreg Roach // Add the matched attributes to the request. 109ee4364daSGreg Roach foreach ($route->attributes as $key => $value) { 110ee4364daSGreg Roach if ($key === 'tree') { 111ee4364daSGreg Roach $value = Tree::findByName($value); 112ee4364daSGreg Roach } 113ee4364daSGreg Roach $request = $request->withAttribute($key, $value); 114ee4364daSGreg Roach } 115ee4364daSGreg Roach 11682e92bfaSGreg Roach $dispatcher = new Dispatcher($middleware, app()); 11782e92bfaSGreg Roach 11882e92bfaSGreg Roach return $dispatcher->dispatch($request); 1199e5d8e6fSGreg Roach } 1209e5d8e6fSGreg Roach} 121