hedera-web/vn/web/rest-service.php

81 lines
1.4 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
namespace Vn\Web;
require_once (__DIR__.'/service.php');
2016-08-25 10:47:09 +00:00
require_once (__DIR__.'/rest-request.php');
2016-07-22 20:00:27 +00:00
2016-08-22 10:41:05 +00:00
use Vn\Lib;
2016-07-22 20:00:27 +00:00
/**
2016-08-22 10:41:05 +00:00
* Base class for REST application.
2016-07-22 20:00:27 +00:00
**/
class RestService extends Service
2016-07-22 20:00:27 +00:00
{
function run ()
{
2016-08-22 10:41:05 +00:00
ini_set ('display_errors', _DEBUG_MODE);
set_error_handler ([$this, 'errorHandler'], E_ALL);
set_exception_handler ([$this, 'exceptionHandler']);
2016-07-22 20:00:27 +00:00
$this->startSession ();
$method = $this->app->loadMethod (
$_REQUEST['method'], __NAMESPACE__.'\RestRequest', './rest');
2016-09-06 14:25:02 +00:00
$method->runRest ();
2016-08-22 10:41:05 +00:00
}
2016-07-22 20:00:27 +00:00
2016-08-22 10:41:05 +00:00
/**
* Deinitializes the Application. When init method is called, this
* function is called automatically at the end of the script .
**/
function deinit ()
2016-07-22 20:00:27 +00:00
{
2016-08-22 10:41:05 +00:00
if ($this->conn)
$this->conn->query ('CALL user_session_end ()');
parent::deinit ();
}
function statusFromException ($e)
{
try {
throw $e;
2016-07-22 20:00:27 +00:00
}
2016-08-22 10:41:05 +00:00
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;
2016-07-22 20:00:27 +00:00
}
2016-08-22 10:41:05 +00:00
function exceptionHandler ($e)
2016-07-22 20:00:27 +00:00
{
2016-08-22 10:41:05 +00:00
$this->statusFromException ($e);
throw $e;
2016-07-22 20:00:27 +00:00
}
}
?>