2016-08-22 10:41:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
|
|
|
use Vn\Lib;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for JSON application.
|
2017-11-29 10:01:48 +00:00
|
|
|
*/
|
2016-08-26 12:43:45 +00:00
|
|
|
class JsonService extends RestService
|
2016-08-22 10:41:05 +00:00
|
|
|
{
|
|
|
|
private $warnings = NULL;
|
|
|
|
|
|
|
|
function run ()
|
|
|
|
{
|
|
|
|
ini_set ('display_errors', FALSE);
|
|
|
|
set_error_handler ([$this, 'errorHandler'], E_ALL);
|
|
|
|
set_exception_handler ([$this, 'exceptionHandler']);
|
|
|
|
|
2017-11-29 10:01:48 +00:00
|
|
|
$this->init ();
|
2016-08-22 10:41:05 +00:00
|
|
|
$this->startSession ();
|
2017-11-29 10:01:48 +00:00
|
|
|
$this->checkVersion ();
|
2016-08-22 10:41:05 +00:00
|
|
|
|
2016-09-23 22:47:34 +00:00
|
|
|
$json = $this->loadMethod (__NAMESPACE__.'\JsonRequest');
|
2016-08-22 10:41:05 +00:00
|
|
|
$this->replyJson ($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
function replyJson ($jsonData)
|
|
|
|
{
|
|
|
|
$reply = new JsonReply ();
|
|
|
|
$reply->data = $jsonData;
|
|
|
|
$reply->warnings = $this->warnings;
|
|
|
|
|
|
|
|
header ('Content-Type: application/json; charset=UTF-8');
|
|
|
|
echo json_encode ($reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorHandler ($errno, $message, $file, $line, $context)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
|
|
|
|
$json = new JsonException ();
|
|
|
|
|
2016-11-14 09:47:39 +00:00
|
|
|
if (_ENABLE_DEBUG || $errno & $eUser)
|
2016-08-22 10:41:05 +00:00
|
|
|
$json->message = $message;
|
|
|
|
else
|
2016-10-20 08:37:08 +00:00
|
|
|
$json->message = s('Something went wrong');
|
2016-08-22 10:41:05 +00:00
|
|
|
|
2016-11-14 09:47:39 +00:00
|
|
|
if (_ENABLE_DEBUG)
|
2016-08-22 10:41:05 +00:00
|
|
|
{
|
|
|
|
$json->code = $errno;
|
|
|
|
$json->file = $file;
|
|
|
|
$json->line = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($errno & $eWarn)
|
|
|
|
{
|
|
|
|
if (!isset ($this->warnings))
|
|
|
|
$this->warnings = [];
|
|
|
|
|
|
|
|
$this->warnings[] = $json;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
http_response_code (500);
|
|
|
|
$this->replyJson ($json);
|
|
|
|
exit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return !($errno & $eUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exceptionHandler ($e)
|
|
|
|
{
|
|
|
|
$json = new JsonException ();
|
|
|
|
|
2016-11-14 09:47:39 +00:00
|
|
|
if (_ENABLE_DEBUG || $e instanceof Lib\UserException)
|
2016-08-22 10:41:05 +00:00
|
|
|
{
|
|
|
|
$json->exception = get_class ($e);
|
|
|
|
$json->message = $e->getMessage ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$json->exception = 'Exception';
|
2016-10-20 08:37:08 +00:00
|
|
|
$json->message = s('Something went wrong');
|
2016-08-22 10:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 09:47:39 +00:00
|
|
|
if (_ENABLE_DEBUG)
|
2016-08-22 10:41:05 +00:00
|
|
|
{
|
|
|
|
$json->code = $e->getCode ();
|
|
|
|
$json->file = $e->getFile ();
|
|
|
|
$json->line = $e->getLine ();
|
2016-11-14 09:47:39 +00:00
|
|
|
$json->trace = $e->getTrace ();
|
2016-08-22 10:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->statusFromException ($e);
|
|
|
|
$this->replyJson ($json);
|
|
|
|
|
|
|
|
if (!($e instanceof Lib\UserException))
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|