Response.php 2.9 KB

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