2016-07-22 20:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
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
|
|
|
**/
|
2016-08-26 12:43:45 +00:00
|
|
|
class RestService extends Service
|
2016-07-22 20:00:27 +00:00
|
|
|
{
|
|
|
|
function run ()
|
|
|
|
{
|
2016-11-14 09:47:39 +00:00
|
|
|
ini_set ('display_errors', _ENABLE_DEBUG);
|
2016-08-22 10:41:05 +00:00
|
|
|
set_error_handler ([$this, 'errorHandler'], E_ALL);
|
|
|
|
set_exception_handler ([$this, 'exceptionHandler']);
|
2016-07-22 20:00:27 +00:00
|
|
|
|
|
|
|
$this->startSession ();
|
2016-09-23 22:47:34 +00:00
|
|
|
$this->loadMethod (__NAMESPACE__.'\RestRequest');
|
2016-08-22 10:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|