|
34 | 34 | * |
35 | 35 | * @author Ibrahim |
36 | 36 | * |
37 | | - * @version 1.0 |
| 37 | + * @version 1.0.1 |
38 | 38 | * |
39 | 39 | */ |
40 | 40 | class Response { |
41 | 41 | /** |
42 | 42 | * |
43 | | - * @var Closure |
| 43 | + * @var boolean |
| 44 | + * |
| 45 | + * @since 1.0.1 |
| 46 | + */ |
| 47 | + private $isSent; |
| 48 | + /** |
| 49 | + * |
| 50 | + * @var array |
44 | 51 | * |
45 | 52 | * @since 1.0 |
46 | 53 | */ |
47 | | - private $beforeSend; |
| 54 | + private $beforeSendCalls; |
48 | 55 | /** |
49 | 56 | * |
50 | 57 | * @var string |
@@ -88,6 +95,8 @@ private function __construct() { |
88 | 95 | $this->body = ''; |
89 | 96 | $this->responseCode = 200; |
90 | 97 | $this->lock = false; |
| 98 | + $this->isSent = false; |
| 99 | + $this->beforeSendCalls = []; |
91 | 100 | } |
92 | 101 | /** |
93 | 102 | * Adds new HTTP header to the response. |
@@ -128,15 +137,17 @@ public static function addHeader($headerName, $headerVal, $isReplace = false) { |
128 | 137 | return $retVal; |
129 | 138 | } |
130 | 139 | /** |
131 | | - * Sets a function to execute before sending the final response. |
| 140 | + * Adds a function to execute before sending the final response. |
| 141 | + * |
| 142 | + * This method can be used to add more than one callback. |
132 | 143 | * |
133 | 144 | * @param Closure $func A PHP callable. |
134 | 145 | * |
135 | 146 | * @since 1.0 |
136 | 147 | */ |
137 | 148 | public static function beforeSend($func) { |
138 | 149 | if (is_callable($func)) { |
139 | | - self::get()->beforeSend = $func; |
| 150 | + self::get()->beforeSendCalls[] = $func; |
140 | 151 | } |
141 | 152 | } |
142 | 153 | /** |
@@ -261,6 +272,17 @@ public static function hasHeader($headerName, $headerVal = null) { |
261 | 272 |
|
262 | 273 | return count($headerValFromObj) != 0; |
263 | 274 | } |
| 275 | + /** |
| 276 | + * Checks if the response was sent or not. |
| 277 | + * |
| 278 | + * @return boolean The method will return true if output is sent. False |
| 279 | + * if not. |
| 280 | + * |
| 281 | + * @since 1.0.1 |
| 282 | + */ |
| 283 | + public static function isSent() { |
| 284 | + return self::get()->isSent; |
| 285 | + } |
264 | 286 | /** |
265 | 287 | * Removes a header from the response. |
266 | 288 | * |
@@ -317,36 +339,41 @@ public static function removeHeader($headerName, $headerVal = null) { |
317 | 339 | * @since 1.0 |
318 | 340 | */ |
319 | 341 | public static function send() { |
320 | | - if (self::get()->beforeSend !== null && !self::get()->lock) { |
321 | | - self::get()->lock = true; |
322 | | - call_user_func(self::get()->beforeSend); |
323 | | - } |
| 342 | + if (!self::isSent()) { |
| 343 | + if (!self::get()->lock) { |
| 344 | + self::get()->lock = true; |
| 345 | + foreach (self::get()->beforeSendCalls as $func) { |
| 346 | + call_user_func($func); |
| 347 | + } |
| 348 | + } |
324 | 349 |
|
325 | | - if (!(http_response_code() === false)) { |
326 | | - // Send response only in non-cli environment. |
| 350 | + if (!(http_response_code() === false)) { |
| 351 | + self::get()->isSent = true; |
| 352 | + // Send response only in non-cli environment. |
327 | 353 |
|
328 | | - http_response_code(self::getCode()); |
| 354 | + http_response_code(self::getCode()); |
329 | 355 |
|
330 | | - foreach (self::getHeaders() as $headerName => $headerVals) { |
331 | | - foreach ($headerVals as $headerVal) { |
332 | | - header($headerName.': '.$headerVal, false); |
| 356 | + foreach (self::getHeaders() as $headerName => $headerVals) { |
| 357 | + foreach ($headerVals as $headerVal) { |
| 358 | + header($headerName.': '.$headerVal, false); |
| 359 | + } |
333 | 360 | } |
334 | | - } |
335 | 361 |
|
336 | | - if (is_callable('fastcgi_finish_request')) { |
337 | | - echo self::getBody(); |
338 | | - fastcgi_finish_request(); |
339 | | - } else { |
340 | | - ob_start(); |
341 | | - echo self::getBody(); |
342 | | - ob_end_flush(); |
| 362 | + if (is_callable('fastcgi_finish_request')) { |
| 363 | + echo self::getBody(); |
| 364 | + fastcgi_finish_request(); |
| 365 | + } else { |
| 366 | + ob_start(); |
| 367 | + echo self::getBody(); |
| 368 | + ob_end_flush(); |
343 | 369 |
|
344 | | - if (ob_get_level() > 0) { |
345 | | - ob_flush(); |
| 370 | + if (ob_get_level() > 0) { |
| 371 | + ob_flush(); |
| 372 | + } |
| 373 | + flush(); |
346 | 374 | } |
347 | | - flush(); |
| 375 | + die; |
348 | 376 | } |
349 | | - die(); |
350 | 377 | } |
351 | 378 | } |
352 | 379 | /** |
|
0 commit comments