0
1
Fork 0
hedera-web-mindshore/web/json-service.php

101 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2016-08-22 10:41:05 +00:00
<?php
namespace Vn\Web;
use Vn\Lib;
2022-10-05 08:49:54 +00:00
use Vn\Lib\Locale;
2016-08-22 10:41:05 +00:00
/**
* Base class for JSON application.
*/
2018-05-23 10:14:20 +00:00
class JsonService extends RestService {
2016-08-22 10:41:05 +00:00
private $warnings = NULL;
2018-05-23 10:14:20 +00:00
function run() {
ini_set('display_errors', FALSE);
set_error_handler([$this, 'errorHandler'], E_ALL);
set_exception_handler([$this, 'exceptionHandler']);
2016-08-22 10:41:05 +00:00
2018-05-23 10:14:20 +00:00
$this->init();
$this->startSession();
$this->checkVersion();
2016-08-22 10:41:05 +00:00
2018-05-23 10:14:20 +00:00
$json = $this->loadMethod(__NAMESPACE__.'\JsonRequest');
$this->replyJson($json);
2016-08-22 10:41:05 +00:00
}
2018-05-23 10:14:20 +00:00
function replyJson($jsonData) {
$reply = new JsonReply();
2016-08-22 10:41:05 +00:00
$reply->data = $jsonData;
$reply->warnings = $this->warnings;
2018-05-23 10:14:20 +00:00
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($reply);
2016-08-22 10:41:05 +00:00
}
function errorHandler($errno, $message, $file, $line, $context = null) {
2016-08-22 10:41:05 +00:00
$eUserWarn =
E_USER_NOTICE
| E_USER_WARNING
| E_USER_DEPRECATED;
$eCoreWarn =
E_NOTICE
| E_WARNING
| E_DEPRECATED;
$eWarn = $eUserWarn | $eCoreWarn;
$eUser = $eUserWarn | E_USER_ERROR;
2018-05-23 10:14:20 +00:00
$json = new JsonException();
2016-08-22 10:41:05 +00:00
if (_ENABLE_DEBUG || $errno & $eUser)
2016-08-22 10:41:05 +00:00
$json->message = $message;
else
2022-10-05 08:49:54 +00:00
$json->message = s('Something went wrong');
2016-08-22 10:41:05 +00:00
2018-05-23 10:14:20 +00:00
if (_ENABLE_DEBUG) {
2016-08-22 10:41:05 +00:00
$json->code = $errno;
$json->file = $file;
$json->line = $line;
}
2018-05-23 10:14:20 +00:00
if ($errno & $eWarn) {
if (!isset($this->warnings))
2016-08-22 10:41:05 +00:00
$this->warnings = [];
$this->warnings[] = $json;
2018-05-23 11:09:55 +00:00
} else {
2018-05-23 10:14:20 +00:00
http_response_code(500);
$this->replyJson($json);
exit();
2016-08-22 10:41:05 +00:00
}
return !($errno & $eUser);
}
2018-05-23 10:14:20 +00:00
function exceptionHandler($e) {
$json = new JsonException();
2016-08-22 10:41:05 +00:00
2018-05-23 10:14:20 +00:00
if (_ENABLE_DEBUG || $e instanceof Lib\UserException) {
$json->exception = get_class($e);
$json->message = $e->getMessage();
2018-05-23 11:09:55 +00:00
} else {
2016-08-22 10:41:05 +00:00
$json->exception = 'Exception';
2022-10-05 08:49:54 +00:00
$json->message = s('Something went wrong');
2016-08-22 10:41:05 +00:00
}
2018-05-23 10:14:20 +00:00
if (_ENABLE_DEBUG) {
$json->code = $e->getCode();
$json->file = $e->getFile();
$json->line = $e->getLine();
$json->trace = $e->getTrace();
2016-08-22 10:41:05 +00:00
}
2018-05-23 10:14:20 +00:00
$this->statusFromException($e);
$this->replyJson($json);
2016-08-22 10:41:05 +00:00
if (!($e instanceof Lib\UserException))
throw $e;
}
}