|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* webtrees: online genealogy |
|
5
|
|
|
* Copyright (C) 2023 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 <https://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Factories; |
|
21
|
|
|
|
|
22
|
|
|
use Aura\Router\Map; |
|
23
|
|
|
use Aura\Router\Route; |
|
24
|
|
|
use Aura\Router\RouterContainer; |
|
25
|
|
|
use Fisharebest\Webtrees\Contracts\RouteFactoryInterface; |
|
26
|
|
|
use Fisharebest\Webtrees\Html; |
|
27
|
|
|
use Fisharebest\Webtrees\Registry; |
|
28
|
|
|
use Fisharebest\Webtrees\Validator; |
|
29
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
30
|
|
|
|
|
31
|
|
|
use function array_filter; |
|
32
|
|
|
use function array_map; |
|
33
|
|
|
use function is_bool; |
|
34
|
|
|
use function parse_url; |
|
35
|
|
|
use function strlen; |
|
36
|
|
|
use function substr; |
|
37
|
|
|
|
|
38
|
|
|
use const ARRAY_FILTER_USE_KEY; |
|
39
|
|
|
use const PHP_URL_PATH; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Make a URL for a route. |
|
43
|
|
|
*/ |
|
44
|
|
|
class RouteFactory implements RouteFactoryInterface |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* Generate a URL for a named route. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $route_name |
|
50
|
|
|
* @param array<bool|int|string|array<string>|null> $parameters |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function route(string $route_name, array $parameters = []): string |
|
55
|
|
|
{ |
|
56
|
|
|
$request = Registry::container()->get(ServerRequestInterface::class); |
|
57
|
|
|
$base_url = Validator::attributes($request)->string('base_url'); |
|
58
|
|
|
$route = $this->routeMap()->getRoute($route_name); |
|
59
|
|
|
|
|
60
|
|
|
// Generate the URL. |
|
61
|
|
|
$router_container = Registry::container()->get(RouterContainer::class); |
|
62
|
|
|
|
|
63
|
|
|
// webtrees uses http_build_query() to generate URLs - which maps false onto "0". |
|
64
|
|
|
// Aura uses rawurlencode(), which maps false onto "" - which does not work as an aura URL parameter. |
|
65
|
|
|
$parameters = array_map(static fn ($var) => is_bool($var) ? (int) $var : $var, $parameters); |
|
66
|
|
|
|
|
67
|
|
|
// Aura doesn't work with empty/optional URL parameters - but we need empty ones for query parameters. |
|
68
|
|
|
$url_parameters = array_map(static fn ($var) => $var === '' ? null : $var, $parameters); |
|
69
|
|
|
|
|
70
|
|
|
$url = $router_container->getGenerator()->generate($route_name, $url_parameters); |
|
71
|
|
|
|
|
72
|
|
|
// Aura ignores parameters that are not tokens. We need to add them as query parameters. |
|
73
|
|
|
$parameters = array_filter($parameters, static function (string $key) use ($route): bool { |
|
74
|
|
|
return !str_contains($route->path, '{' . $key . '}') && !str_contains($route->path, '{/' . $key . '}'); |
|
75
|
|
|
}, ARRAY_FILTER_USE_KEY); |
|
76
|
|
|
|
|
77
|
|
|
if (Validator::attributes($request)->boolean('rewrite_urls', false)) { |
|
78
|
|
|
// Make the pretty URL absolute. |
|
79
|
|
|
$base_path = parse_url($base_url, PHP_URL_PATH) ?: ''; |
|
80
|
|
|
$url = $base_url . substr($url, strlen($base_path)); |
|
81
|
|
|
} else { |
|
82
|
|
|
// Turn the pretty URL into an ugly one. |
|
83
|
|
|
$path = parse_url($url, PHP_URL_PATH); |
|
84
|
|
|
$parameters = ['route' => $path] + $parameters; |
|
85
|
|
|
$url = $base_url . '/index.php'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return Html::url($url, $parameters); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return Map<Route> |
|
93
|
|
|
*/ |
|
94
|
|
|
public function routeMap(): Map |
|
95
|
|
|
{ |
|
96
|
|
|
$router_container = Registry::container()->get(RouterContainer::class); |
|
97
|
|
|
|
|
98
|
|
|
return $router_container->getMap(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|