|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
|
4
|
|
|
|
|
5
|
|
|
class Server |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var array |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $input = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @return Server |
|
15
|
|
|
*/ |
|
16
|
3 |
|
public static function sharedInstance(): self |
|
17
|
|
|
{ |
|
18
|
3 |
|
static $self; |
|
19
|
3 |
|
if (!$self) { |
|
20
|
1 |
|
$self = new static(); |
|
21
|
|
|
} |
|
22
|
3 |
|
return $self; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param null|string $default |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function get(string $name, ?string $default = null): ?string |
|
31
|
|
|
{ |
|
32
|
3 |
|
if (empty($this->input[$name])) { |
|
33
|
3 |
|
$this->input[$name] = \filter_input(INPUT_SERVER, $name, \FILTER_DEFAULT, [ |
|
34
|
|
|
'options' => [ |
|
35
|
3 |
|
'default' => $default |
|
36
|
|
|
] |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
3 |
|
return $this->input[$name]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $default |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function method(string $default = 'GET'): string |
|
48
|
|
|
{ |
|
49
|
3 |
|
return $this->get('REQUEST_METHOD', $default); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $default |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function protocol(string $default = 'https'): string |
|
57
|
|
|
{ |
|
58
|
3 |
|
$protocol = $this->get('HTTP_CF_VISITOR'); // cloudFlare |
|
59
|
|
|
|
|
60
|
3 |
|
if ($protocol) |
|
61
|
|
|
{ |
|
62
|
|
|
/** |
|
63
|
|
|
* { scheme: "https" } |
|
64
|
|
|
* |
|
65
|
|
|
* @var string $protocol |
|
66
|
|
|
*/ |
|
67
|
|
|
$protocol = json_decode($protocol, true); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
return $protocol['scheme'] ?? |
|
71
|
3 |
|
$this->get( |
|
72
|
3 |
|
'HTTP_X_FORWARDED_PROTO', |
|
73
|
3 |
|
$this->get('REQUEST_SCHEME', $default) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function host(): string |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->get('HTTP_HOST', PHP_SAPI); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function path(): string |
|
89
|
|
|
{ |
|
90
|
|
|
return \parse_url($this->get('REQUEST_URI'), PHP_URL_PATH); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @param null|string $host |
|
96
|
|
|
* @param null|string $protocol |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public static function url(string $path, ?string $host = null, ?string $protocol = null): string |
|
100
|
|
|
{ |
|
101
|
4 |
|
$scheme = $protocol ?? static::sharedInstance()->protocol(); |
|
102
|
4 |
|
$host = $host ?? static::sharedInstance()->host(); |
|
103
|
4 |
|
return $scheme . '://' . $host . $path; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|