| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- Lib::loadClass('Request');
- Lib::loadClass('DBG');
- /**
- * @example: Response::sendJson($data);
- * @example: Response::sendPlainText($data);
- * @example: Response::sendJsonExit($data);
- * @example: Response::sendPlainTextExit($data);
- * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args);
- * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
- */
- class Response {
- public static function sendJson($data) {
- header("Content-type: application/json");
- echo json_encode($data);
- }
- public static function sendPlainText($data) {
- header("Content-type: text/plain");
- if (!is_scalar($data)) throw new Exception("Wrong data type - only scalar values allowed");
- echo $data;
- }
- public static function sendJsonExit($data) {
- self::sendJson($data);
- exit;
- }
- public static function sendPlainTextExit($data) {
- self::sendPlainText($data);
- exit;
- }
- public static function sendTryCatchJson($callback, $args = array()) {// @param callback must return $response object to send as json or throw exception
- $response = array();
- try {
- if (!is_callable($callback)) throw new Exception("#1L" . __LINE__ . " Callback is not callable");
- if ('JSON_FROM_REQUEST_BODY' == $args) $args = Request::getRequestJson();
- $response = call_user_func($callback, $args);
- } catch (AlertDangerException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'error';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (AlertSuccessException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'success';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (AlertInfoException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'info';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (AlertWarningException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'warning';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (Exception $e) {
- Http::sendHeaderByCode(500);
- $response['type'] = 'error';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- DBG::log($e);
- Response::sendJsonExit($response);
- }
- Http::sendHeaderByCode(200);
- Response::sendJsonExit($response);
- }
- }
|