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