30 $segments = explode(
'/',
$path);
31 foreach ($segments as $segment) {
32 if ($segment ===
'..') {
34 } elseif ($segment !==
'.') {
41 if (
$path[0] ===
'/' && (!isset($newPath[0]) || $newPath[0] !==
'/')) {
43 $newPath =
'/' . $newPath;
44 } elseif ($newPath !==
'' && ($segment ===
'.' || $segment ===
'..')) {
64 if ((
string) $rel ===
'') {
75 $targetPath = self::removeDotSegments($rel->
getPath());
83 if ($rel->
getPath()[0] ===
'/') {
86 if ($targetAuthority !=
'' && $base->
getPath() ===
'') {
87 $targetPath =
'/' . $rel->
getPath();
89 $lastSlashPos = strrpos($base->
getPath(),
'/');
90 if ($lastSlashPos ===
false) {
93 $targetPath = substr($base->
getPath(), 0, $lastSlashPos + 1) . $rel->
getPath();
97 $targetPath = self::removeDotSegments($targetPath);
159 $emptyPathUri = $target->
withScheme(
'')->withPath(
'')->withUserInfo(
'')->withPort(null)->withHost(
'');
162 return $emptyPathUri->withPath(self::getRelativePath($base, $target));
167 return $emptyPathUri->withQuery(
'');
173 $segments = explode(
'/', $target->
getPath());
174 $lastSegment = end($segments);
176 return $emptyPathUri->withPath($lastSegment ===
'' ?
'./' : $lastSegment);
179 return $emptyPathUri;
184 $sourceSegments = explode(
'/', $base->
getPath());
185 $targetSegments = explode(
'/', $target->
getPath());
186 array_pop($sourceSegments);
187 $targetLastSegment = array_pop($targetSegments);
188 foreach ($sourceSegments as
$i => $segment) {
189 if (isset($targetSegments[
$i]) && $segment === $targetSegments[$i]) {
190 unset($sourceSegments[$i], $targetSegments[$i]);
195 $targetSegments[] = $targetLastSegment;
196 $relativePath = str_repeat(
'../', count($sourceSegments)) . implode(
'/', $targetSegments);
201 if (
'' === $relativePath ||
false !== strpos(explode(
'/', $relativePath, 2)[0],
':')) {
202 $relativePath =
"./$relativePath";
203 } elseif (
'/' === $relativePath[0]) {
206 $relativePath =
".$relativePath";
208 $relativePath =
"./$relativePath";
212 return $relativePath;
static getRelativePath(UriInterface $base, UriInterface $target)
static removeDotSegments($path)
Removes dot segments from a path and returns the new path.
getFragment()
Retrieve the fragment component of the URI.
Value object representing a URI.
static resolve(UriInterface $base, UriInterface $rel)
Converts the relative URI into a new URI that is resolved against the base URI.
PSR-7 URI implementation.
static relativize(UriInterface $base, UriInterface $target)
Returns the target URI as a relative reference from the base URI.
getScheme()
Retrieve the scheme component of the URI.
withPath($path)
Return an instance with the specified path.
static isRelativePathReference(UriInterface $uri)
Whether the URI is a relative-path reference.
getAuthority()
Retrieve the authority component of the URI.
getPath()
Retrieve the path component of the URI.
Resolves a URI reference in the context of a base URI and the opposite way.
static composeComponents($scheme, $authority, $path, $query, $fragment)
Composes a URI reference string from its various components.
withScheme($scheme)
Return an instance with the specified scheme.
getQuery()
Retrieve the query string of the URI.