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