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