|
@@ -5,6 +5,7 @@
|
|
|
require_once('request.class.php');
|
|
|
require_once('uri.class.php');
|
|
|
require_once('events.trait.php');
|
|
|
+ require_once('App/exception.class.php');
|
|
|
class App extends Base {
|
|
|
use Events;
|
|
|
private static $apps = [];
|
|
@@ -12,9 +13,12 @@
|
|
|
private $routers;
|
|
|
private $router;
|
|
|
public $_onerror;
|
|
|
+ public $request;
|
|
|
+ public $response;
|
|
|
public function __construct(string $name, Callable $fn = null, array $events = null){
|
|
|
$this->name = $name;
|
|
|
$this->router = new Router();
|
|
|
+ $this->response = = new Response();
|
|
|
$this->routers = [];
|
|
|
$this->domains = [];
|
|
|
$this->_onerror = function($req, $res, $error){
|
|
@@ -25,15 +29,10 @@
|
|
|
'line'=>$error->getLine(),
|
|
|
'traceback'=>$error->getTrace()
|
|
|
]);
|
|
|
- if($res){
|
|
|
- $res->code(404);
|
|
|
- $res->header('Content-Type', 'application/json');
|
|
|
- $res->end($o);
|
|
|
- }else{
|
|
|
- http_response_code(404);
|
|
|
- header('Content-Type: application/json');
|
|
|
- print($o);
|
|
|
- }
|
|
|
+ $res->code(404);
|
|
|
+ $res->header('Content-Type', 'application/json');
|
|
|
+ $res->end($o);
|
|
|
+ $res->shutdown();
|
|
|
};
|
|
|
if(!is_null($events)){
|
|
|
foreach($events as $name => $handler){
|
|
@@ -77,10 +76,12 @@
|
|
|
}
|
|
|
}
|
|
|
public static function shutdown_error($error){
|
|
|
- $req = new Request(App::get_url());
|
|
|
- $res = new Response();
|
|
|
foreach(static::$apps as $k => $app){
|
|
|
- $app->onerror($req, $res, $error);
|
|
|
+ if(is_null($app->request)){
|
|
|
+ $app->$request = new Request(static::get_url(), getallheaders(), file_get_contents( 'php://input','r'));
|
|
|
+ }
|
|
|
+ $app->onerror($app->request, $app->response, $error);
|
|
|
+ $app->response->shutdown();
|
|
|
}
|
|
|
}
|
|
|
public static function get_url(){
|
|
@@ -101,8 +102,8 @@
|
|
|
if(is_null($headers)){
|
|
|
$headers = getallheaders();
|
|
|
}
|
|
|
- $res = new Response();
|
|
|
- $req = new Request($url, $headers, $data);
|
|
|
+ $res = $this->response;
|
|
|
+ $this->request = $req = new Request($url, $headers, $data);
|
|
|
if($this->fire('handle', $req, $res)){
|
|
|
$self = $this;
|
|
|
$onerror = function($res, $error) use($self){
|
|
@@ -156,16 +157,20 @@
|
|
|
$fn($this->domains[$host]);
|
|
|
return $this;
|
|
|
}
|
|
|
- public function map_domain(string $host1, string $host2){
|
|
|
- $this->domains[$host2] = $host1;
|
|
|
+ public function map_domain(string $host, $handle){
|
|
|
+ if(!is_array($handle)){
|
|
|
+ $handle = [$handle];
|
|
|
+ }
|
|
|
+ foreach($handle as $host2){
|
|
|
+ $this->domains[$host] = $host2;
|
|
|
+ }
|
|
|
return $this;
|
|
|
}
|
|
|
public function onerror(Request $req, Response $res, $error){
|
|
|
- if($this->fire('error', $error, $req, $res)){
|
|
|
- $fn = $this->_onerror;
|
|
|
- if(is_callable($fn)){
|
|
|
- $fn($req, $res, $error);
|
|
|
- }
|
|
|
+ $this->fire('error', $error);
|
|
|
+ $fn = $this->_onerror;
|
|
|
+ if(is_callable($fn)){
|
|
|
+ $fn($req, $res, $error);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -175,13 +180,6 @@
|
|
|
App::shutdown();
|
|
|
});
|
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline){
|
|
|
- App::shutdown_error([
|
|
|
- 'number'=> $errno,
|
|
|
- 'msg'=> $errstr,
|
|
|
- 'file'=> $errfile,
|
|
|
- 'line'=> $errline,
|
|
|
- 'backtrace'=> debug_backtrace(),
|
|
|
- 'included'=> get_included_files()
|
|
|
- ]);
|
|
|
+ App::shutdown_error(new App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace(), get_included_files()));
|
|
|
},E_ALL);
|
|
|
?>
|