Skip to content

Commit

Permalink
Route: added port to %host%, %domain%, %tld% WIP [Closes #10][Closes n…
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2023
1 parent 5c4bfba commit 1bcdc82
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,13 @@ public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?stri
$parts = ip2long($host)
? [$host]
: array_reverse(explode('.', $host));
$port = $refUrl->getDefaultPort() === ($tmp = $refUrl->getPort()) ? '' : ':' . $tmp;
$url = strtr($url, [
'/%basePath%/' => $refUrl->getBasePath(),
'%tld%' => $parts[0],
'%domain%' => isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0],
'%tld%' => $parts[0] . $port,
'%domain%' => (isset($parts[1]) ? "$parts[1].$parts[0]" : $parts[0]) . $port,
'%sld%' => $parts[1] ?? '',
'%host%' => $host,
'%host%' => $host . $port,
]);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Route/ports.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Test: Nette\Routing\Route ports
*/

declare(strict_types=1);

use Nette\Http\UrlScript;
use Nette\Routing\Route;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$route = new Route('//%domain%');

$url = $route->constructUrl(
[],
new UrlScript('https://example.org:8000'),
);
Assert::same('https://example.org:8000', $url);

$url = $route->constructUrl(
[],
new UrlScript('https://localhost:8000'),
);
Assert::same('https://localhost:8000', $url);

0 comments on commit 1bcdc82

Please sign in to comment.