Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Psr7;
|
Chris@0
|
3
|
Chris@0
|
4 use Psr\Http\Message\UriInterface;
|
Chris@0
|
5
|
Chris@0
|
6 /**
|
Chris@0
|
7 * Resolves a URI reference in the context of a base URI and the opposite way.
|
Chris@0
|
8 *
|
Chris@0
|
9 * @author Tobias Schultze
|
Chris@0
|
10 *
|
Chris@0
|
11 * @link https://tools.ietf.org/html/rfc3986#section-5
|
Chris@0
|
12 */
|
Chris@0
|
13 final class UriResolver
|
Chris@0
|
14 {
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Removes dot segments from a path and returns the new path.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @param string $path
|
Chris@0
|
19 *
|
Chris@0
|
20 * @return string
|
Chris@0
|
21 * @link http://tools.ietf.org/html/rfc3986#section-5.2.4
|
Chris@0
|
22 */
|
Chris@0
|
23 public static function removeDotSegments($path)
|
Chris@0
|
24 {
|
Chris@0
|
25 if ($path === '' || $path === '/') {
|
Chris@0
|
26 return $path;
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 $results = [];
|
Chris@0
|
30 $segments = explode('/', $path);
|
Chris@0
|
31 foreach ($segments as $segment) {
|
Chris@0
|
32 if ($segment === '..') {
|
Chris@0
|
33 array_pop($results);
|
Chris@0
|
34 } elseif ($segment !== '.') {
|
Chris@0
|
35 $results[] = $segment;
|
Chris@0
|
36 }
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 $newPath = implode('/', $results);
|
Chris@0
|
40
|
Chris@0
|
41 if ($path[0] === '/' && (!isset($newPath[0]) || $newPath[0] !== '/')) {
|
Chris@0
|
42 // Re-add the leading slash if necessary for cases like "/.."
|
Chris@0
|
43 $newPath = '/' . $newPath;
|
Chris@0
|
44 } elseif ($newPath !== '' && ($segment === '.' || $segment === '..')) {
|
Chris@0
|
45 // Add the trailing slash if necessary
|
Chris@0
|
46 // If newPath is not empty, then $segment must be set and is the last segment from the foreach
|
Chris@0
|
47 $newPath .= '/';
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 return $newPath;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Converts the relative URI into a new URI that is resolved against the base URI.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @param UriInterface $base Base URI
|
Chris@0
|
57 * @param UriInterface $rel Relative URI
|
Chris@0
|
58 *
|
Chris@0
|
59 * @return UriInterface
|
Chris@0
|
60 * @link http://tools.ietf.org/html/rfc3986#section-5.2
|
Chris@0
|
61 */
|
Chris@0
|
62 public static function resolve(UriInterface $base, UriInterface $rel)
|
Chris@0
|
63 {
|
Chris@0
|
64 if ((string) $rel === '') {
|
Chris@0
|
65 // we can simply return the same base URI instance for this same-document reference
|
Chris@0
|
66 return $base;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 if ($rel->getScheme() != '') {
|
Chris@0
|
70 return $rel->withPath(self::removeDotSegments($rel->getPath()));
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 if ($rel->getAuthority() != '') {
|
Chris@0
|
74 $targetAuthority = $rel->getAuthority();
|
Chris@0
|
75 $targetPath = self::removeDotSegments($rel->getPath());
|
Chris@0
|
76 $targetQuery = $rel->getQuery();
|
Chris@0
|
77 } else {
|
Chris@0
|
78 $targetAuthority = $base->getAuthority();
|
Chris@0
|
79 if ($rel->getPath() === '') {
|
Chris@0
|
80 $targetPath = $base->getPath();
|
Chris@0
|
81 $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery();
|
Chris@0
|
82 } else {
|
Chris@0
|
83 if ($rel->getPath()[0] === '/') {
|
Chris@0
|
84 $targetPath = $rel->getPath();
|
Chris@0
|
85 } else {
|
Chris@0
|
86 if ($targetAuthority != '' && $base->getPath() === '') {
|
Chris@0
|
87 $targetPath = '/' . $rel->getPath();
|
Chris@0
|
88 } else {
|
Chris@0
|
89 $lastSlashPos = strrpos($base->getPath(), '/');
|
Chris@0
|
90 if ($lastSlashPos === false) {
|
Chris@0
|
91 $targetPath = $rel->getPath();
|
Chris@0
|
92 } else {
|
Chris@0
|
93 $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1) . $rel->getPath();
|
Chris@0
|
94 }
|
Chris@0
|
95 }
|
Chris@0
|
96 }
|
Chris@0
|
97 $targetPath = self::removeDotSegments($targetPath);
|
Chris@0
|
98 $targetQuery = $rel->getQuery();
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 return new Uri(Uri::composeComponents(
|
Chris@0
|
103 $base->getScheme(),
|
Chris@0
|
104 $targetAuthority,
|
Chris@0
|
105 $targetPath,
|
Chris@0
|
106 $targetQuery,
|
Chris@0
|
107 $rel->getFragment()
|
Chris@0
|
108 ));
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Returns the target URI as a relative reference from the base URI.
|
Chris@0
|
113 *
|
Chris@0
|
114 * This method is the counterpart to resolve():
|
Chris@0
|
115 *
|
Chris@0
|
116 * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
|
Chris@0
|
117 *
|
Chris@0
|
118 * One use-case is to use the current request URI as base URI and then generate relative links in your documents
|
Chris@0
|
119 * to reduce the document size or offer self-contained downloadable document archives.
|
Chris@0
|
120 *
|
Chris@0
|
121 * $base = new Uri('http://example.com/a/b/');
|
Chris@0
|
122 * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'.
|
Chris@0
|
123 * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'.
|
Chris@0
|
124 * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'.
|
Chris@0
|
125 * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
|
Chris@0
|
126 *
|
Chris@0
|
127 * This method also accepts a target that is already relative and will try to relativize it further. Only a
|
Chris@0
|
128 * relative-path reference will be returned as-is.
|
Chris@0
|
129 *
|
Chris@0
|
130 * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well
|
Chris@0
|
131 *
|
Chris@0
|
132 * @param UriInterface $base Base URI
|
Chris@0
|
133 * @param UriInterface $target Target URI
|
Chris@0
|
134 *
|
Chris@0
|
135 * @return UriInterface The relative URI reference
|
Chris@0
|
136 */
|
Chris@0
|
137 public static function relativize(UriInterface $base, UriInterface $target)
|
Chris@0
|
138 {
|
Chris@0
|
139 if ($target->getScheme() !== '' &&
|
Chris@0
|
140 ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '')
|
Chris@0
|
141 ) {
|
Chris@0
|
142 return $target;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 if (Uri::isRelativePathReference($target)) {
|
Chris@0
|
146 // As the target is already highly relative we return it as-is. It would be possible to resolve
|
Chris@0
|
147 // the target with `$target = self::resolve($base, $target);` and then try make it more relative
|
Chris@0
|
148 // by removing a duplicate query. But let's not do that automatically.
|
Chris@0
|
149 return $target;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) {
|
Chris@0
|
153 return $target->withScheme('');
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 // We must remove the path before removing the authority because if the path starts with two slashes, the URI
|
Chris@0
|
157 // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also
|
Chris@0
|
158 // invalid.
|
Chris@0
|
159 $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost('');
|
Chris@0
|
160
|
Chris@0
|
161 if ($base->getPath() !== $target->getPath()) {
|
Chris@0
|
162 return $emptyPathUri->withPath(self::getRelativePath($base, $target));
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 if ($base->getQuery() === $target->getQuery()) {
|
Chris@0
|
166 // Only the target fragment is left. And it must be returned even if base and target fragment are the same.
|
Chris@0
|
167 return $emptyPathUri->withQuery('');
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 // If the base URI has a query but the target has none, we cannot return an empty path reference as it would
|
Chris@0
|
171 // inherit the base query component when resolving.
|
Chris@0
|
172 if ($target->getQuery() === '') {
|
Chris@0
|
173 $segments = explode('/', $target->getPath());
|
Chris@0
|
174 $lastSegment = end($segments);
|
Chris@0
|
175
|
Chris@0
|
176 return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment);
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 return $emptyPathUri;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 private static function getRelativePath(UriInterface $base, UriInterface $target)
|
Chris@0
|
183 {
|
Chris@0
|
184 $sourceSegments = explode('/', $base->getPath());
|
Chris@0
|
185 $targetSegments = explode('/', $target->getPath());
|
Chris@0
|
186 array_pop($sourceSegments);
|
Chris@0
|
187 $targetLastSegment = array_pop($targetSegments);
|
Chris@0
|
188 foreach ($sourceSegments as $i => $segment) {
|
Chris@0
|
189 if (isset($targetSegments[$i]) && $segment === $targetSegments[$i]) {
|
Chris@0
|
190 unset($sourceSegments[$i], $targetSegments[$i]);
|
Chris@0
|
191 } else {
|
Chris@0
|
192 break;
|
Chris@0
|
193 }
|
Chris@0
|
194 }
|
Chris@0
|
195 $targetSegments[] = $targetLastSegment;
|
Chris@0
|
196 $relativePath = str_repeat('../', count($sourceSegments)) . implode('/', $targetSegments);
|
Chris@0
|
197
|
Chris@0
|
198 // A reference to am empty last segment or an empty first sub-segment must be prefixed with "./".
|
Chris@0
|
199 // This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
|
Chris@0
|
200 // as the first segment of a relative-path reference, as it would be mistaken for a scheme name.
|
Chris@0
|
201 if ('' === $relativePath || false !== strpos(explode('/', $relativePath, 2)[0], ':')) {
|
Chris@0
|
202 $relativePath = "./$relativePath";
|
Chris@0
|
203 } elseif ('/' === $relativePath[0]) {
|
Chris@0
|
204 if ($base->getAuthority() != '' && $base->getPath() === '') {
|
Chris@0
|
205 // In this case an extra slash is added by resolve() automatically. So we must not add one here.
|
Chris@0
|
206 $relativePath = ".$relativePath";
|
Chris@0
|
207 } else {
|
Chris@0
|
208 $relativePath = "./$relativePath";
|
Chris@0
|
209 }
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 return $relativePath;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 private function __construct()
|
Chris@0
|
216 {
|
Chris@0
|
217 // cannot be instantiated
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|