Skip to content

Commit dd05d00

Browse files
committed
Update Response.php
1 parent f4557be commit dd05d00

1 file changed

Lines changed: 54 additions & 27 deletions

File tree

src/Response.php

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,24 @@
3434
*
3535
* @author Ibrahim
3636
*
37-
* @version 1.0
37+
* @version 1.0.1
3838
*
3939
*/
4040
class Response {
4141
/**
4242
*
43-
* @var Closure
43+
* @var boolean
44+
*
45+
* @since 1.0.1
46+
*/
47+
private $isSent;
48+
/**
49+
*
50+
* @var array
4451
*
4552
* @since 1.0
4653
*/
47-
private $beforeSend;
54+
private $beforeSendCalls;
4855
/**
4956
*
5057
* @var string
@@ -88,6 +95,8 @@ private function __construct() {
8895
$this->body = '';
8996
$this->responseCode = 200;
9097
$this->lock = false;
98+
$this->isSent = false;
99+
$this->beforeSendCalls = [];
91100
}
92101
/**
93102
* Adds new HTTP header to the response.
@@ -128,15 +137,17 @@ public static function addHeader($headerName, $headerVal, $isReplace = false) {
128137
return $retVal;
129138
}
130139
/**
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.
132143
*
133144
* @param Closure $func A PHP callable.
134145
*
135146
* @since 1.0
136147
*/
137148
public static function beforeSend($func) {
138149
if (is_callable($func)) {
139-
self::get()->beforeSend = $func;
150+
self::get()->beforeSendCalls[] = $func;
140151
}
141152
}
142153
/**
@@ -261,6 +272,17 @@ public static function hasHeader($headerName, $headerVal = null) {
261272

262273
return count($headerValFromObj) != 0;
263274
}
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+
}
264286
/**
265287
* Removes a header from the response.
266288
*
@@ -317,36 +339,41 @@ public static function removeHeader($headerName, $headerVal = null) {
317339
* @since 1.0
318340
*/
319341
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+
}
324349

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.
327353

328-
http_response_code(self::getCode());
354+
http_response_code(self::getCode());
329355

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+
}
333360
}
334-
}
335361

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();
343369

344-
if (ob_get_level() > 0) {
345-
ob_flush();
370+
if (ob_get_level() > 0) {
371+
ob_flush();
372+
}
373+
flush();
346374
}
347-
flush();
375+
die;
348376
}
349-
die();
350377
}
351378
}
352379
/**

0 commit comments

Comments
 (0)