Passed
Push — develop ( f3d484...e21e10 )
by Greg
06:29
created

RouteFactory::route()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
nc 2
nop 2
dl 0
loc 32
rs 9.6666
c 1
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Factories;
21
22
use Aura\Router\RouterContainer;
23
use Fisharebest\Webtrees\Contracts\RouteFactoryInterface;
24
use Fisharebest\Webtrees\Html;
25
use Fisharebest\Webtrees\Validator;
26
use Psr\Http\Message\ServerRequestInterface;
27
28
use function app;
29
use function array_filter;
30
use function assert;
31
use function parse_url;
32
use function strlen;
33
use function substr;
34
35
use const ARRAY_FILTER_USE_KEY;
36
use const PHP_URL_PATH;
37
38
/**
39
 * Make a URL for a route.
40
 */
41
class RouteFactory implements RouteFactoryInterface
42
{
43
    /**
44
     * Generate a URL for a named route.
45
     *
46
     * @param string                                    $route_name
47
     * @param array<bool|int|string|array<string>|null> $parameters
48
     *
49
     * @return string
50
     */
51
    public function route(string $route_name, array $parameters = []): string
52
    {
53
        $request = app(ServerRequestInterface::class);
54
        assert($request instanceof ServerRequestInterface);
55
56
        $base_url = Validator::attributes($request)->string('base_url');
57
58
        $router_container = app(RouterContainer::class);
59
        assert($router_container instanceof RouterContainer);
60
61
        $route = $router_container->getMap()->getRoute($route_name);
62
63
        // Generate the URL.
64
        $url = $router_container->getGenerator()->generate($route_name, $parameters);
65
66
        // Aura ignores parameters that are not tokens.  We need to add them as query parameters.
67
        $parameters = array_filter($parameters, static function (string $key) use ($route): bool {
68
            return !str_contains($route->path, '{' . $key . '}') && !str_contains($route->path, '{/' . $key . '}');
69
        }, ARRAY_FILTER_USE_KEY);
70
71
        if (Validator::attributes($request)->boolean('rewrite_urls', false)) {
72
            // Make the pretty URL absolute.
73
            $base_path = parse_url($base_url, PHP_URL_PATH) ?: '';
74
            $url       = $base_url . substr($url, strlen($base_path));
75
        } else {
76
            // Turn the pretty URL into an ugly one.
77
            $path       = parse_url($url, PHP_URL_PATH);
78
            $parameters = ['route' => $path] + $parameters;
79
            $url        = $base_url . '/index.php';
80
        }
81
82
        return Html::url($url, $parameters);
83
    }
84
}
85