14
14
*/
15
15
class Headers implements HeadersInterface {
16
16
17
+ public const APACHE = 'Apache ' ;
18
+
17
19
/**
18
20
* @var array Заголовки
19
21
*/
20
22
private array $ headers ;
23
+ /**
24
+ * @var bool Является ли сервер Apache
25
+ */
26
+ private bool $ isApache ;
21
27
22
28
public function __construct () {
29
+ $ this ->isApache = array_key_exists ('SERVER_SOFTWARE ' , $ _SERVER ) && $ _SERVER ['SERVER_SOFTWARE ' ] === self ::APACHE ;
23
30
$ this ->headers = $ this ->getAllHeaders ();
24
31
}
25
32
33
+ /**
34
+ * Возвращает, является ли сервер Apache
35
+ *
36
+ * @return bool
37
+ */
38
+ public function getIsApache ():bool {
39
+ return $ this ->isApache ;
40
+ }
41
+
26
42
/**
27
43
* Получает все заголовки методами apache и nginx
28
44
*
29
45
* @return array
30
46
*/
31
- private function getAllHeaders (): array {
32
- if (function_exists ('getallheaders ' )) {
47
+ public function getAllHeaders (): array {
48
+ if ($ this -> isApache && function_exists ('getallheaders ' )) {
33
49
return getallheaders () !== false ? getallheaders () : [];
34
50
}
35
51
@@ -48,25 +64,10 @@ private function getAllHeaders(): array {
48
64
return $ headers ;
49
65
}
50
66
51
- /**
52
- * @inheritDoc
53
- */
54
- public function set (array $ params ): void {
55
- $ this ->getAll ();
56
-
57
- foreach ($ params as $ header => $ value ) {
58
- $ this ->headers [$ header ] = $ value ;
59
- }
60
-
61
- $ this ->add ($ params );
62
- }
63
-
64
67
/**
65
68
* @inheritDoc
66
69
*/
67
70
public function getAll (): array {
68
- $ this ->headers = !empty ($ this ->headers ) ? $ this ->headers : $ this ->getAllHeaders ();
69
-
70
71
return $ this ->headers ;
71
72
}
72
73
@@ -78,16 +79,14 @@ public function add(array $params): void {
78
79
$ headerExists = array_key_exists ($ header , $ this ->headers );
79
80
$ this ->headers [$ header ] = $ value ;
80
81
81
- header ("{ $ header} : { $ value} " , $ headerExists );
82
+ header ("$ header: $ value " , $ headerExists );
82
83
}
83
84
}
84
85
85
86
/**
86
87
* @inheritDoc
87
88
*/
88
89
public function remove (string $ key ): void {
89
- $ this ->getAll ();
90
-
91
90
unset($ this ->headers [$ key ]);
92
91
header_remove ($ key );
93
92
}
@@ -116,8 +115,6 @@ public function get(string $key): string {
116
115
* @inheritDoc
117
116
*/
118
117
public function has (string $ key ): bool {
119
- $ this ->getAll ();
120
-
121
118
return array_key_exists ($ key , $ this ->headers );
122
119
}
123
120
}
0 commit comments