Skip to content

Commit 6a02df3

Browse files
committed
UrlScript extends from UrlImmutable (BC break)
1 parent ecef547 commit 6a02df3

File tree

7 files changed

+108
-128
lines changed

7 files changed

+108
-128
lines changed

src/Http/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __construct(UrlScript $url, array $post = null, array $files = n
8181
*/
8282
public function getUrl(): UrlScript
8383
{
84-
return clone $this->url;
84+
return $this->url;
8585
}
8686

8787

src/Http/RequestFactory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ public function setProxy($proxy)
6262
*/
6363
public function createHttpRequest(): Request
6464
{
65-
$url = new UrlScript;
65+
$url = new Url;
6666
$this->getServer($url);
6767
$this->getPathAndQuery($url);
68-
$this->getScriptPath($url);
6968
[$post, $cookies] = $this->getGetPostCookie($url);
7069
[$remoteAddr, $remoteHost] = $this->getClient($url);
7170

7271
return new Request(
73-
$url,
72+
new UrlScript($url, $this->getScriptPath($url)),
7473
$post,
7574
$this->getFiles(),
7675
$cookies,
@@ -117,7 +116,7 @@ private function getPathAndQuery(Url $url): void
117116
}
118117

119118

120-
private function getScriptPath(Url $url): void
119+
private function getScriptPath(Url $url): string
121120
{
122121
$path = $url->getPath();
123122
$lpath = strtolower($path);
@@ -127,7 +126,7 @@ private function getScriptPath(Url $url): void
127126
for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++);
128127
$path = $i ? substr($path, 0, strrpos($path, '/', $i - strlen($path) - 1) + 1) : '/';
129128
}
130-
$url->setScriptPath($path);
129+
return $path;
131130
}
132131

133132

src/Http/UrlScript.php

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,69 @@
1111

1212

1313
/**
14-
* Extended HTTP URL.
14+
* Immutable representation of a URL with application base-path.
1515
*
1616
* <pre>
17+
* baseUrl basePath relativePath relativeUrl
18+
* | | | |
19+
* /---------------/-----\/--------\-----------------------------\
1720
* http://nette.org/admin/script.php/pathinfo/?name=param#fragment
1821
* \_______________/\________/
1922
* | |
2023
* scriptPath pathInfo
2124
* </pre>
2225
*
23-
* - scriptPath: /admin/script.php (or simply /admin/ when script is directory index)
24-
* - pathInfo: /pathinfo/ (additional path information)
25-
*
26-
* @property string $scriptPath
26+
* @property-read string $scriptPath
27+
* @property-read string $basePath
2728
* @property-read string $relativePath
29+
* @property-read string $baseUrl
30+
* @property-read string $relativeUrl
2831
* @property-read string $pathInfo
2932
*/
30-
class UrlScript extends Url
33+
class UrlScript extends UrlImmutable
3134
{
3235
/** @var string */
3336
private $scriptPath;
3437

38+
/** @var string */
39+
private $basePath;
40+
3541

36-
public function __construct($url = null, string $scriptPath = '')
42+
public function __construct($url = '/', string $scriptPath = '')
3743
{
3844
parent::__construct($url);
39-
$this->setScriptPath($scriptPath);
45+
$this->scriptPath = $scriptPath;
46+
$this->build();
4047
}
4148

4249

43-
/**
44-
* Sets the script-path part of URI.
45-
* @return static
46-
*/
47-
public function setScriptPath(string $value)
50+
public function getScriptPath(): string
4851
{
49-
$this->scriptPath = $value;
50-
return $this;
52+
return $this->scriptPath;
5153
}
5254

5355

54-
/**
55-
* Returns the script-path part of URI.
56-
*/
57-
public function getScriptPath(): string
56+
public function getBasePath(): string
5857
{
59-
return $this->scriptPath ?: $this->path;
58+
return $this->basePath;
6059
}
6160

6261

6362
public function getRelativePath(): string
6463
{
65-
return substr($this->getPath(), strrpos($this->scriptPath, '/') + 1);
64+
return substr($this->getPath(), strlen($this->basePath));
6665
}
6766

6867

69-
/**
70-
* Returns the base-path.
71-
*/
72-
public function getBasePath(): string
68+
public function getBaseUrl(): string
69+
{
70+
return $this->getHostUrl() . $this->basePath;
71+
}
72+
73+
74+
public function getRelativeUrl(): string
7375
{
74-
$pos = strrpos($this->getScriptPath(), '/');
75-
return $pos === false ? '' : substr($this->getPath(), 0, $pos + 1);
76+
return substr($this->getAbsoluteUrl(), strlen($this->getBaseUrl()));
7677
}
7778

7879

@@ -81,6 +82,15 @@ public function getBasePath(): string
8182
*/
8283
public function getPathInfo(): string
8384
{
84-
return (string) substr($this->getPath(), strlen($this->getScriptPath()));
85+
return (string) substr($this->getPath(), strlen($this->scriptPath));
86+
}
87+
88+
89+
protected function build(): void
90+
{
91+
parent::build();
92+
$this->scriptPath = $this->scriptPath ?: $this->getPath();
93+
$pos = strrpos($this->scriptPath, '/');
94+
$this->basePath = $pos === false ? '' : substr($this->scriptPath, 0, $pos + 1);
8595
}
8696
}

tests/Http/Request.getUrl.phpt

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/Http/UrlScript.modify.phpt

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/Http/UrlScript.parse.phpt

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/Http/UrlScript.usage.phpt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Http\UrlScript parse.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Http\UrlScript;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
test(function () {
17+
$url = new UrlScript('http://nette.org:8080/file.php?q=search');
18+
Assert::same('/file.php', $url->scriptPath);
19+
Assert::same('http://nette.org:8080/', $url->baseUrl);
20+
Assert::same('/', $url->basePath);
21+
Assert::same('file.php?q=search', $url->relativeUrl);
22+
Assert::same('file.php', $url->relativePath);
23+
Assert::same('', $url->pathInfo);
24+
});
25+
26+
27+
test(function () {
28+
$url = new UrlScript('http://nette.org:8080/file.php?q=search', '/');
29+
Assert::same('/', $url->scriptPath);
30+
Assert::same('http://nette.org:8080/', $url->baseUrl);
31+
Assert::same('/', $url->basePath);
32+
Assert::same('file.php?q=search', $url->relativeUrl);
33+
Assert::same('file.php', $url->relativePath);
34+
Assert::same('file.php', $url->pathInfo);
35+
});
36+
37+
38+
test(function () {
39+
$url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php');
40+
Assert::same('/test/index.php', $url->scriptPath);
41+
Assert::same('http://nette.org:8080/test/', $url->baseUrl);
42+
Assert::same('/test/', $url->basePath);
43+
Assert::same('?q=search', $url->relativeUrl);
44+
Assert::same('', $url->relativePath);
45+
Assert::same('', $url->pathInfo);
46+
Assert::same('http://nette.org:8080/test/?q=search', $url->absoluteUrl);
47+
});
48+
49+
50+
test(function () {
51+
$url = new UrlScript('http://nette.org:8080/www/about', '/www/');
52+
Assert::same('/www/about', $url->path);
53+
Assert::same('/www/', $url->scriptPath);
54+
Assert::same('about', $url->relativePath);
55+
Assert::same('about', $url->pathInfo);
56+
});
57+
58+
59+
test(function () {
60+
$url = new UrlScript('http://nette.org:8080/www/index.php', '/www/index.php');
61+
Assert::same('/www/index.php', $url->path);
62+
Assert::same('/www/index.php', $url->scriptPath);
63+
Assert::same('index.php', $url->relativePath);
64+
Assert::same('', $url->pathInfo);
65+
});

0 commit comments

Comments
 (0)