1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resilient\Factory; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Resilient\Http\Uri as UriItem; |
7
|
|
|
use \Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
class Uri |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Create uri Instance from header. |
13
|
|
|
* |
14
|
|
|
* @access public |
15
|
|
|
* @static |
16
|
|
|
* @return Uri |
17
|
|
|
*/ |
18
|
|
|
public static function createFromServer($serv) |
19
|
|
|
{ |
20
|
|
|
$scheme = isset($serv['HTTPS']) ? 'https://' : 'http://'; |
21
|
|
|
$host = !empty($serv['HTTP_HOST']) ? $serv['HTTP_HOST'] : $serv['SERVER_NAME']; |
22
|
|
|
$port = empty($serv['SERVER_PORT']) ? $serv['SERVER_PORT'] : null; |
23
|
|
|
|
24
|
|
|
//Path |
25
|
|
|
$scriptName = parse_url($serv['SCRIPT_NAME'], PHP_URL_PATH); |
26
|
|
|
$scriptPath = dirname($scriptName); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$path = (string) parse_url('http://www.example.com/' . $serv['REQUEST_URI'], PHP_URL_PATH); |
29
|
|
|
|
30
|
|
|
$query = empty($serv['QUERY_STRING']) ? parse_url('http://example.com' . $serv['REQUEST_URI'], PHP_URL_QUERY) : $serv['QUERY_STRING']; |
31
|
|
|
|
32
|
|
|
$fragment = ''; |
33
|
|
|
|
34
|
|
|
$user = !empty($serv['PHP_AUTH_USER']) ? $serv['PHP_AUTH_USER'] : ''; |
35
|
|
|
$password = !empty($serv['PHP_AUTH_PW']) ? $serv['PHP_AUTH_PW'] : ''; |
36
|
|
|
|
37
|
|
|
if (empty($user) && empty($password) && !empty($serv['HTTP_AUTHORIZATION'])) { |
38
|
|
|
list($user, $password) = explode(':', base64_decode(substr($serv['HTTP_AUTHORIZATION'], 6))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$uri = new UriItem($scheme, $host, $port, $path, $query, $fragment, $user, $password); |
42
|
|
|
|
43
|
|
|
return $uri; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create Uri Instance from string http://www.example.com/url/path.html |
49
|
|
|
* |
50
|
|
|
* @access public |
51
|
|
|
* @static |
52
|
|
|
* @param string $uri |
53
|
|
|
* @return Uri |
54
|
|
|
*/ |
55
|
|
|
public static function createFromString(string $uri) |
56
|
|
|
{ |
57
|
|
|
$parts = parse_url($uri); |
58
|
|
|
$scheme = isset($parts['scheme']) ? $parts['scheme'] : ''; |
59
|
|
|
$user = isset($parts['user']) ? $parts['user'] : ''; |
60
|
|
|
$pass = isset($parts['pass']) ? $parts['pass'] : ''; |
61
|
|
|
$host = isset($parts['host']) ? $parts['host'] : ''; |
62
|
|
|
$port = isset($parts['port']) ? $parts['port'] : null; |
63
|
|
|
$path = isset($parts['path']) ? $parts['path'] : ''; |
64
|
|
|
$query = isset($parts['query']) ? $parts['query'] : ''; |
65
|
|
|
$fragment = isset($parts['fragment']) ? $parts['fragment'] : ''; |
66
|
|
|
|
67
|
|
|
return new UriItem($scheme, $host, $port, $path, $query, $fragment, $user, $pass); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.