Response.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. DBG::log($e);
  42. Response::sendJsonExit($response);
  43. } catch (AlertSuccessException $e) {
  44. Http::sendHeaderByCode(200);
  45. $response['type'] = 'success';
  46. $response['msg'] = $e->getMessage();
  47. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  48. DBG::log($e);
  49. Response::sendJsonExit($response);
  50. } catch (AlertInfoException $e) {
  51. Http::sendHeaderByCode(200);
  52. $response['type'] = 'info';
  53. $response['msg'] = $e->getMessage();
  54. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  55. DBG::log($e);
  56. Response::sendJsonExit($response);
  57. } catch (AlertWarningException $e) {
  58. Http::sendHeaderByCode(200);
  59. $response['type'] = 'warning';
  60. $response['msg'] = $e->getMessage();
  61. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  62. DBG::log($e);
  63. Response::sendJsonExit($response);
  64. } catch (Exception $e) {
  65. Http::sendHeaderByCode(500);
  66. $response['type'] = 'error';
  67. $response['msg'] = $e->getMessage();
  68. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  69. DBG::log($e);
  70. Response::sendJsonExit($response);
  71. }
  72. Http::sendHeaderByCode(200);
  73. Response::sendJsonExit($response);
  74. }
  75. }