Permalink
Browse files

UrlScript extends from UrlImmutable (BC break)

  • Loading branch information...
dg committed Feb 11, 2019
1 parent 1bd5d3f commit 3a53583df4a89c378ddb979d66a9d99df78c5e7f
@@ -81,7 +81,7 @@ public function __construct(UrlScript $url, array $post = null, array $files = n
*/
public function getUrl(): UrlScript
{
return clone $this->url;
return $this->url;
}
@@ -62,15 +62,14 @@ public function setProxy($proxy)
*/
public function createHttpRequest(): Request
{
$url = new UrlScript;
$url = new Url;
$this->identifyServer($url);
$this->identifyPathAndQuery($url);
$this->identifyScriptPath($url);
[$post, $cookies] = $this->identifyGetPostCookie($url);
[$remoteAddr, $remoteHost] = $this->identifyProxy($url);
return new Request(
$url,
new UrlScript($url, $this->identifyScriptPath($url)),
$post,
$this->identifyFiles(),
$cookies,
@@ -117,7 +116,7 @@ private function identifyPathAndQuery(Url $url): void
}
private function identifyScriptPath(Url $url): void
private function identifyScriptPath(Url $url): string
{
$path = $url->getPath();
$lpath = strtolower($path);
@@ -127,7 +126,7 @@ private function identifyScriptPath(Url $url): void
for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++);
$path = $i ? substr($path, 0, strrpos($path, '/', $i - strlen($path) - 1) + 1) : '/';
}
$url->setScriptPath($path);
return $path;
}
@@ -23,56 +23,58 @@
* scriptPath pathInfo
* </pre>
*
* @property string $scriptPath
* @property-read string $basePath
* @property-read string $relativePath
* @property-read string $baseUrl
* @property-read string $relativeUrl
* @property-read string $scriptPath
* @property-read string $pathInfo
*/
class UrlScript extends Url
class UrlScript extends UrlImmutable
{
/** @var string */
private $scriptPath;
/** @var string */
private $basePath;
public function __construct($url = null, string $scriptPath = '')
{
parent::__construct($url);
$this->setScriptPath($scriptPath);
$this->scriptPath = $scriptPath ?: $this->getPath();
$pos = strrpos($this->scriptPath, '/');
$this->basePath = $pos === false ? '' : substr($this->scriptPath, 0, $pos + 1);
}
/**
* Sets the script-path part of URI.
* @return static
*/
public function setScriptPath(string $value)
public function getBasePath(): string
{
$this->scriptPath = $value;
return $this;
return $this->basePath;
}
/**
* Returns the script-path part of URI.
*/
public function getScriptPath(): string
public function getRelativePath(): string
{
return $this->scriptPath ?: $this->path;
return substr($this->getPath(), strlen($this->basePath));
}
public function getRelativePath(): string
public function getBaseUrl(): string
{
return substr($this->getPath(), strrpos($this->scriptPath, '/') + 1);
return $this->getHostUrl() . $this->basePath;
}
/**
* Returns the base-path.
*/
public function getBasePath(): string
public function getRelativeUrl(): string
{
return substr($this->getAbsoluteUrl(), strlen($this->getBaseUrl()));
}
public function getScriptPath(): string
{
$pos = strrpos($this->getScriptPath(), '/');
return $pos === false ? '' : substr($this->getPath(), 0, $pos + 1);
return $this->scriptPath;
}
@@ -81,6 +83,6 @@ public function getBasePath(): string
*/
public function getPathInfo(): string
{
return (string) substr($this->getPath(), strlen($this->getScriptPath()));
return (string) substr($this->getPath(), strlen($this->scriptPath));
}
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,65 @@
<?php
/**
* Test: Nette\Http\UrlScript parse.
*/
declare(strict_types=1);
use Nette\Http\UrlScript;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
test(function () {
$url = new UrlScript('http://nette.org:8080/file.php?q=search');
Assert::same('/file.php', $url->scriptPath);
Assert::same('http://nette.org:8080/', $url->baseUrl);
Assert::same('/', $url->basePath);
Assert::same('file.php?q=search', $url->relativeUrl);
Assert::same('file.php', $url->relativePath);
Assert::same('', $url->pathInfo);
});
test(function () {
$url = new UrlScript('http://nette.org:8080/file.php?q=search', '/');
Assert::same('/', $url->scriptPath);
Assert::same('http://nette.org:8080/', $url->baseUrl);
Assert::same('/', $url->basePath);
Assert::same('file.php?q=search', $url->relativeUrl);
Assert::same('file.php', $url->relativePath);
Assert::same('file.php', $url->pathInfo);
});
test(function () {
$url = new UrlScript('http://nette.org:8080/test/?q=search', '/test/index.php');
Assert::same('/test/index.php', $url->scriptPath);
Assert::same('http://nette.org:8080/test/', $url->baseUrl);
Assert::same('/test/', $url->basePath);
Assert::same('?q=search', $url->relativeUrl);
Assert::same('', $url->relativePath);
Assert::same('', $url->pathInfo);
Assert::same('http://nette.org:8080/test/?q=search', $url->absoluteUrl);
});
test(function () {
$url = new UrlScript('http://nette.org:8080/www/about', '/www/');
Assert::same('/www/about', $url->path);
Assert::same('/www/', $url->scriptPath);
Assert::same('about', $url->relativePath);
Assert::same('about', $url->pathInfo);
});
test(function () {
$url = new UrlScript('http://nette.org:8080/www/index.php', '/www/index.php');
Assert::same('/www/index.php', $url->path);
Assert::same('/www/index.php', $url->scriptPath);
Assert::same('index.php', $url->relativePath);
Assert::same('', $url->pathInfo);
});

0 comments on commit 3a53583

Please sign in to comment.