| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Http; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* Immutable representation of a URL with application base-path. |
| 13 |
* |
| 14 |
* <pre> |
| 15 |
* baseUrl basePath relativePath relativeUrl |
| 16 |
* | | | | |
| 17 |
* /---------------/-----\/--------\-----------------------------\ |
| 18 |
* http://nette.org/admin/script.php/pathinfo/?name=param#fragment |
| 19 |
* \_______________/\________/ |
| 20 |
* | | |
| 21 |
* scriptPath pathInfo |
| 22 |
* </pre> |
| 23 |
* |
| 24 |
* @property-read string $scriptPath |
| 25 |
* @property-read string $basePath |
| 26 |
* @property-read string $relativePath |
| 27 |
* @property-read string $baseUrl |
| 28 |
* @property-read string $relativeUrl |
| 29 |
* @property-read string $pathInfo |
| 30 |
*/ |
| 31 |
class UrlScript extends UrlImmutable |
| 32 |
{ |
| 33 |
/** @var string */ |
| 34 |
private $scriptPath; |
| 35 |
/** @var string */ |
| 36 |
private $basePath; |
| 37 |
public function __construct($url = '/', string $scriptPath = '') |
| 38 |
{ |
| 39 |
parent::__construct($url); |
| 40 |
$this->scriptPath = $scriptPath; |
| 41 |
$this->build(); |
| 42 |
} |
| 43 |
/** @return static */ |
| 44 |
public function withPath(string $path, string $scriptPath = '') |
| 45 |
{ |
| 46 |
$dolly = clone $this; |
| 47 |
$dolly->scriptPath = $scriptPath; |
| 48 |
$parent = \Closure::fromCallable([UrlImmutable::class, 'withPath'])->bindTo($dolly); |
| 49 |
return $parent($path); |
| 50 |
} |
| 51 |
public function getScriptPath() : string |
| 52 |
{ |
| 53 |
return $this->scriptPath; |
| 54 |
} |
| 55 |
public function getBasePath() : string |
| 56 |
{ |
| 57 |
return $this->basePath; |
| 58 |
} |
| 59 |
public function getRelativePath() : string |
| 60 |
{ |
| 61 |
return \substr($this->getPath(), \strlen($this->basePath)); |
| 62 |
} |
| 63 |
public function getBaseUrl() : string |
| 64 |
{ |
| 65 |
return $this->getHostUrl() . $this->basePath; |
| 66 |
} |
| 67 |
public function getRelativeUrl() : string |
| 68 |
{ |
| 69 |
return \substr($this->getAbsoluteUrl(), \strlen($this->getBaseUrl())); |
| 70 |
} |
| 71 |
/** |
| 72 |
* Returns the additional path information. |
| 73 |
*/ |
| 74 |
public function getPathInfo() : string |
| 75 |
{ |
| 76 |
return (string) \substr($this->getPath(), \strlen($this->scriptPath)); |
| 77 |
} |
| 78 |
protected function build() : void |
| 79 |
{ |
| 80 |
parent::build(); |
| 81 |
$path = $this->getPath(); |
| 82 |
$this->scriptPath = $this->scriptPath ?: $path; |
| 83 |
$pos = \strrpos($this->scriptPath, '/'); |
| 84 |
if ($pos === \false || \strncmp($this->scriptPath, $path, $pos + 1)) { |
| 85 |
throw new \Packetery\Nette\InvalidArgumentException("ScriptPath '{$this->scriptPath}' doesn't match path '{$path}'"); |
| 86 |
} |
| 87 |
$this->basePath = \substr($this->scriptPath, 0, $pos + 1); |
| 88 |
} |
| 89 |
} |
| 90 |
|