8ec727c1
曹明
初始化代码提交
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
<?php
namespace Codeception\Util;
use GuzzleHttp\Psr7\Uri as Psr7Uri;
class Uri
{
/**
* Merges the passed $add argument onto $base.
*
* If a relative URL is passed as the 'path' part of the $add url
* array, the relative URL is mapped using the base 'path' part as
* its base.
*
* @param string $baseUri the base URL
* @param string $uri the URL to merge
* @return array the merged array
*/
public static function mergeUrls($baseUri, $uri)
{
$base = new Psr7Uri($baseUri);
$parts = parse_url($uri);
if ($parts === false) {
throw new \InvalidArgumentException("Invalid URI $uri");
}
if (isset($parts['host']) and isset($parts['scheme'])) {
// if this is an absolute url, replace with it
return $uri;
}
if (isset($parts['host'])) {
$base = $base->withHost($parts['host']);
$base = $base->withPath('');
$base = $base->withQuery('');
$base = $base->withFragment('');
}
if (isset($parts['path'])) {
$path = $parts['path'];
$basePath = $base->getPath();
if ((strpos($path, '/') !== 0) && !empty($path)) {
if ($basePath) {
// if it ends with a slash, relative paths are below it
if (preg_match('~/$~', $basePath)) {
$path = $basePath . $path;
} else {
// remove double slashes
$dir = rtrim(dirname($basePath), '\\/');
$path = $dir . '/' . $path;
}
} else {
$path = '/' . ltrim($path, '/');
}
}
$base = $base->withPath($path);
$base = $base->withQuery('');
$base = $base->withFragment('');
}
if (isset($parts['query'])) {
$base = $base->withQuery($parts['query']);
$base = $base->withFragment('');
}
if (isset($parts['fragment'])) {
$base = $base->withFragment($parts['fragment']);
}
return (string) $base;
}
/**
* Retrieve /path?query#fragment part of URL
* @param $url
* @return string
*/
public static function retrieveUri($url)
{
$uri = new Psr7Uri($url);
return (string)(new Psr7Uri())
->withPath($uri->getPath())
->withQuery($uri->getQuery())
->withFragment($uri->getFragment());
}
public static function retrieveHost($url)
{
$urlParts = parse_url($url);
if (!isset($urlParts['host']) or !isset($urlParts['scheme'])) {
throw new \InvalidArgumentException("Wrong URL passes, host and scheme not set");
}
$host = $urlParts['scheme'] . '://' . $urlParts['host'];
if (isset($urlParts['port'])) {
$host .= ':' . $urlParts['port'];
}
return $host;
}
public static function appendPath($url, $path)
{
$uri = new Psr7Uri($url);
$cutUrl = (string)$uri->withQuery('')->withFragment('');
if ($path === '' || $path[0] === '#') {
return $cutUrl . $path;
} else {
return rtrim($cutUrl, '/') . '/' . ltrim($path, '/');
}
}
}
|