Skip to content

Commit

Permalink
back compatible solution
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJurasek committed Jan 19, 2021
1 parent 6739750 commit 2f18bdc
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Link generator.
*/
final class LinkGenerator implements ILinkGenerator
final class DefaultLinkGenerator implements ILinkGenerator
{
use Nette\SmartObject;

Expand Down Expand Up @@ -93,7 +93,7 @@ public function link(string $dest, array $params = []): string
}


public function withReferenceUrl(string $url): self
public function withReferenceUrl(string $url): ILinkGenerator
{
return new self(
$this->router,
Expand Down
2 changes: 1 addition & 1 deletion src/Application/ILinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ interface ILinkGenerator
*/
public function link(string $dest, array $params = []): string;

public function withReferenceUrl(string $url): self;
public function withReferenceUrl(string $url): ILinkGenerator;
}
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function loadConfiguration()

$builder->addDefinition($this->prefix('linkGenerator'))
->setType(Nette\Application\ILinkGenerator::class)
->setFactory(Nette\Application\LinkGenerator::class, [
->setFactory(Nette\Application\DefaultLinkGenerator::class, [
1 => new Definitions\Statement([new Definitions\Statement('@Nette\Http\IRequest::getUrl'), 'withoutUserInfo']),
]);

Expand Down
9 changes: 9 additions & 0 deletions src/compatibility-intf.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class_alias(\Nette\Routing\Router::class, IRouter::class);
class_alias(Response::class, IResponse::class);
}

if (false) {
/** @deprecated use ILinkGenerator */
interface LinkGenerator
{
}
} elseif (!interface_exists(LinkGenerator::class)) {
class_alias(\Nette\Application\ILinkGenerator::class, LinkGenerator::class);
}

namespace Nette\Application\UI;

if (false) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Bridges.DI/ApplicationExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ test('', function () {
$container = new Container1;
Assert::type(Nette\Application\Application::class, $container->getService('application'));
Assert::type(Nette\Application\PresenterFactory::class, $container->getService('nette.presenterFactory'));
Assert::type(Nette\Application\LinkGenerator::class, $container->getService('application.linkGenerator'));
Assert::type(Nette\Application\DefaultLinkGenerator::class, $container->getService('application.linkGenerator'));
});
14 changes: 7 additions & 7 deletions tests/Routers/LinkGenerator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace ModuleModule {

namespace {

use Nette\Application\LinkGenerator;
use Nette\Application\DefaultLinkGenerator;
use Nette\Application\PresenterFactory;
use Nette\Application\Routers;
use Nette\Http;
Expand All @@ -50,7 +50,7 @@ namespace {


test('', function () use ($pf) {
$generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'), $pf);
$generator = new DefaultLinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'), $pf);
Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default'));
Assert::same('http://nette.org/en/?action=default&presenter=Module%3AMy', $generator->link('Module:My:default'));
Assert::same('http://nette.org/en/?presenter=Module%3AMy', $generator->link('Module:My:'));
Expand All @@ -63,25 +63,25 @@ namespace {


Assert::exception(function () use ($pf) {
$generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'), $pf);
$generator = new DefaultLinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'), $pf);
$generator->link('default');
}, Nette\Application\UI\InvalidLinkException::class, "Invalid link destination 'default'.");


Assert::exception(function () use ($pf) {
$generator = new LinkGenerator(new Routers\Route('/', 'Product:'), new Http\UrlScript('http://nette.org/en/'), $pf);
$generator = new DefaultLinkGenerator(new Routers\Route('/', 'Product:'), new Http\UrlScript('http://nette.org/en/'), $pf);
$generator->link('Homepage:default', ['id' => 10]);
}, Nette\Application\UI\InvalidLinkException::class, 'No route for Homepage:default(id=10)');


Assert::exception(function () use ($pf) {
$generator = new LinkGenerator(new Routers\Route('/', 'Homepage:'), new Http\UrlScript('http://nette.org/en/'), $pf);
$generator = new DefaultLinkGenerator(new Routers\Route('/', 'Homepage:'), new Http\UrlScript('http://nette.org/en/'), $pf);
$generator->link('Homepage:missing', [10]);
}, Nette\Application\UI\InvalidLinkException::class, "Unable to pass parameters to action 'Homepage:missing', missing corresponding method.");


test('', function () {
$generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'));
$generator = new DefaultLinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'));
Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default'));
Assert::same('http://nette.org/en/?action=default&presenter=Module%3AMy', $generator->link('Module:My:default'));
Assert::same('http://nette.org/en/?presenter=Module%3AMy', $generator->link('Module:My:'));
Expand All @@ -94,7 +94,7 @@ namespace {


test('', function () {
$generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'));
$generator = new DefaultLinkGenerator(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'));
Expand Down

0 comments on commit 2f18bdc

Please sign in to comment.