1 | <?php |
||
12 | class Response { |
||
|
|||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | public $headers; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $content; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $version; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $statusCode; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $statusText; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $charset; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $file = null; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $csv = null; |
||
53 | |||
54 | /** |
||
55 | * Holds HTTP response statuses |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $statusTexts = [ |
||
60 | 200 => 'OK', |
||
61 | 302 => 'Found', |
||
62 | 400 => 'Bad Request', |
||
63 | 401 => 'Unauthorized', |
||
64 | 403 => 'Forbidden', |
||
65 | 404 => 'Not Found', |
||
66 | 500 => 'Internal Server Error' |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * Holds type key to mime type mappings for known mime types. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private $mimeTypes = [ |
||
75 | 'csv' => ['text/csv', 'application/vnd.ms-excel'], |
||
76 | 'doc' => 'application/msword', |
||
77 | 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
||
78 | 'pdf' => 'application/pdf', |
||
79 | 'zip' => 'application/zip', |
||
80 | 'ppt' => 'application/vnd.ms-powerpoint' |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Constructor. |
||
85 | * |
||
86 | * @param string $content The response content |
||
87 | * @param int $status The response status code |
||
88 | * @param array $headers An array of response headers |
||
89 | * |
||
90 | */ |
||
91 | public function __construct($content = '', $status = 200, $headers = array()){ |
||
100 | |||
101 | /** |
||
102 | * Sends HTTP headers and content. |
||
103 | * |
||
104 | */ |
||
105 | public function send(){ |
||
106 | |||
107 | $this->sendHeaders(); |
||
108 | |||
109 | if ($this->file) { |
||
110 | $this->readFile(); |
||
111 | } else if ($this->csv) { |
||
112 | $this->writeCSV(); |
||
113 | } else { |
||
114 | $this->sendContent(); |
||
115 | } |
||
116 | |||
117 | if (function_exists('fastcgi_finish_request')) { |
||
118 | fastcgi_finish_request(); |
||
119 | } elseif ('cli' !== PHP_SAPI) { |
||
120 | $this->flushBuffer(); |
||
121 | } |
||
122 | |||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Flushes output buffers. |
||
128 | * |
||
129 | */ |
||
130 | private function flushBuffer(){ |
||
134 | |||
135 | /** |
||
136 | * Sends HTTP headers. |
||
137 | * |
||
138 | * @return Response |
||
139 | */ |
||
140 | private function sendHeaders(){ |
||
163 | |||
164 | /** |
||
165 | * Sends content for the current web response. |
||
166 | * |
||
167 | * @return Response |
||
168 | */ |
||
169 | private function sendContent(){ |
||
173 | |||
174 | /** |
||
175 | * Sets content for the current web response. |
||
176 | * |
||
177 | * @param string $content The response content |
||
178 | * @return Response |
||
179 | */ |
||
180 | public function setContent($content = ""){ |
||
184 | |||
185 | /** |
||
186 | * Sets content for the current web response. |
||
187 | * |
||
188 | * @param string|null $content The response content |
||
189 | * @return Response |
||
190 | */ |
||
191 | public function type($contentType = null){ |
||
201 | |||
202 | /** |
||
203 | * Stop execution of the current script. . |
||
204 | * |
||
205 | * @param int|string $status |
||
206 | * @return void |
||
207 | * @see http://php.net/exit |
||
208 | */ |
||
209 | public function stop($status = 0){ |
||
212 | |||
213 | /** |
||
214 | * read file |
||
215 | * |
||
216 | * @return Response |
||
217 | */ |
||
218 | private function readFile(){ |
||
222 | |||
223 | /** |
||
224 | * write to CSV file |
||
225 | * |
||
226 | * @return Response |
||
227 | */ |
||
228 | private function writeCSV(){ |
||
243 | |||
244 | /** |
||
245 | * Sets the response status code & it's relevant text. |
||
246 | * |
||
247 | * @param int $code HTTP status code |
||
248 | * @return Response |
||
249 | */ |
||
250 | public function setStatusCode($code){ |
||
257 | |||
258 | /** |
||
259 | * Returns the mime type definition for an alias |
||
260 | * |
||
261 | * @param string $key |
||
262 | * @return mixed |
||
263 | */ |
||
264 | private function getMimeType($key){ |
||
272 | |||
273 | /** |
||
274 | * Clean (erase) the output buffer |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | public function clearBuffer(){ |
||
285 | |||
286 | /** |
||
287 | * download a file |
||
288 | * |
||
289 | * @param string $path |
||
290 | * @param array $file |
||
291 | * @param array $headers |
||
292 | * @return Response |
||
293 | */ |
||
294 | public function download($path, array $file, array $headers = []){ |
||
322 | |||
323 | /** |
||
324 | * download CSV file |
||
325 | * This will create csv file using $csvData |
||
326 | * |
||
327 | * @param array $csvData |
||
328 | * @param array $file |
||
329 | * @return Response |
||
330 | * @see core/Response/writeCSV() |
||
331 | */ |
||
332 | public function csv(array $csvData, array $file){ |
||
347 | } |
||
348 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.