forked from verdnatura/hedera-web
81 lines
1.4 KiB
PHP
81 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Vn\Web;
|
|
|
|
require_once (__DIR__.'/service.php');
|
|
require_once (__DIR__.'/rest-request.php');
|
|
|
|
use Vn\Lib;
|
|
|
|
/**
|
|
* Base class for REST application.
|
|
**/
|
|
class RestService extends Service
|
|
{
|
|
function run ()
|
|
{
|
|
ini_set ('display_errors', _DEBUG_MODE);
|
|
set_error_handler ([$this, 'errorHandler'], E_ALL);
|
|
set_exception_handler ([$this, 'exceptionHandler']);
|
|
|
|
$this->startSession ();
|
|
|
|
$method = $this->app->loadMethod (
|
|
$_REQUEST['method'], __NAMESPACE__.'\RestRequest', './rest');
|
|
$method->run ();
|
|
}
|
|
|
|
/**
|
|
* Deinitializes the Application. When init method is called, this
|
|
* function is called automatically at the end of the script .
|
|
**/
|
|
function deinit ()
|
|
{
|
|
if ($this->conn)
|
|
$this->conn->query ('CALL user_session_end ()');
|
|
|
|
parent::deinit ();
|
|
}
|
|
|
|
function statusFromException ($e)
|
|
{
|
|
try {
|
|
throw $e;
|
|
}
|
|
catch (SessionExpiredException $e)
|
|
{ $status = 401; }
|
|
catch (BadLoginException $e)
|
|
{ $status = 401; }
|
|
catch (Lib\UserException $e)
|
|
{ $status = 400; }
|
|
catch (\Exception $e)
|
|
{ $status = 500; }
|
|
|
|
http_response_code ($status);
|
|
}
|
|
|
|
function errorHandler ($errno, $message, $file, $line, $context)
|
|
{
|
|
$eFlag =
|
|
E_USER_NOTICE
|
|
| E_USER_WARNING
|
|
| E_USER_DEPRECATED
|
|
| E_NOTICE
|
|
| E_WARNING
|
|
| E_DEPRECATED;
|
|
|
|
if (!($errno & $eFlag))
|
|
http_response_code (500);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
function exceptionHandler ($e)
|
|
{
|
|
$this->statusFromException ($e);
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
?>
|