From ef333e63950ceea40def63c2e0f253fc90f19e19 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 9 Jun 2020 13:56:55 +0200 Subject: [PATCH] LinkGenerator: added withReferenceUrl() --- src/Application/LinkGenerator.php | 15 +++++++++++++-- tests/Routers/LinkGenerator.phpt | 8 ++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Application/LinkGenerator.php b/src/Application/LinkGenerator.php index 803300779..7738e1404 100644 --- a/src/Application/LinkGenerator.php +++ b/src/Application/LinkGenerator.php @@ -10,6 +10,7 @@ namespace Nette\Application; use Nette; +use Nette\Http\UrlScript; use Nette\Routing\Router; @@ -23,14 +24,14 @@ final class LinkGenerator /** @var Router */ private $router; - /** @var Nette\Http\UrlScript */ + /** @var UrlScript */ private $refUrl; /** @var IPresenterFactory|null */ private $presenterFactory; - public function __construct(Router $router, Nette\Http\UrlScript $refUrl, IPresenterFactory $presenterFactory = null) + public function __construct(Router $router, UrlScript $refUrl, IPresenterFactory $presenterFactory = null) { $this->router = $router; $this->refUrl = $refUrl; @@ -88,4 +89,14 @@ public function link(string $dest, array $params = []): string } return $url . $frag; } + + + public function withReferenceUrl(string $url): self + { + return new self( + $this->router, + new UrlScript($url), + $this->presenterFactory + ); + } } diff --git a/tests/Routers/LinkGenerator.phpt b/tests/Routers/LinkGenerator.phpt index e3f1787c9..cb53526ce 100644 --- a/tests/Routers/LinkGenerator.phpt +++ b/tests/Routers/LinkGenerator.phpt @@ -89,6 +89,14 @@ namespace { Assert::same('http://nette.org/en/?0=10&presenter=Homepage', $generator->link('Homepage:', [10])); Assert::same('http://nette.org/en/?0=10&id=20&action=detail&presenter=Homepage', $generator->link('Homepage:detail', [10, 'id' => 20])); Assert::same('http://nette.org/en/?presenter=Homepage#frag:ment', $generator->link('Homepage:#frag:ment')); + Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default')); }); + + test(function () { + $generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/')); + $generator2 = $generator->withReferenceUrl('http://nette.org/cs/'); + Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default')); + Assert::same('http://nette.org/cs/?action=default&presenter=Homepage', $generator2->link('Homepage:default')); + }); }