1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlugHttp\Globals; |
4
|
|
|
|
5
|
|
|
use PlugHttp\Utils\ArrayUtil; |
6
|
|
|
|
7
|
|
|
class Server |
8
|
|
|
{ |
9
|
|
|
private array $server; |
10
|
|
|
|
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
$this->server = $_SERVER; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
private function getHeadersFromHeadersList() |
17
|
|
|
{ |
18
|
|
|
return headers_list(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function clearHeadersFromHeadersList($headers, $needle) |
22
|
|
|
{ |
23
|
|
|
foreach ($headers as $header) { |
24
|
|
|
if(stripos($header,$needle) !== false) { |
25
|
|
|
$headerParts = explode(':',$header); |
26
|
|
|
return trim($headerParts[1]); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function getContentTypeFromHeadersList($needle) |
32
|
|
|
{ |
33
|
|
|
$contentType = $this->getHeadersFromHeadersList(); |
34
|
|
|
return $this->clearHeadersFromHeadersList($contentType, $needle); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getContentType() |
38
|
|
|
{ |
39
|
|
|
$contentType = $this->getContentTypeFromHeadersList('Content-Type'); |
40
|
|
|
return $this->server['CONTENT_TYPE'] ?? $contentType; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function isMethod(string $method): bool |
44
|
|
|
{ |
45
|
|
|
return $this->method() === $method; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function method(): string |
49
|
|
|
{ |
50
|
|
|
return parse_url($this->server['REQUEST_METHOD'], PHP_URL_PATH); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getUrl(): string |
54
|
|
|
{ |
55
|
|
|
$url = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
56
|
|
|
|
57
|
|
|
if (!empty($this->server['REDIRECT_BASE'])) { |
58
|
|
|
$url = str_replace($this->server['REDIRECT_BASE'], '', $url); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $url; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getContent() |
65
|
|
|
{ |
66
|
|
|
if (!empty($_POST)) { |
67
|
|
|
return $_POST; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return file_get_contents("php://input"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function get(string $key) |
74
|
|
|
{ |
75
|
|
|
return $this->server[$key]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function all(): array |
79
|
|
|
{ |
80
|
|
|
return $this->server; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function except(array $keys): array |
84
|
|
|
{ |
85
|
|
|
return ArrayUtil::except($this->server, $keys); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function only(array $keys): array |
89
|
|
|
{ |
90
|
|
|
return ArrayUtil::only($this->server, $keys); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function has(string $key): bool |
94
|
|
|
{ |
95
|
|
|
return !empty($this->server[$key]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function add($key, $value): void |
99
|
|
|
{ |
100
|
|
|
$this->server[$key] = $value; |
101
|
|
|
|
102
|
|
|
$this->set($key, $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function remove(string $key): Server |
106
|
|
|
{ |
107
|
|
|
unset($this->server[$key]); |
108
|
|
|
|
109
|
|
|
unset($_SERVER[$key]); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function set(string $key, $value) |
115
|
|
|
{ |
116
|
|
|
$_SERVER[$key] = $value; |
117
|
|
|
} |
118
|
|
|
} |