ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
GuzzleHttp\Psr7\UriResolver Class Reference

Resolves a URI reference in the context of a base URI and the opposite way. More...

+ Collaboration diagram for GuzzleHttp\Psr7\UriResolver:

Static Public Member Functions

static removeDotSegments ($path)
 Removes dot segments from a path and returns the new path. More...
 
static resolve (UriInterface $base, UriInterface $rel)
 Converts the relative URI into a new URI that is resolved against the base URI. More...
 
static relativize (UriInterface $base, UriInterface $target)
 Returns the target URI as a relative reference from the base URI. More...
 

Private Member Functions

 __construct ()
 

Static Private Member Functions

static getRelativePath (UriInterface $base, UriInterface $target)
 

Detailed Description

Resolves a URI reference in the context of a base URI and the opposite way.

Author
Tobias Schultze

https://tools.ietf.org/html/rfc3986#section-5

Definition at line 13 of file UriResolver.php.

Constructor & Destructor Documentation

◆ __construct()

GuzzleHttp\Psr7\UriResolver::__construct ( )
private

Definition at line 215 of file UriResolver.php.

216  {
217  // cannot be instantiated
218  }

Member Function Documentation

◆ getRelativePath()

static GuzzleHttp\Psr7\UriResolver::getRelativePath ( UriInterface  $base,
UriInterface  $target 
)
staticprivate

Definition at line 182 of file UriResolver.php.

References $i, Psr\Http\Message\UriInterface\getAuthority(), and Psr\Http\Message\UriInterface\getPath().

183  {
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]);
191  } else {
192  break;
193  }
194  }
195  $targetSegments[] = $targetLastSegment;
196  $relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments);
197 
198  // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
199  // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
200  // as the first segment of a relative-path reference, as it would be mistaken for a scheme name.
201  if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) {
202  $relativePath = "./$relativePath";
203  } elseif ('/' === $relativePath[0]) {
204  if ($base->getAuthority() != '' && $base->getPath() === '') {
205  // In this case an extra slash is added by resolve() automatically. So we must not add one here.
206  $relativePath = ".$relativePath";
207  } else {
208  $relativePath = "./$relativePath";
209  }
210  }
211 
212  return $relativePath;
213  }
$base
Definition: index.php:4
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ relativize()

static GuzzleHttp\Psr7\UriResolver::relativize ( UriInterface  $base,
UriInterface  $target 
)
static

Returns the target URI as a relative reference from the base URI.

This method is the counterpart to resolve():

(string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))

One use-case is to use the current request URI as base URI and then generate relative links in your documents to reduce the document size or offer self-contained downloadable document archives.

$base = new Uri('http://example.com/a/b/'); echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.

This method also accepts a target that is already relative and will try to relativize it further. Only a relative-path reference will be returned as-is.

echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well

Parameters
UriInterface$baseBase URI
UriInterface$targetTarget URI
Returns
UriInterface The relative URI reference

Definition at line 137 of file UriResolver.php.

References $target, Psr\Http\Message\UriInterface\getAuthority(), Psr\Http\Message\UriInterface\getPath(), Psr\Http\Message\UriInterface\getQuery(), Psr\Http\Message\UriInterface\getScheme(), GuzzleHttp\Psr7\Uri\isRelativePathReference(), and Psr\Http\Message\UriInterface\withScheme().

138  {
139  if ($target->getScheme() !== '' &&
140  ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
141  ) {
142  return $target;
143  }
144 
146  // As the target is already highly relative we return it as-is. It would be possible to resolve
147  // the target with `$target = self::resolve($base, $target);` and then try make it more relative
148  // by removing a duplicate query. But let's not do that automatically.
149  return $target;
150  }
151 
152  if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) {
153  return $target->withScheme('');
154  }
155 
156  // We must remove the path before removing the authority because if the path starts with two slashes, the URI
157  // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
158  // invalid.
159  $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost('');
160 
161  if ($base->getPath() !== $target->getPath()) {
162  return $emptyPathUri->withPath(self::getRelativePath($base, $target));
163  }
164 
165  if ($base->getQuery() === $target->getQuery()) {
166  // Only the target fragment is left. And it must be returned even if base and target fragment are the same.
167  return $emptyPathUri->withQuery('');
168  }
169 
170  // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
171  // inherit the base query component when resolving.
172  if ($target->getQuery() === '') {
173  $segments = explode('/', $target->getPath());
174  $lastSegment = end($segments);
175 
176  return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment);
177  }
178 
179  return $emptyPathUri;
180  }
$base
Definition: index.php:4
static isRelativePathReference(UriInterface $uri)
Whether the URI is a relative-path reference.
Definition: Uri.php:222
+ Here is the call graph for this function:

◆ removeDotSegments()

static GuzzleHttp\Psr7\UriResolver::removeDotSegments (   $path)
static

Removes dot segments from a path and returns the new path.

Parameters
string$path
Returns
string http://tools.ietf.org/html/rfc3986#section-5.2.4

Definition at line 23 of file UriResolver.php.

References $path, and $results.

Referenced by GuzzleHttp\Psr7\UriNormalizer\normalize(), and GuzzleHttp\Psr7\Uri\removeDotSegments().

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  // Re-add the leading slash if necessary for cases like "/.."
43  $newPath = '/' . $newPath;
44  } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
45  // Add the trailing slash if necessary
46  // If newPath is not empty, then $segment must be set and is the last segment from the foreach
47  $newPath .= '/';
48  }
49 
50  return $newPath;
51  }
$results
Definition: svg-scanner.php:47
+ Here is the caller graph for this function:

◆ resolve()

static GuzzleHttp\Psr7\UriResolver::resolve ( UriInterface  $base,
UriInterface  $rel 
)
static

Converts the relative URI into a new URI that is resolved against the base URI.

Parameters
UriInterface$baseBase URI
UriInterface$relRelative URI
Returns
UriInterface http://tools.ietf.org/html/rfc3986#section-5.2

Definition at line 62 of file UriResolver.php.

References $base, GuzzleHttp\Psr7\Uri\composeComponents(), Psr\Http\Message\UriInterface\getAuthority(), Psr\Http\Message\UriInterface\getFragment(), Psr\Http\Message\UriInterface\getPath(), Psr\Http\Message\UriInterface\getQuery(), Psr\Http\Message\UriInterface\getScheme(), and Psr\Http\Message\UriInterface\withPath().

Referenced by GuzzleHttp\Psr7\Uri\isSameDocumentReference(), and GuzzleHttp\Psr7\Uri\resolve().

63  {
64  if ((string) $rel === '') {
65  // we can simply return the same base URI instance for this same-document reference
66  return $base;
67  }
68 
69  if ($rel->getScheme() != '') {
70  return $rel->withPath(self::removeDotSegments($rel->getPath()));
71  }
72 
73  if ($rel->getAuthority() != '') {
74  $targetAuthority = $rel->getAuthority();
75  $targetPath = self::removeDotSegments($rel->getPath());
76  $targetQuery = $rel->getQuery();
77  } else {
78  $targetAuthority = $base->getAuthority();
79  if ($rel->getPath() === '') {
80  $targetPath = $base->getPath();
81  $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery();
82  } else {
83  if ($rel->getPath()[0] === '/') {
84  $targetPath = $rel->getPath();
85  } else {
86  if ($targetAuthority != '' && $base->getPath() === '') {
87  $targetPath = '/' . $rel->getPath();
88  } else {
89  $lastSlashPos = strrpos($base->getPath(), '/');
90  if ($lastSlashPos === false) {
91  $targetPath = $rel->getPath();
92  } else {
93  $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath();
94  }
95  }
96  }
97  $targetPath = self::removeDotSegments($targetPath);
98  $targetQuery = $rel->getQuery();
99  }
100  }
101 
102  return new Uri(Uri::composeComponents(
103  $base->getScheme(),
104  $targetAuthority,
105  $targetPath,
106  $targetQuery,
107  $rel->getFragment()
108  ));
109  }
$base
Definition: index.php:4
static composeComponents($scheme, $authority, $path, $query, $fragment)
Composes a URI reference string from its various components.
Definition: Uri.php:114
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: