Total Complexity | 9 |
Total Lines | 82 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | <?php |
||
5 | class Server |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var array |
||
10 | */ |
||
11 | protected $server = []; |
||
12 | |||
13 | /** |
||
14 | * @return Server |
||
15 | */ |
||
16 | public static function sharedInstance(): self |
||
17 | { |
||
18 | static $self; |
||
19 | if (!$self) { |
||
20 | $self = new static(); |
||
21 | } |
||
22 | return $self; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @param string $name |
||
27 | * @param null|string $default |
||
28 | * @return string |
||
29 | */ |
||
30 | public function server(string $name, ?string $default = null): ?string |
||
|
|||
31 | { |
||
32 | if (empty($this->server[$name])) { |
||
33 | $this->server[$name] = \filter_input(INPUT_SERVER, $name) ?? $default; |
||
34 | } |
||
35 | return $this->server[$name]; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param string $default |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | public function method(string $default = 'GET'): string |
||
44 | { |
||
45 | return $this->server('REQUEST_METHOD', $default); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $default |
||
50 | * @return string |
||
51 | */ |
||
52 | public function protocol(string $default = 'https'): string |
||
53 | { |
||
54 | $protocol = $this->server('HTTP_CF_VISITOR'); // cloudFlare |
||
55 | |||
56 | if ($protocol) |
||
57 | { |
||
58 | /** |
||
59 | * { scheme: "https" } |
||
60 | * |
||
61 | * @var string $protocol |
||
62 | */ |
||
63 | $protocol = json_decode($protocol, true); |
||
64 | } |
||
65 | |||
66 | return $protocol['scheme'] ?? |
||
67 | $this->server( |
||
68 | 'HTTP_X_FORWARDED_PROTO', |
||
69 | $this->server('REQUEST_SCHEME', $default) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | public function host(): string |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function path(): string |
||
87 | } |
||
88 | |||
89 | } |
||
90 |