1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouter; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
use Vectorface\SnappyRouter\Di\Di; |
7
|
|
|
use Vectorface\SnappyRouter\Response\AbstractResponse; |
8
|
|
|
|
9
|
|
|
function http_response_code($code = null) |
10
|
|
|
{ |
11
|
12 |
|
if (function_exists('\http_response_code')) { |
12
|
|
|
// @codeCoverageIgnoreStart |
13
|
|
|
return \http_response_code($code); |
14
|
|
|
} |
15
|
|
|
// @codeCoverageIgnoreEnd |
16
|
|
|
|
17
|
12 |
|
$diKey = 'currentResponseCode'; |
18
|
12 |
|
if (null === $code) { |
19
|
|
|
try { |
20
|
1 |
|
return Di::getDefault()->get($diKey); |
21
|
1 |
|
} catch (Exception $e) { |
22
|
1 |
|
return Di::getDefault()->set($diKey, AbstractResponse::RESPONSE_OK)->get($diKey); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$http_status_codes = array( |
27
|
11 |
|
100 => 'Continue', |
28
|
|
|
101 => 'Switching Protocols', |
29
|
|
|
102 => 'Processing', |
30
|
|
|
200 => 'OK', |
31
|
|
|
201 => 'Created', |
32
|
|
|
202 => 'Accepted', |
33
|
|
|
203 => 'Non-Authoritative Information', |
34
|
|
|
204 => 'No Content', |
35
|
|
|
205 => 'Reset Content', |
36
|
|
|
206 => 'Partial Content', |
37
|
|
|
207 => 'Multi-Status', |
38
|
|
|
300 => 'Multiple Choices', |
39
|
|
|
301 => 'Moved Permanently', |
40
|
|
|
302 => 'Found', |
41
|
|
|
303 => 'See Other', |
42
|
|
|
304 => 'Not Modified', |
43
|
|
|
305 => 'Use Proxy', |
44
|
|
|
306 => '(Unused)', |
45
|
|
|
307 => 'Temporary Redirect', |
46
|
|
|
308 => 'Permanent Redirect', |
47
|
|
|
400 => 'Bad Request', |
48
|
|
|
401 => 'Unauthorized', |
49
|
|
|
402 => 'Payment Required', |
50
|
|
|
403 => 'Forbidden', |
51
|
|
|
404 => 'Not Found', |
52
|
|
|
405 => 'Method Not Allowed', |
53
|
|
|
406 => 'Not Acceptable', |
54
|
|
|
407 => 'Proxy Authentication Required', |
55
|
|
|
408 => 'Request Timeout', |
56
|
|
|
409 => 'Conflict', |
57
|
|
|
410 => 'Gone', |
58
|
|
|
411 => 'Length Required', |
59
|
|
|
412 => 'Precondition Failed', |
60
|
|
|
413 => 'Request Entity Too Large', |
61
|
|
|
414 => 'Request-URI Too Long', |
62
|
|
|
415 => 'Unsupported Media Type', |
63
|
|
|
416 => 'Requested Range Not Satisfiable', |
64
|
|
|
417 => 'Expectation Failed', |
65
|
|
|
418 => 'I\'m a teapot', |
66
|
|
|
419 => 'Authentication Timeout', |
67
|
|
|
420 => 'Enhance Your Calm', |
68
|
|
|
422 => 'Unprocessable Entity', |
69
|
|
|
423 => 'Locked', |
70
|
|
|
424 => 'Failed Dependency', |
71
|
|
|
424 => 'Method Failure', |
72
|
|
|
425 => 'Unordered Collection', |
73
|
|
|
426 => 'Upgrade Required', |
74
|
|
|
428 => 'Precondition Required', |
75
|
|
|
429 => 'Too Many Requests', |
76
|
|
|
431 => 'Request Header Fields Too Large', |
77
|
|
|
444 => 'No Response', |
78
|
|
|
449 => 'Retry With', |
79
|
|
|
450 => 'Blocked by Windows Parental Controls', |
80
|
|
|
451 => 'Unavailable For Legal Reasons', |
81
|
|
|
494 => 'Request Header Too Large', |
82
|
|
|
495 => 'Cert Error', |
83
|
|
|
496 => 'No Cert', |
84
|
|
|
497 => 'HTTP to HTTPS', |
85
|
|
|
499 => 'Client Closed Request', |
86
|
|
|
500 => 'Internal Server Error', |
87
|
|
|
501 => 'Not Implemented', |
88
|
|
|
502 => 'Bad Gateway', |
89
|
|
|
503 => 'Service Unavailable', |
90
|
|
|
504 => 'Gateway Timeout', |
91
|
|
|
505 => 'HTTP Version Not Supported', |
92
|
|
|
506 => 'Variant Also Negotiates', |
93
|
|
|
507 => 'Insufficient Storage', |
94
|
|
|
508 => 'Loop Detected', |
95
|
|
|
509 => 'Bandwidth Limit Exceeded', |
96
|
|
|
510 => 'Not Extended', |
97
|
|
|
511 => 'Network Authentication Required', |
98
|
|
|
598 => 'Network read timeout error', |
99
|
|
|
599 => 'Network connect timeout error' |
100
|
|
|
); |
101
|
|
|
|
102
|
11 |
|
$code = intval($code); |
103
|
11 |
|
if (!isset($http_status_codes[$code])) { |
104
|
1 |
|
throw new Exception('Invalid response code: '.$code); |
105
|
|
|
} |
106
|
10 |
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
107
|
10 |
|
@header(sprintf('%s %s %s', $protocol, $code, $http_status_codes[$code])); |
|
|
|
|
108
|
10 |
|
Di::getDefault()->set($diKey, $code); |
109
|
|
|
} |
110
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: