1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Http\Middleware; 20 21use Fig\Http\Message\RequestMethodInterface; 22use Fisharebest\Webtrees\Webtrees; 23use Illuminate\Support\Str; 24use Psr\Http\Message\ResponseInterface; 25use Psr\Http\Message\ServerRequestInterface; 26use Psr\Http\Server\MiddlewareInterface; 27use Psr\Http\Server\RequestHandlerInterface; 28 29use function app; 30use function explode; 31 32/** 33 * Simple class to help migrate to a third-party routing library. 34 */ 35class Router implements MiddlewareInterface, RequestMethodInterface 36{ 37 private const CONTROLLER_NAMESPACE = '\\Fisharebest\\Webtrees\\Http\\Controllers\\'; 38 39 // To parse Controller::action 40 private const SCOPE_OPERATOR = '::'; 41 42 /** @var string[][] */ 43 private $routes = [ 44 self::METHOD_GET => [], 45 self::METHOD_POST => [], 46 ]; 47 48 /** 49 * @param string $path 50 * @param string $handler 51 * 52 * @return Router 53 */ 54 public function get(string $path, string $handler): Router 55 { 56 return $this->add(self::METHOD_GET, $path, $handler); 57 } 58 59 /** 60 * @param string $method 61 * @param string $path 62 * @param string $handler 63 * 64 * @return Router 65 */ 66 private function add(string $method, string $path, string $handler): Router 67 { 68 $this->routes[$method][$path] = $handler; 69 70 return $this; 71 } 72 73 /** 74 * @param string $path 75 * @param string $handler 76 * 77 * @return Router 78 */ 79 public function post(string $path, string $handler): Router 80 { 81 return $this->add(self::METHOD_POST, $path, $handler); 82 } 83 84 /** 85 * @param ServerRequestInterface $request 86 * @param RequestHandlerInterface $handler 87 * 88 * @return ResponseInterface 89 */ 90 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 91 { 92 app()->instance(self::class, $this); 93 require Webtrees::ROOT_DIR . 'routes/web.php'; 94 95 $method = $request->getMethod(); 96 $route = $request->getQueryParams()['route'] ?? ''; 97 $routing = $this->routes[$method][$route] ?? ''; 98 99 // Bind the request into the container 100 app()->instance(ServerRequestInterface::class, $request); 101 102 // No route matched? 103 if ($routing === '') { 104 return $handler->handle($request); 105 } 106 107 // Routes defined using controller::action 108 if (Str::contains($routing, self::SCOPE_OPERATOR)) { 109 [$class, $method] = explode(self::SCOPE_OPERATOR, $routing); 110 111 return app(self::CONTROLLER_NAMESPACE . $class)->$method($request); 112 } 113 114 // Routes defined using a request handler 115 return app($routing)->handle($request); 116 } 117} 118