1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This class used to override some header*() functions and http_response_code() |
4
|
|
|
* |
5
|
|
|
* We put these into the Yii namespace, so that Yiisoft\Yii\Web\Emitter will use these versions of header*() and |
6
|
|
|
* http_response_code() when we test its output. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Yiisoft\Yii\Web\Tests\Emitter\Support; |
12
|
|
|
|
13
|
|
|
class HTTPFunctions |
14
|
|
|
{ |
15
|
|
|
/** @var string[][] */ |
16
|
|
|
private static $headers = []; |
17
|
|
|
/** @var int */ |
18
|
|
|
private static $responseCode = 200; |
19
|
|
|
private static $headersSent = false; |
20
|
|
|
private static string $headersSentFile = ''; |
21
|
|
|
private static int $headersSentLine = 0; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Reset state |
25
|
|
|
*/ |
26
|
|
|
public static function reset(): void |
27
|
|
|
{ |
28
|
|
|
self::$headers = []; |
29
|
|
|
self::$responseCode = 200; |
30
|
|
|
self::$headersSent = false; |
31
|
|
|
self::$headersSentFile = ''; |
32
|
|
|
self::$headersSentLine = 0; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set header_sent() state |
37
|
|
|
*/ |
38
|
|
|
public static function set_headers_sent(bool $value = false, string $file = '', int $line = 0): void |
39
|
|
|
{ |
40
|
|
|
static::$headersSent = $value; |
|
|
|
|
41
|
|
|
static::$headersSentFile = $file; |
|
|
|
|
42
|
|
|
static::$headersSentLine = $line; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Check if headers have been sent |
47
|
|
|
*/ |
48
|
|
|
public static function headers_sent(&$file = null, &$line = null): bool |
49
|
|
|
{ |
50
|
|
|
$file = static::$headersSentFile; |
|
|
|
|
51
|
|
|
$line = static::$headersSentLine; |
|
|
|
|
52
|
|
|
return static::$headersSent; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Send a raw HTTP header |
57
|
|
|
*/ |
58
|
|
|
public static function header(string $string, bool $replace = true, ?int $http_response_code = null): void |
59
|
|
|
{ |
60
|
|
|
if (strpos($string, 'HTTP/') !== 0) { |
61
|
|
|
$header = strtolower(explode(':', $string, 2)[0]); |
62
|
|
|
if ($replace || !array_key_exists($header, self::$headers)) { |
63
|
|
|
self::$headers[$header] = []; |
64
|
|
|
} |
65
|
|
|
self::$headers[$header][] = $string; |
66
|
|
|
} |
67
|
|
|
if ($http_response_code !== null) { |
68
|
|
|
self::$responseCode = $http_response_code; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Remove previously set headers |
74
|
|
|
*/ |
75
|
|
|
public static function header_remove(?string $header = null): void |
76
|
|
|
{ |
77
|
|
|
if ($header === null) { |
78
|
|
|
self::$headers = []; |
79
|
|
|
} else { |
80
|
|
|
unset(self::$headers[strtolower($header)]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns a list of response headers sent |
86
|
|
|
* |
87
|
|
|
* @return string[] |
88
|
|
|
*/ |
89
|
|
|
public static function headers_list(): array |
90
|
|
|
{ |
91
|
|
|
$result = []; |
92
|
|
|
foreach (self::$headers as $values) { |
93
|
|
|
foreach ($values as $header) { |
94
|
|
|
$result[] = $header; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get or Set the HTTP response code |
102
|
|
|
*/ |
103
|
|
|
public static function http_response_code(?int $response_code = null): int |
104
|
|
|
{ |
105
|
|
|
if ($response_code !== null) { |
106
|
|
|
self::$responseCode = $response_code; |
107
|
|
|
} |
108
|
|
|
return self::$responseCode; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Check header is exists |
113
|
|
|
*/ |
114
|
|
|
public static function hasHeader(string $header): bool |
115
|
|
|
{ |
116
|
|
|
return key_exists(strtolower($header), self::$headers); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|