|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* webtrees: online genealogy |
|
4
|
|
|
* Copyright (C) 2019 webtrees development team |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* You should have received a copy of the GNU General Public License |
|
14
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15
|
|
|
*/ |
|
16
|
|
|
declare(strict_types=1); |
|
17
|
|
|
|
|
18
|
|
|
namespace Fisharebest\Webtrees; |
|
19
|
|
|
|
|
20
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
|
21
|
|
|
use Illuminate\Support\Str; |
|
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
24
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
25
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
26
|
|
|
use function app; |
|
27
|
|
|
use function explode; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Simple class to help migrate to a third-party routing library. |
|
31
|
|
|
*/ |
|
32
|
|
|
class Router implements MiddlewareInterface, RequestMethodInterface |
|
33
|
|
|
{ |
|
34
|
|
|
private const CONTROLLER_NAMESPACE = __NAMESPACE__ . '\\Http\\Controllers\\'; |
|
35
|
|
|
|
|
36
|
|
|
// To parse Controller::action |
|
37
|
|
|
private const SCOPE_OPERATOR = '::'; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string[][] */ |
|
40
|
|
|
private $routes = [ |
|
41
|
|
|
self::METHOD_GET => [], |
|
42
|
|
|
self::METHOD_POST => [], |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* @param string $handler |
|
48
|
|
|
* |
|
49
|
|
|
* @return Router |
|
50
|
|
|
*/ |
|
51
|
|
|
public function get(string $path, string $handler): Router |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->add(self::METHOD_GET, $path, $handler); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $method |
|
58
|
|
|
* @param string $path |
|
59
|
|
|
* @param string $handler |
|
60
|
|
|
* |
|
61
|
|
|
* @return Router |
|
62
|
|
|
*/ |
|
63
|
|
|
private function add(string $method, string $path, string $handler): Router |
|
64
|
|
|
{ |
|
65
|
|
|
$this->routes[$method][$path] = $handler; |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
* @param string $handler |
|
73
|
|
|
* |
|
74
|
|
|
* @return Router |
|
75
|
|
|
*/ |
|
76
|
|
|
public function post(string $path, string $handler): Router |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->add(self::METHOD_POST, $path, $handler); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ServerRequestInterface $request |
|
83
|
|
|
* @param RequestHandlerInterface $handler |
|
84
|
|
|
* |
|
85
|
|
|
* @return ResponseInterface |
|
86
|
|
|
*/ |
|
87
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
88
|
|
|
{ |
|
89
|
|
|
app()->instance(self::class, $this); |
|
90
|
|
|
require 'routes/web.php'; |
|
91
|
|
|
|
|
92
|
|
|
$method = $request->getMethod(); |
|
93
|
|
|
$route = $request->getQueryParams()['route'] ?? ''; |
|
94
|
|
|
$routing = $this->routes[$method][$route] ?? ''; |
|
95
|
|
|
|
|
96
|
|
|
// Bind the request into the container |
|
97
|
|
|
app()->instance(ServerRequestInterface::class, $request); |
|
98
|
|
|
|
|
99
|
|
|
// No route matched? |
|
100
|
|
|
if ($routing === '') { |
|
101
|
|
|
return $handler->handle($request); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Routes defined using controller::action |
|
105
|
|
|
if (Str::contains($routing, self::SCOPE_OPERATOR)) { |
|
106
|
|
|
[$class, $method] = explode(self::SCOPE_OPERATOR, $routing); |
|
107
|
|
|
|
|
108
|
|
|
$controller = app(self::CONTROLLER_NAMESPACE . $class); |
|
109
|
|
|
|
|
110
|
|
|
return app()->dispatch($controller, $method); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Routes defined using a request handler |
|
114
|
|
|
return app($routing)->handle($request); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|