Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the webmozart/path-util package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Webmozart\PathUtil;
|
Chris@0
|
13
|
Chris@0
|
14 use InvalidArgumentException;
|
Chris@0
|
15 use Webmozart\Assert\Assert;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Contains utility methods for handling URL strings.
|
Chris@0
|
19 *
|
Chris@0
|
20 * The methods in this class are able to deal with URLs.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @since 2.3
|
Chris@0
|
23 *
|
Chris@0
|
24 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
25 * @author Claudio Zizza <claudio@budgegeria.de>
|
Chris@0
|
26 */
|
Chris@0
|
27 final class Url
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Turns a URL into a relative path.
|
Chris@0
|
31 *
|
Chris@0
|
32 * The result is a canonical path. This class is using functionality of Path class.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @see Path
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param string $url A URL to make relative.
|
Chris@0
|
37 * @param string $baseUrl A base URL.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @return string
|
Chris@0
|
40 *
|
Chris@0
|
41 * @throws InvalidArgumentException If the URL and base URL does
|
Chris@0
|
42 * not match.
|
Chris@0
|
43 */
|
Chris@0
|
44 public static function makeRelative($url, $baseUrl)
|
Chris@0
|
45 {
|
Chris@0
|
46 Assert::string($url, 'The URL must be a string. Got: %s');
|
Chris@0
|
47 Assert::string($baseUrl, 'The base URL must be a string. Got: %s');
|
Chris@0
|
48 Assert::contains($baseUrl, '://', '%s is not an absolute Url.');
|
Chris@0
|
49
|
Chris@0
|
50 list($baseHost, $basePath) = self::split($baseUrl);
|
Chris@0
|
51
|
Chris@0
|
52 if (false === strpos($url, '://')) {
|
Chris@0
|
53 if (0 === strpos($url, '/')) {
|
Chris@0
|
54 $host = $baseHost;
|
Chris@0
|
55 } else {
|
Chris@0
|
56 $host = '';
|
Chris@0
|
57 }
|
Chris@0
|
58 $path = $url;
|
Chris@0
|
59 } else {
|
Chris@0
|
60 list($host, $path) = self::split($url);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 if ('' !== $host && $host !== $baseHost) {
|
Chris@0
|
64 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
65 'The URL "%s" cannot be made relative to "%s" since their host names are different.',
|
Chris@0
|
66 $host,
|
Chris@0
|
67 $baseHost
|
Chris@0
|
68 ));
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 return Path::makeRelative($path, $basePath);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Splits a URL into its host and the path.
|
Chris@0
|
76 *
|
Chris@0
|
77 * ```php
|
Chris@0
|
78 * list ($root, $path) = Path::split("http://example.com/webmozart")
|
Chris@0
|
79 * // => array("http://example.com", "/webmozart")
|
Chris@0
|
80 *
|
Chris@0
|
81 * list ($root, $path) = Path::split("http://example.com")
|
Chris@0
|
82 * // => array("http://example.com", "")
|
Chris@0
|
83 * ```
|
Chris@0
|
84 *
|
Chris@0
|
85 * @param string $url The URL to split.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return string[] An array with the host and the path of the URL.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @throws InvalidArgumentException If $url is not a URL.
|
Chris@0
|
90 */
|
Chris@0
|
91 private static function split($url)
|
Chris@0
|
92 {
|
Chris@0
|
93 $pos = strpos($url, '://');
|
Chris@0
|
94 $scheme = substr($url, 0, $pos + 3);
|
Chris@0
|
95 $url = substr($url, $pos + 3);
|
Chris@0
|
96
|
Chris@0
|
97 if (false !== ($pos = strpos($url, '/'))) {
|
Chris@0
|
98 $host = substr($url, 0, $pos);
|
Chris@0
|
99 $url = substr($url, $pos);
|
Chris@0
|
100 } else {
|
Chris@0
|
101 // No path, only host
|
Chris@0
|
102 $host = $url;
|
Chris@0
|
103 $url = '/';
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 // At this point, we have $scheme, $host and $path
|
Chris@0
|
107 $root = $scheme.$host;
|
Chris@0
|
108
|
Chris@0
|
109 return array($root, $url);
|
Chris@0
|
110 }
|
Chris@0
|
111 }
|