Skip to content

Commit

Permalink
LinkGenerator: added withReferenceUrl()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 9, 2020
1 parent 7575c5f commit ef333e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Application/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application;

use Nette;
use Nette\Http\UrlScript;
use Nette\Routing\Router;


Expand All @@ -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;
Expand Down Expand Up @@ -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
);
}
}
8 changes: 8 additions & 0 deletions tests/Routers/LinkGenerator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}

0 comments on commit ef333e6

Please sign in to comment.