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