1 | <?php |
||
12 | class Response { |
||
13 | use |
||
14 | Singleton, |
||
15 | Psr7; |
||
16 | /** |
||
17 | * Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $protocol; |
||
22 | /** |
||
23 | * HTTP status code |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | public $code; |
||
28 | /** |
||
29 | * Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, `content-type`, `accept-language` |
||
30 | * |
||
31 | * Values might be strings in case of single value or array of strings in case of multiple values with the same field name |
||
32 | * |
||
33 | * @var string[][] |
||
34 | */ |
||
35 | public $headers; |
||
36 | /** |
||
37 | * String body (is used instead of `$this->body_stream` in most cases, ignored if `$this->body_stream` is present) |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $body; |
||
42 | /** |
||
43 | * Body in form of stream (might be used instead of `$this->body` in some cases, if present, `$this->body` is ignored) |
||
44 | * |
||
45 | * Stream is read/write |
||
46 | * |
||
47 | * @var resource |
||
48 | */ |
||
49 | public $body_stream; |
||
50 | /** |
||
51 | * Initialize response object with specified data |
||
52 | * |
||
53 | * @param string $body |
||
54 | * @param null|resource|string $body_stream String, like `php://temp` or resource, like `fopen('php://temp', 'a+b')`, if present, `$body` is ignored |
||
55 | * @param string[]|string[][] $headers Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, |
||
56 | * `content-type`, `accept-language`; Values might be strings in case of single value or array of strings in case |
||
57 | * of multiple values with the same field name |
||
58 | * @param int $code HTTP status code |
||
59 | * @param string $protocol Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
60 | * |
||
61 | * @return Response |
||
62 | */ |
||
63 | function init ($body = '', $body_stream = null, $headers = [], $code = 200, $protocol = 'HTTP/1.1') { |
||
74 | /** |
||
75 | * Initialize with typical default settings (headers `Content-Type` and `Vary`, protocol taken from `cs\Request::$protocol`) |
||
76 | * |
||
77 | * @return Response |
||
78 | */ |
||
79 | function init_with_typical_default_settings () { |
||
80 | return $this->init( |
||
81 | '', |
||
82 | null, |
||
83 | [ |
||
84 | 'Content-Type' => 'text/html; charset=utf-8', |
||
85 | 'Vary' => 'Accept-Language,User-Agent,Cookie' |
||
86 | ], |
||
87 | 200, |
||
88 | Request::instance()->protocol |
||
89 | ); |
||
90 | } |
||
91 | /** |
||
92 | * Set raw HTTP header |
||
93 | * |
||
94 | * @param string $field Field |
||
95 | * @param string $value Value, empty string will cause header removal |
||
96 | * @param bool $replace The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header |
||
97 | * of the same type. By default it will replace |
||
98 | * |
||
99 | * @return Response |
||
100 | */ |
||
101 | function header ($field, $value, $replace = true) { |
||
112 | /** |
||
113 | * Make redirect to specified location |
||
114 | * |
||
115 | * @param string $location |
||
116 | * @param int $code |
||
117 | * |
||
118 | * @return Response |
||
119 | */ |
||
120 | function redirect ($location, $code = 302) { |
||
126 | /** |
||
127 | * Function for setting cookies, taking into account cookies prefix. Parameters like in system `setcookie()` function, but `$path`, `$domain` and `$secure` |
||
128 | * are skipped, they are detected automatically |
||
129 | * |
||
130 | * This function have side effect of setting cookie on `cs\Request` object |
||
131 | * |
||
132 | * @param string $name |
||
133 | * @param string $value |
||
134 | * @param int $expire |
||
135 | * @param bool $httponly |
||
136 | * |
||
137 | * @return Response |
||
138 | */ |
||
139 | function cookie ($name, $value, $expire = 0, $httponly = false) { |
||
174 | /** |
||
175 | * Provides default output for all the response data using `header()`, `http_response_code()` and `echo` or `php://output` |
||
176 | */ |
||
177 | function output_default () { |
||
215 | } |
||
216 |