|
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
|
11 |
|
101 => 'Switching Protocols', |
|
29
|
11 |
|
102 => 'Processing', |
|
30
|
11 |
|
200 => 'OK', |
|
31
|
11 |
|
201 => 'Created', |
|
32
|
11 |
|
202 => 'Accepted', |
|
33
|
11 |
|
203 => 'Non-Authoritative Information', |
|
34
|
11 |
|
204 => 'No Content', |
|
35
|
11 |
|
205 => 'Reset Content', |
|
36
|
11 |
|
206 => 'Partial Content', |
|
37
|
11 |
|
207 => 'Multi-Status', |
|
38
|
11 |
|
300 => 'Multiple Choices', |
|
39
|
11 |
|
301 => 'Moved Permanently', |
|
40
|
11 |
|
302 => 'Found', |
|
41
|
11 |
|
303 => 'See Other', |
|
42
|
11 |
|
304 => 'Not Modified', |
|
43
|
11 |
|
305 => 'Use Proxy', |
|
44
|
11 |
|
306 => '(Unused)', |
|
45
|
11 |
|
307 => 'Temporary Redirect', |
|
46
|
11 |
|
308 => 'Permanent Redirect', |
|
47
|
11 |
|
400 => 'Bad Request', |
|
48
|
11 |
|
401 => 'Unauthorized', |
|
49
|
11 |
|
402 => 'Payment Required', |
|
50
|
11 |
|
403 => 'Forbidden', |
|
51
|
11 |
|
404 => 'Not Found', |
|
52
|
11 |
|
405 => 'Method Not Allowed', |
|
53
|
11 |
|
406 => 'Not Acceptable', |
|
54
|
11 |
|
407 => 'Proxy Authentication Required', |
|
55
|
11 |
|
408 => 'Request Timeout', |
|
56
|
11 |
|
409 => 'Conflict', |
|
57
|
11 |
|
410 => 'Gone', |
|
58
|
11 |
|
411 => 'Length Required', |
|
59
|
11 |
|
412 => 'Precondition Failed', |
|
60
|
11 |
|
413 => 'Request Entity Too Large', |
|
61
|
11 |
|
414 => 'Request-URI Too Long', |
|
62
|
11 |
|
415 => 'Unsupported Media Type', |
|
63
|
11 |
|
416 => 'Requested Range Not Satisfiable', |
|
64
|
11 |
|
417 => 'Expectation Failed', |
|
65
|
11 |
|
418 => 'I\'m a teapot', |
|
66
|
11 |
|
419 => 'Authentication Timeout', |
|
67
|
11 |
|
420 => 'Enhance Your Calm', |
|
68
|
11 |
|
422 => 'Unprocessable Entity', |
|
69
|
11 |
|
423 => 'Locked', |
|
70
|
11 |
|
424 => 'Failed Dependency', |
|
71
|
11 |
|
424 => 'Method Failure', |
|
72
|
11 |
|
425 => 'Unordered Collection', |
|
73
|
11 |
|
426 => 'Upgrade Required', |
|
74
|
11 |
|
428 => 'Precondition Required', |
|
75
|
11 |
|
429 => 'Too Many Requests', |
|
76
|
11 |
|
431 => 'Request Header Fields Too Large', |
|
77
|
11 |
|
444 => 'No Response', |
|
78
|
11 |
|
449 => 'Retry With', |
|
79
|
11 |
|
450 => 'Blocked by Windows Parental Controls', |
|
80
|
11 |
|
451 => 'Unavailable For Legal Reasons', |
|
81
|
11 |
|
494 => 'Request Header Too Large', |
|
82
|
11 |
|
495 => 'Cert Error', |
|
83
|
11 |
|
496 => 'No Cert', |
|
84
|
11 |
|
497 => 'HTTP to HTTPS', |
|
85
|
11 |
|
499 => 'Client Closed Request', |
|
86
|
11 |
|
500 => 'Internal Server Error', |
|
87
|
11 |
|
501 => 'Not Implemented', |
|
88
|
11 |
|
502 => 'Bad Gateway', |
|
89
|
11 |
|
503 => 'Service Unavailable', |
|
90
|
11 |
|
504 => 'Gateway Timeout', |
|
91
|
11 |
|
505 => 'HTTP Version Not Supported', |
|
92
|
11 |
|
506 => 'Variant Also Negotiates', |
|
93
|
11 |
|
507 => 'Insufficient Storage', |
|
94
|
11 |
|
508 => 'Loop Detected', |
|
95
|
11 |
|
509 => 'Bandwidth Limit Exceeded', |
|
96
|
11 |
|
510 => 'Not Extended', |
|
97
|
11 |
|
511 => 'Network Authentication Required', |
|
98
|
11 |
|
598 => 'Network read timeout error', |
|
99
|
|
|
599 => 'Network connect timeout error' |
|
100
|
11 |
|
); |
|
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
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: