From 56c5e1104238679fc98f27d3563b61a09dc3257c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 12 Feb 2019 01:03:26 +0100 Subject: [PATCH] Router::constructUrl() typehint changed from Nette\Http\Url to UrlScript (BC break) --- src/Application/IRouter.php | 2 +- src/Application/LinkGenerator.php | 4 ++-- src/Application/MicroPresenter.php | 4 ++-- src/Application/Routers/CliRouter.php | 2 +- src/Application/Routers/Route.php | 2 +- src/Application/Routers/RouteList.php | 2 +- src/Application/Routers/SimpleRouter.php | 2 +- src/Application/UI/Presenter.php | 6 +++--- tests/Application/Presenter.twoDomains.phpt | 3 +-- tests/Bridges.DI/RoutingExtension.cache.phpt | 2 +- tests/Routers/LinkGenerator.phpt | 10 +++++----- tests/Routers/Route.php | 8 ++++---- tests/Routers/Route.secured.phpt | 4 ++-- tests/Routers/Route.withHost.secured.phpt | 10 +++++----- tests/Routers/SimpleRouter.module.phpt | 5 ++--- tests/Routers/SimpleRouter.phpt | 5 ++--- tests/UI/Component.isLinkCurrent().asserts.php | 3 +-- tests/UI/Presenter.link().persistent.phpt | 3 +-- tests/UI/Presenter.link().phpt | 3 +-- 19 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/Application/IRouter.php b/src/Application/IRouter.php index 4a1285ac5..ffe3e254c 100644 --- a/src/Application/IRouter.php +++ b/src/Application/IRouter.php @@ -28,5 +28,5 @@ function match(Nette\Http\IRequest $httpRequest): ?array; /** * Constructs absolute URL from array. */ - function constructUrl(array $params, Nette\Http\Url $refUrl): ?string; + function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string; } diff --git a/src/Application/LinkGenerator.php b/src/Application/LinkGenerator.php index f4348858d..2330480af 100644 --- a/src/Application/LinkGenerator.php +++ b/src/Application/LinkGenerator.php @@ -22,14 +22,14 @@ final class LinkGenerator /** @var IRouter */ private $router; - /** @var Nette\Http\Url */ + /** @var Nette\Http\UrlScript */ private $refUrl; /** @var IPresenterFactory|null */ private $presenterFactory; - public function __construct(IRouter $router, Nette\Http\Url $refUrl, IPresenterFactory $presenterFactory = null) + public function __construct(IRouter $router, Nette\Http\UrlScript $refUrl, IPresenterFactory $presenterFactory = null) { $this->router = $router; $this->refUrl = $refUrl; diff --git a/src/Application/MicroPresenter.php b/src/Application/MicroPresenter.php index 767975661..681fce98b 100644 --- a/src/Application/MicroPresenter.php +++ b/src/Application/MicroPresenter.php @@ -58,8 +58,8 @@ public function run(Application\Request $request): Application\IResponse $this->request = $request; if ($this->httpRequest && $this->router && !$this->httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) { - $refUrl = clone $this->httpRequest->getUrl(); - $url = $this->router->constructUrl($request->toArray(), $refUrl->setPath($refUrl->getScriptPath())); + $refUrl = $this->httpRequest->getUrl(); + $url = $this->router->constructUrl($request->toArray(), $refUrl); if ($url !== null && !$this->httpRequest->getUrl()->isEqual($url)) { return new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY); } diff --git a/src/Application/Routers/CliRouter.php b/src/Application/Routers/CliRouter.php index 356ba2873..cd7216bb3 100644 --- a/src/Application/Routers/CliRouter.php +++ b/src/Application/Routers/CliRouter.php @@ -91,7 +91,7 @@ public function match(Nette\Http\IRequest $httpRequest): ?array /** * This router is only unidirectional. */ - public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string + public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string { return null; } diff --git a/src/Application/Routers/Route.php b/src/Application/Routers/Route.php index 12a403495..a95e89474 100644 --- a/src/Application/Routers/Route.php +++ b/src/Application/Routers/Route.php @@ -257,7 +257,7 @@ public function match(Nette\Http\IRequest $httpRequest): ?array /** * Constructs absolute URL from array. */ - public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string + public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string { if ($this->flags & self::ONE_WAY) { return null; diff --git a/src/Application/Routers/RouteList.php b/src/Application/Routers/RouteList.php index f478196af..388dd6137 100644 --- a/src/Application/Routers/RouteList.php +++ b/src/Application/Routers/RouteList.php @@ -54,7 +54,7 @@ public function match(Nette\Http\IRequest $httpRequest): ?array /** * Constructs absolute URL from array. */ - public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string + public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string { if ($this->cachedRoutes === null) { $this->warmupCache(); diff --git a/src/Application/Routers/SimpleRouter.php b/src/Application/Routers/SimpleRouter.php index ab8a4ecbb..841069e82 100644 --- a/src/Application/Routers/SimpleRouter.php +++ b/src/Application/Routers/SimpleRouter.php @@ -81,7 +81,7 @@ public function match(Nette\Http\IRequest $httpRequest): ?array /** * Constructs absolute URL from array. */ - public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string + public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string { if ($this->flags & self::ONE_WAY) { return null; diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index a16ee63bd..b0233e197 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -129,7 +129,7 @@ abstract class Presenter extends Control implements Application\IPresenter /** @var ITemplateFactory */ private $templateFactory; - /** @var Nette\Http\Url */ + /** @var Nette\Http\UrlScript */ private $refUrlCache; @@ -911,8 +911,8 @@ public static function parseDestination(string $destination): array protected function requestToUrl(Application\Request $request, bool $relative = null): string { if ($this->refUrlCache === null) { - $this->refUrlCache = new Http\Url($this->httpRequest->getUrl()); - $this->refUrlCache->setPath($this->httpRequest->getUrl()->getScriptPath()); + $url = $this->httpRequest->getUrl(); + $this->refUrlCache = new Http\UrlScript($url->getHostUrl() . $url->getScriptPath()); } if (!$this->router) { throw new Nette\InvalidStateException('Unable to generate URL, service Router has not been set.'); diff --git a/tests/Application/Presenter.twoDomains.phpt b/tests/Application/Presenter.twoDomains.phpt index 540d8d9e1..06fcda2a9 100644 --- a/tests/Application/Presenter.twoDomains.phpt +++ b/tests/Application/Presenter.twoDomains.phpt @@ -24,8 +24,7 @@ class TestPresenter extends Application\UI\Presenter function testLink($domain) { - $url = new Http\UrlScript('http://' . $domain . '/index.php'); - $url->setScriptPath('/index.php'); + $url = new Http\UrlScript('http://' . $domain . '/index.php', '/index.php'); $presenter = new TestPresenter; $presenter->injectPrimary( diff --git a/tests/Bridges.DI/RoutingExtension.cache.phpt b/tests/Bridges.DI/RoutingExtension.cache.phpt index 886f5d765..64c2c5b32 100644 --- a/tests/Bridges.DI/RoutingExtension.cache.phpt +++ b/tests/Bridges.DI/RoutingExtension.cache.phpt @@ -25,7 +25,7 @@ class MyRouter implements Nette\Application\IRouter } - public function constructUrl(array $params, Nette\Http\Url $refUrl): ?string + public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string { } diff --git a/tests/Routers/LinkGenerator.phpt b/tests/Routers/LinkGenerator.phpt index 53a9ccdfa..e3f1787c9 100644 --- a/tests/Routers/LinkGenerator.phpt +++ b/tests/Routers/LinkGenerator.phpt @@ -50,7 +50,7 @@ namespace { test(function () use ($pf) { - $generator = new LinkGenerator(new Routers\SimpleRouter, new Http\Url('http://nette.org/en/'), $pf); + $generator = new LinkGenerator(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:')); @@ -63,25 +63,25 @@ namespace { Assert::exception(function () use ($pf) { - $generator = new LinkGenerator(new Routers\SimpleRouter, new Http\Url('http://nette.org/en/'), $pf); + $generator = new LinkGenerator(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\Url('http://nette.org/en/'), $pf); + $generator = new LinkGenerator(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\Url('http://nette.org/en/'), $pf); + $generator = new LinkGenerator(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\Url('http://nette.org/en/')); + $generator = new LinkGenerator(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:')); diff --git a/tests/Routers/Route.php b/tests/Routers/Route.php index b3a514593..1a7dd1184 100644 --- a/tests/Routers/Route.php +++ b/tests/Routers/Route.php @@ -11,12 +11,12 @@ function testRouteIn(Nette\Application\IRouter $route, string $url, array $expectedParams = null, string $expectedUrl = null): void { - $url = new Nette\Http\UrlScript("http://example.com$url"); - $url->setScriptPath('/'); - $url->appendQuery([ + $urlBuilder = new Nette\Http\Url("http://example.com$url"); + $urlBuilder->appendQuery([ 'test' => 'testvalue', 'presenter' => 'querypresenter', ]); + $url = new Nette\Http\UrlScript($urlBuilder, '/'); $httpRequest = new Nette\Http\Request($url); @@ -40,6 +40,6 @@ function testRouteIn(Nette\Application\IRouter $route, string $url, array $expec function testRouteOut(Nette\Application\IRouter $route, array $params = []): ?string { - $url = new Nette\Http\Url('http://example.com'); + $url = new Nette\Http\UrlScript('http://example.com'); return $route->constructUrl($params, $url); } diff --git a/tests/Routers/Route.secured.phpt b/tests/Routers/Route.secured.phpt index 890b796ae..f5a0a79a4 100644 --- a/tests/Routers/Route.secured.phpt +++ b/tests/Routers/Route.secured.phpt @@ -7,7 +7,7 @@ declare(strict_types=1); use Nette\Application\Routers\Route; -use Nette\Http\Url; +use Nette\Http\UrlScript; use Tester\Assert; @@ -22,6 +22,6 @@ $route = new Route('', [ $url = $route->constructUrl( ['presenter' => 'Presenter', 'param' => 'any'], - new Url('https://example.org') + new UrlScript('https://example.org') ); Assert::same('https://example.org/any', $url); diff --git a/tests/Routers/Route.withHost.secured.phpt b/tests/Routers/Route.withHost.secured.phpt index 5a7e030b7..4ec0c0389 100644 --- a/tests/Routers/Route.withHost.secured.phpt +++ b/tests/Routers/Route.withHost.secured.phpt @@ -7,7 +7,7 @@ declare(strict_types=1); use Nette\Application\Routers\Route; -use Nette\Http\Url; +use Nette\Http\UrlScript; use Tester\Assert; @@ -23,13 +23,13 @@ $route = new Route('//example.org/test', [ $url = $route->constructUrl( ['presenter' => 'Default', 'action' => 'default'], - new Url('https://example.org') + new UrlScript('https://example.org') ); Assert::same('https://example.org/test', $url); $url = $route->constructUrl( ['presenter' => 'Default', 'action' => 'default'], - new Url('https://example.com') + new UrlScript('https://example.com') ); Assert::same('https://example.org/test', $url); @@ -42,12 +42,12 @@ $route = new Route('https://example.org/test', [ $url = $route->constructUrl( ['presenter' => 'Default', 'action' => 'default'], - new Url('https://example.org') + new UrlScript('https://example.org') ); Assert::same('https://example.org/test', $url); $url = $route->constructUrl( ['presenter' => 'Default', 'action' => 'default'], - new Url('https://example.com') + new UrlScript('https://example.com') ); Assert::same('https://example.org/test', $url); diff --git a/tests/Routers/SimpleRouter.module.phpt b/tests/Routers/SimpleRouter.module.phpt index 79779c80f..843ab9288 100644 --- a/tests/Routers/SimpleRouter.module.phpt +++ b/tests/Routers/SimpleRouter.module.phpt @@ -18,12 +18,11 @@ $router = new Application\Routers\SimpleRouter([ 'module' => 'main:sub', ]); -$url = new Http\UrlScript('http://nette.org/file.php'); -$url->setScriptPath('/file.php'); +$url = new Http\Url('http://nette.org/file.php', '/file.php'); $url->setQuery([ 'presenter' => 'myPresenter', ]); -$httpRequest = new Http\Request($url); +$httpRequest = new Http\Request(new Http\UrlScript($url)); $req = $router->match($httpRequest); Assert::same('main:sub:myPresenter', $req['presenter']); diff --git a/tests/Routers/SimpleRouter.phpt b/tests/Routers/SimpleRouter.phpt index bba16029a..8b3fb7a2f 100644 --- a/tests/Routers/SimpleRouter.phpt +++ b/tests/Routers/SimpleRouter.phpt @@ -19,15 +19,14 @@ $router = new SimpleRouter([ 'any' => 'anyvalue', ]); -$url = new Http\UrlScript('http://nette.org/file.php'); -$url->setScriptPath('/file.php'); +$url = new Http\Url('http://nette.org/file.php'); $url->setQuery([ 'presenter' => 'myPresenter', 'action' => 'action', 'id' => '12', 'test' => 'testvalue', ]); -$httpRequest = new Http\Request($url); +$httpRequest = new Http\Request(new Http\UrlScript($url, '/file.php')); $params = $router->match($httpRequest); Assert::same([ diff --git a/tests/UI/Component.isLinkCurrent().asserts.php b/tests/UI/Component.isLinkCurrent().asserts.php index 53d87bcd7..743284423 100644 --- a/tests/UI/Component.isLinkCurrent().asserts.php +++ b/tests/UI/Component.isLinkCurrent().asserts.php @@ -25,8 +25,7 @@ function callIsComponentLinkCurrent( $destination, array $args ): bool { - $url = new Http\UrlScript('http://localhost/index.php'); - $url->setScriptPath('/index.php'); + $url = new Http\UrlScript('http://localhost/index.php', '/index.php'); $presenterFactory = Mockery::mock(Nette\Application\IPresenterFactory::class); $presenterFactory->shouldReceive('getPresenterClass')->andReturn('TestPresenter'); diff --git a/tests/UI/Presenter.link().persistent.phpt b/tests/UI/Presenter.link().persistent.phpt index cbd438ec1..04ef25169 100644 --- a/tests/UI/Presenter.link().persistent.phpt +++ b/tests/UI/Presenter.link().persistent.phpt @@ -121,8 +121,7 @@ Assert::same([ ], ThirdPresenter::getReflection()->getPersistentParams()); -$url = new Http\UrlScript('http://localhost/index.php'); -$url->setScriptPath('/index.php'); +$url = new Http\UrlScript('http://localhost/index.php', '/index.php'); $presenterFactory = Mockery::mock(Nette\Application\IPresenterFactory::class); $presenterFactory->shouldReceive('getPresenterClass') diff --git a/tests/UI/Presenter.link().phpt b/tests/UI/Presenter.link().phpt index 6d3bbbd57..b3321f473 100644 --- a/tests/UI/Presenter.link().phpt +++ b/tests/UI/Presenter.link().phpt @@ -281,8 +281,7 @@ class OtherPresenter extends TestPresenter } -$url = new Http\UrlScript('http://localhost/index.php'); -$url->setScriptPath('/index.php'); +$url = new Http\UrlScript('http://localhost/index.php', '/index.php'); $presenterFactory = Mockery::mock(Nette\Application\IPresenterFactory::class); $presenterFactory->shouldReceive('getPresenterClass')