/** * @param Request $request * @param Response $response * @param bool $autoAddReason * @throws TierException */ function sendResponse(Request $request, Response $response, $autoAddReason = true) { $statusCode = $response->getStatus(); $reason = $response->getReasonPhrase(); if ($autoAddReason && empty($reason)) { $reasonConstant = "Arya\\Reason::HTTP_{$statusCode}"; $reason = defined($reasonConstant) ? constant($reasonConstant) : ''; $response->setReasonPhrase($reason); } if ($response->hasHeader('Date') == false) { $response->addHeader("Date", gmdate("D, d M Y H:i:s", time()) . " UTC"); } $statusLine = sprintf("HTTP/%s %s", $request->getProtocol(), $statusCode); if (isset($reason[0])) { $statusLine .= " {$reason}"; } $file = null; $line = null; if (headers_sent($file, $line)) { //TODO - this is not optimal throw new TierException("Headers already sent by File " . $file . " line " . $line); } header($statusLine); $headers = $response->getAllHeaderLines(); foreach ($headers as $headerLine) { header($headerLine, $replace = false); } flush(); // Force header output $body = $response->getBody(); if (method_exists($body, '__toString')) { echo $body->__toString(); return; } else { if (is_string($body)) { echo $body; return; } elseif (is_callable($body)) { $body(); return; } } //this is bad. throw new TierException("Unknown body type."); }