. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Factories; use Aura\Router\Map; use Aura\Router\Route; use Aura\Router\RouterContainer; use Fisharebest\Webtrees\Contracts\RouteFactoryInterface; use Fisharebest\Webtrees\Html; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Validator; use Psr\Http\Message\ServerRequestInterface; use function app; use function array_filter; use function array_map; use function assert; use function intval; use function is_bool; use function parse_url; use function strlen; use function substr; use const ARRAY_FILTER_USE_KEY; use const PHP_URL_PATH; /** * Make a URL for a route. */ class RouteFactory implements RouteFactoryInterface { /** * Generate a URL for a named route. * * @param string $route_name * @param array|null> $parameters * * @return string */ public function route(string $route_name, array $parameters = []): string { $request = app(ServerRequestInterface::class); assert($request instanceof ServerRequestInterface); $base_url = Validator::attributes($request)->string('base_url'); $route = $this->routeMap()->getRoute($route_name); // Generate the URL. $router_container = app(RouterContainer::class); assert($router_container instanceof RouterContainer); // webtrees uses http_build_query() to generate URLs - which maps false onto "0". // Aura uses rawurlencode(), which maps false onto "" - which does not work as an aura URL parameter. $parameters = array_map(static fn ($var) => is_bool($var) ? (int) $var : $var, $parameters); // Aura doesn't work with empty/optional parameters. $parameters = array_map(static fn ($var) => $var === '' ? null : $var, $parameters); $url = $router_container->getGenerator()->generate($route_name, $parameters); // Aura ignores parameters that are not tokens. We need to add them as query parameters. $parameters = array_filter($parameters, static function (string $key) use ($route): bool { return !str_contains($route->path, '{' . $key . '}') && !str_contains($route->path, '{/' . $key . '}'); }, ARRAY_FILTER_USE_KEY); if (Validator::attributes($request)->boolean('rewrite_urls', false)) { // Make the pretty URL absolute. $base_path = parse_url($base_url, PHP_URL_PATH) ?: ''; $url = $base_url . substr($url, strlen($base_path)); } else { // Turn the pretty URL into an ugly one. $path = parse_url($url, PHP_URL_PATH); $parameters = ['route' => $path] + $parameters; $url = $base_url . '/index.php'; } return Html::url($url, $parameters); } /** * @return Map */ public function routeMap(): Map { $router_container = app(RouterContainer::class); assert($router_container instanceof RouterContainer); return $router_container->getMap(); } }