Total Complexity | 9 |
Total Lines | 82 |
Duplicated Lines | 0 % |
Coverage | 86.96% |
Changes | 0 |
1 | <?php |
||
5 | class Server |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var array |
||
10 | */ |
||
11 | protected $server = []; |
||
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 server(string $name, ?string $default = null): ?string |
|
|
|||
31 | { |
||
32 | 3 | if (empty($this->server[$name])) { |
|
33 | 3 | $this->server[$name] = \filter_input(INPUT_SERVER, $name) ?? $default; |
|
34 | } |
||
35 | 3 | return $this->server[$name]; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param string $default |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | 3 | public function method(string $default = 'GET'): string |
|
44 | { |
||
45 | 3 | return $this->server('REQUEST_METHOD', $default); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $default |
||
50 | * @return string |
||
51 | */ |
||
52 | 3 | public function protocol(string $default = 'https'): string |
|
53 | { |
||
54 | 3 | $protocol = $this->server('HTTP_CF_VISITOR'); // cloudFlare |
|
55 | |||
56 | 3 | if ($protocol) |
|
57 | { |
||
58 | /** |
||
59 | * { scheme: "https" } |
||
60 | * |
||
61 | * @var string $protocol |
||
62 | */ |
||
63 | $protocol = json_decode($protocol, true); |
||
64 | } |
||
65 | |||
66 | 3 | return $protocol['scheme'] ?? |
|
67 | 3 | $this->server( |
|
68 | 3 | 'HTTP_X_FORWARDED_PROTO', |
|
69 | 3 | $this->server('REQUEST_SCHEME', $default) |
|
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 3 | public function host(): string |
|
77 | { |
||
78 | 3 | return $this->server('HTTP_HOST', PHP_SAPI); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function path(): string |
||
87 | } |
||
88 | |||
89 | } |
||
90 |