Response.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @example: Response::sendJson($data);
  4. * @example: Response::sendPlainText($data);
  5. * @example: Response::sendJsonExit($data);
  6. * @example: Response::sendPlainTextExit($data);
  7. * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args);
  8. * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
  9. */
  10. class Response {
  11. public static function sendJson($data) {
  12. header("Content-type: application/json");
  13. echo json_encode($data);
  14. }
  15. public static function sendPlainText($data) {
  16. header("Content-type: text/plain");
  17. if (!is_scalar($data)) throw new Exception("Wrong data type - only scalar values allowed");
  18. echo $data;
  19. }
  20. public static function sendJsonExit($data) {
  21. self::sendJson($data);
  22. exit;
  23. }
  24. public static function sendPlainTextExit($data) {
  25. self::sendPlainText($data);
  26. exit;
  27. }
  28. public static function sendTryCatchJson($callback, $args = array()) {// @param callback must return $response object to send as json or throw exception
  29. $response = array();
  30. try {
  31. if (!is_callable($callback)) throw new Exception("#1L" . __LINE__ . " Callback is not callable");
  32. if ('JSON_FROM_REQUEST_BODY' == $args) $args = Request::getRequestJson();
  33. $response = call_user_func($callback, $args);
  34. } catch (AlertDangerException $e) {
  35. Http::sendHeaderByCode(200);
  36. $response['type'] = 'error';
  37. $response['msg'] = $e->getMessage();
  38. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  39. Response::sendJsonExit($response);
  40. } catch (AlertSuccessException $e) {
  41. Http::sendHeaderByCode(200);
  42. $response['type'] = 'success';
  43. $response['msg'] = $e->getMessage();
  44. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  45. Response::sendJsonExit($response);
  46. } catch (AlertInfoException $e) {
  47. Http::sendHeaderByCode(200);
  48. $response['type'] = 'info';
  49. $response['msg'] = $e->getMessage();
  50. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  51. Response::sendJsonExit($response);
  52. } catch (AlertWarningException $e) {
  53. Http::sendHeaderByCode(200);
  54. $response['type'] = 'warning';
  55. $response['msg'] = $e->getMessage();
  56. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  57. Response::sendJsonExit($response);
  58. } catch (Exception $e) {
  59. Http::sendHeaderByCode(500);
  60. $response['type'] = 'error';
  61. $response['msg'] = $e->getMessage();
  62. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  63. Response::sendJsonExit($response);
  64. }
  65. Http::sendHeaderByCode(200);
  66. Response::sendJsonExit($response);
  67. }
  68. }