/** * Universal, web-agnostic response function. * * Responds with a JSON and closes the connection if used in a web request; * merely echoes the response message (but optionally exits) otherwise. * * @param type $message The message to respond with. * @param bool $error Indicates that an error has occurred * @param bool $fatal Shut down PHP thread after printing the message * @param string $shutdown Optional shutdown expression to be evaluated; it must halt the current PHP process. */ public static function respond($message, $error = false, $fatal = false) { if (self::isCli()) { // Command line interface message self::$_responding = true; echo trim($message) . "\n"; if ($error && $fatal) { self::end(); } } else { // One-off JSON response to HTTP client if (!isset(self::$_response)) { self::$_response = new ResponseUtil(array()); } // Default error code if there's an error; default/previously-set // response code otherwise. self::$_response->sendHttp($error ? self::$errorCode : null, $message, $error); } }
break; case 'sendHttp': $r = new ResponseUtil(); if (ctype_digit($subcase) || is_int($subcase)) { $r->sendHttp($subcase); } $r['message'] = 'The response'; $r['error'] = false; switch ($subcase) { case 'badCode': $r->sendHttp(666); break; case 'extraHeader': $r->httpHeader['Content-MD5'] = base64_encode(md5('not the content')); $r->sendHttp(); break; case 'raw': $r->body = 'The message in plain text.'; $r->httpHeader['Content-Type'] = 'text/plain'; $r->sendHttp(); break; } break; case 'setProperties': $r = new ResponseUtil(); $r->setProperties(array('foo' => 'bar', 'message' => 'ni')); $r->sendHttp(); break; default: die('Unknown test case.'); }