forked from verdnatura/hedera-web
70 lines
1.4 KiB
PHP
Executable File
70 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Vn\Rest;
|
|
|
|
require_once ('vn/rest/reply.php');
|
|
require_once ('vn/rest/encoding.php');
|
|
require_once ('vn/rest/message.php');
|
|
|
|
/**
|
|
* Manages the REST service.
|
|
*
|
|
* @property Reply $reply The reply
|
|
**/
|
|
class Service
|
|
{
|
|
private static $reply = NULL;
|
|
static $encoding = Encoding::JSON;
|
|
|
|
static function init ()
|
|
{
|
|
self::$reply = new Reply ();
|
|
set_error_handler ('Vn\Rest\Service::errorHandler', E_ALL);
|
|
set_exception_handler ('Vn\Rest\Service::exceptionHandler');
|
|
}
|
|
|
|
static function setData ($data)
|
|
{
|
|
self::$reply->data = $data;
|
|
}
|
|
|
|
static function addWarning ($domain, $code, $message)
|
|
{
|
|
if (!isset (self::$reply->warnings))
|
|
self::$reply->warnings = [];
|
|
|
|
self::$reply->warnings[] =
|
|
new Message ($domain, $code, $message);
|
|
}
|
|
|
|
static function setError ($domain, $code, $message)
|
|
{
|
|
self::$reply->data = NULL;
|
|
self::$reply->error =
|
|
new Message ($domain, $code, $message);
|
|
}
|
|
|
|
static function exceptionHandler ($e)
|
|
{
|
|
// XXX: Exception trace
|
|
//$trace = array_pop (debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS, 2));
|
|
|
|
self::setError ('PHP', 'exception', $e->getMessage ());
|
|
self::sendReply ();
|
|
}
|
|
|
|
static function errorHandler ($code, $message, $file, $line, $context)
|
|
{
|
|
self::setError ('PHP', 'error', "$file:$line:$message");
|
|
return TRUE;
|
|
}
|
|
|
|
static function sendReply ()
|
|
{
|
|
header ('Content-Type: application/json; charset=UTF-8');
|
|
echo json_encode (self::$reply);
|
|
}
|
|
}
|
|
|
|
?>
|