| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace GuzzleHttp\Psr7; |
| 6 |
|
| 7 |
use Psr\Http\Message\UriInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
final class UriResolver |
| 17 |
{ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public static function removeDotSegments(string $path): string |
| 24 |
{ |
| 25 |
if ($path === '' || $path === '/') { |
| 26 |
return $path; |
| 27 |
} |
| 28 |
|
| 29 |
$results = []; |
| 30 |
$segments = explode('/', $path); |
| 31 |
foreach ($segments as $segment) { |
| 32 |
if ($segment === '..') { |
| 33 |
array_pop($results); |
| 34 |
} elseif ($segment !== '.') { |
| 35 |
$results[] = $segment; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
$newPath = implode('/', $results); |
| 40 |
|
| 41 |
if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) { |
| 42 |
|
| 43 |
$newPath = '/'.$newPath; |
| 44 |
} elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) { |
| 45 |
|
| 46 |
|
| 47 |
$newPath .= '/'; |
| 48 |
} |
| 49 |
|
| 50 |
return $newPath; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static function resolve(UriInterface $base, UriInterface $rel): UriInterface |
| 59 |
{ |
| 60 |
if ((string) $rel === '') { |
| 61 |
|
| 62 |
return $base; |
| 63 |
} |
| 64 |
|
| 65 |
if ($rel->getScheme() != '') { |
| 66 |
return $rel->withPath(self::removeDotSegments($rel->getPath())); |
| 67 |
} |
| 68 |
|
| 69 |
if ($rel->getAuthority() != '') { |
| 70 |
return $rel |
| 71 |
->withScheme($base->getScheme()) |
| 72 |
->withPath(self::removeDotSegments($rel->getPath())); |
| 73 |
} |
| 74 |
|
| 75 |
if ($rel->getPath() === '') { |
| 76 |
$targetPath = $base->getPath(); |
| 77 |
$targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery(); |
| 78 |
} else { |
| 79 |
if ($rel->getPath()[0] === '/') { |
| 80 |
$targetPath = $rel->getPath(); |
| 81 |
} else { |
| 82 |
if ($base->getAuthority() != '' && $base->getPath() === '') { |
| 83 |
$targetPath = '/'.$rel->getPath(); |
| 84 |
} else { |
| 85 |
$lastSlashPos = strrpos($base->getPath(), '/'); |
| 86 |
if ($lastSlashPos === false) { |
| 87 |
$targetPath = $rel->getPath(); |
| 88 |
} else { |
| 89 |
$targetPath = substr($base->getPath(), 0, $lastSlashPos + 1).$rel->getPath(); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
$targetPath = self::removeDotSegments($targetPath); |
| 94 |
$targetQuery = $rel->getQuery(); |
| 95 |
} |
| 96 |
|
| 97 |
return $base |
| 98 |
->withPath($targetPath) |
| 99 |
->withQuery($targetQuery) |
| 100 |
->withFragment($rel->getFragment()); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static function relativize(UriInterface $base, UriInterface $target): UriInterface |
| 125 |
{ |
| 126 |
if ($target->getScheme() !== '' |
| 127 |
&& ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '') |
| 128 |
) { |
| 129 |
return $target; |
| 130 |
} |
| 131 |
|
| 132 |
if (Uri::isRelativePathReference($target)) { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
return $target; |
| 137 |
} |
| 138 |
|
| 139 |
if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) { |
| 140 |
return $target->withScheme(''); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
$emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost(''); |
| 147 |
|
| 148 |
if ($base->getPath() !== $target->getPath()) { |
| 149 |
return $emptyPathUri->withPath(self::getRelativePath($base, $target)); |
| 150 |
} |
| 151 |
|
| 152 |
if ($base->getQuery() === $target->getQuery()) { |
| 153 |
|
| 154 |
return $emptyPathUri->withQuery(''); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if ($target->getQuery() === '') { |
| 160 |
$segments = explode('/', $target->getPath()); |
| 161 |
|
| 162 |
$lastSegment = end($segments); |
| 163 |
|
| 164 |
return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment); |
| 165 |
} |
| 166 |
|
| 167 |
return $emptyPathUri; |
| 168 |
} |
| 169 |
|
| 170 |
private static function getRelativePath(UriInterface $base, UriInterface $target): string |
| 171 |
{ |
| 172 |
$sourceSegments = explode('/', $base->getPath()); |
| 173 |
$targetSegments = explode('/', $target->getPath()); |
| 174 |
array_pop($sourceSegments); |
| 175 |
$targetLastSegment = array_pop($targetSegments); |
| 176 |
foreach ($sourceSegments as $i => $segment) { |
| 177 |
if (isset($targetSegments[$i]) && $segment === $targetSegments[$i]) { |
| 178 |
unset($sourceSegments[$i], $targetSegments[$i]); |
| 179 |
} else { |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
$targetSegments[] = $targetLastSegment; |
| 184 |
$relativePath = str_repeat('../', count($sourceSegments)).implode('/', $targetSegments); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) { |
| 190 |
$relativePath = "./$relativePath"; |
| 191 |
} elseif ('/' === $relativePath[0]) { |
| 192 |
if ($base->getAuthority() != '' && $base->getPath() === '') { |
| 193 |
|
| 194 |
$relativePath = ".$relativePath"; |
| 195 |
} else { |
| 196 |
$relativePath = "./$relativePath"; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return $relativePath; |
| 201 |
} |
| 202 |
|
| 203 |
private function __construct() |
| 204 |
{ |
| 205 |
|
| 206 |
} |
| 207 |
} |