Skip to content

Commit

Permalink
Presenter: removed static from refUrl variable (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yrwein authored and dg committed Jul 28, 2016
1 parent 86eb0ee commit 82330d4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ abstract class Presenter extends Control implements Application\IPresenter
/** @var ITemplateFactory */
private $templateFactory;

/** @var Nette\Http\Url */
private $refUrlCache;


public function __construct()
{
Expand Down Expand Up @@ -970,15 +973,14 @@ protected function createRequest($component, $destination, array $args, $mode)
}

// CONSTRUCT URL
static $refUrl;
if ($refUrl === NULL) {
$refUrl = new Http\Url($this->httpRequest->getUrl());
$refUrl->setPath($this->httpRequest->getUrl()->getScriptPath());
if ($this->refUrlCache === NULL) {
$this->refUrlCache = new Http\Url($this->httpRequest->getUrl());
$this->refUrlCache->setPath($this->httpRequest->getUrl()->getScriptPath());
}
if (!$this->router) {
throw new Nette\InvalidStateException('Unable to generate URL, service Router has not been set.');
}
$url = $this->router->constructUrl($this->lastCreatedRequest, $refUrl);
$url = $this->router->constructUrl($this->lastCreatedRequest, $this->refUrlCache);
if ($url === NULL) {
unset($args[self::ACTION_KEY]);
$params = urldecode(http_build_query($args, NULL, ', '));
Expand All @@ -987,7 +989,7 @@ protected function createRequest($component, $destination, array $args, $mode)

// make URL relative if possible
if ($mode === 'link' && $scheme === FALSE && !$this->absoluteUrls) {
$hostUrl = $refUrl->getHostUrl() . '/';
$hostUrl = $this->refUrlCache->getHostUrl() . '/';
if (strncmp($url, $hostUrl, strlen($hostUrl)) === 0) {
$url = substr($url, strlen($hostUrl) - 1);
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Application/Presenter.twoDomains.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Test: Nette\Application\UI\Presenter::link()
*/

use Nette\Http,
Nette\Application,
Tester\Assert;


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


class TestPresenter extends Application\UI\Presenter
{

protected function createTemplate($class = NULL)
{
}

}

class MockPresenterFactory extends Nette\Object implements Nette\Application\IPresenterFactory
{
function getPresenterClass(& $name)
{
return str_replace(':', 'Module\\', $name) . 'Presenter';
}

function createPresenter($name)
{}
}

function testLink($domain)
{
$url = new Http\UrlScript('http://' . $domain . '/index.php');
$url->setScriptPath('/index.php');

$presenter = new TestPresenter;
$presenter->injectPrimary(
NULL,
new MockPresenterFactory,
new Application\Routers\SimpleRouter,
new Http\Request($url),
new Http\Response
);

$request = new Application\Request('Test', Http\Request::GET, []);
$presenter->run($request);

Assert::same( 'http://' . $domain . '/index.php?action=default&presenter=Test', $presenter->link('//this') );
}

testLink('first.localhost');
testLink('second.localhost');

0 comments on commit 82330d4

Please sign in to comment.