Skip to content

Commit 82330d4

Browse files
Yrweindg
authored andcommitted
Presenter: removed static from refUrl variable (#80)
1 parent 86eb0ee commit 82330d4

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/Application/UI/Presenter.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ abstract class Presenter extends Control implements Application\IPresenter
121121
/** @var ITemplateFactory */
122122
private $templateFactory;
123123

124+
/** @var Nette\Http\Url */
125+
private $refUrlCache;
126+
124127

125128
public function __construct()
126129
{
@@ -970,15 +973,14 @@ protected function createRequest($component, $destination, array $args, $mode)
970973
}
971974

972975
// CONSTRUCT URL
973-
static $refUrl;
974-
if ($refUrl === NULL) {
975-
$refUrl = new Http\Url($this->httpRequest->getUrl());
976-
$refUrl->setPath($this->httpRequest->getUrl()->getScriptPath());
976+
if ($this->refUrlCache === NULL) {
977+
$this->refUrlCache = new Http\Url($this->httpRequest->getUrl());
978+
$this->refUrlCache->setPath($this->httpRequest->getUrl()->getScriptPath());
977979
}
978980
if (!$this->router) {
979981
throw new Nette\InvalidStateException('Unable to generate URL, service Router has not been set.');
980982
}
981-
$url = $this->router->constructUrl($this->lastCreatedRequest, $refUrl);
983+
$url = $this->router->constructUrl($this->lastCreatedRequest, $this->refUrlCache);
982984
if ($url === NULL) {
983985
unset($args[self::ACTION_KEY]);
984986
$params = urldecode(http_build_query($args, NULL, ', '));
@@ -987,7 +989,7 @@ protected function createRequest($component, $destination, array $args, $mode)
987989

988990
// make URL relative if possible
989991
if ($mode === 'link' && $scheme === FALSE && !$this->absoluteUrls) {
990-
$hostUrl = $refUrl->getHostUrl() . '/';
992+
$hostUrl = $this->refUrlCache->getHostUrl() . '/';
991993
if (strncmp($url, $hostUrl, strlen($hostUrl)) === 0) {
992994
$url = substr($url, strlen($hostUrl) - 1);
993995
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\UI\Presenter::link()
5+
*/
6+
7+
use Nette\Http,
8+
Nette\Application,
9+
Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
class TestPresenter extends Application\UI\Presenter
16+
{
17+
18+
protected function createTemplate($class = NULL)
19+
{
20+
}
21+
22+
}
23+
24+
class MockPresenterFactory extends Nette\Object implements Nette\Application\IPresenterFactory
25+
{
26+
function getPresenterClass(& $name)
27+
{
28+
return str_replace(':', 'Module\\', $name) . 'Presenter';
29+
}
30+
31+
function createPresenter($name)
32+
{}
33+
}
34+
35+
function testLink($domain)
36+
{
37+
$url = new Http\UrlScript('http://' . $domain . '/index.php');
38+
$url->setScriptPath('/index.php');
39+
40+
$presenter = new TestPresenter;
41+
$presenter->injectPrimary(
42+
NULL,
43+
new MockPresenterFactory,
44+
new Application\Routers\SimpleRouter,
45+
new Http\Request($url),
46+
new Http\Response
47+
);
48+
49+
$request = new Application\Request('Test', Http\Request::GET, []);
50+
$presenter->run($request);
51+
52+
Assert::same( 'http://' . $domain . '/index.php?action=default&presenter=Test', $presenter->link('//this') );
53+
}
54+
55+
testLink('first.localhost');
56+
testLink('second.localhost');

0 commit comments

Comments
 (0)