|
@@ -47,7 +47,7 @@
|
|
}
|
|
}
|
|
public function __destruct(){
|
|
public function __destruct(){
|
|
$this->routers = [];
|
|
$this->routers = [];
|
|
- $index = array_search(static::$apps, $this);
|
|
|
|
|
|
+ $index = array_search($this, static::$apps);
|
|
if($index !== false){
|
|
if($index !== false){
|
|
array_splice(static::$apps, $index, 1);
|
|
array_splice(static::$apps, $index, 1);
|
|
}
|
|
}
|
|
@@ -65,18 +65,7 @@
|
|
}
|
|
}
|
|
public static function shutdown(){
|
|
public static function shutdown(){
|
|
$verb = $_SERVER['REQUEST_METHOD'];
|
|
$verb = $_SERVER['REQUEST_METHOD'];
|
|
- if(isset($_SERVER['REDIRECT_URL'])){
|
|
|
|
- $url = 'REDIRECT_URL';
|
|
|
|
- }else{
|
|
|
|
- $url = 'REQUEST_URI';
|
|
|
|
- }
|
|
|
|
- if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
|
|
|
|
- $root = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
|
|
|
|
- }else{
|
|
|
|
- $root = !empty($_SERVER['HTTPS']) ? "https://" : "http://";
|
|
|
|
- }
|
|
|
|
- // @todo - determine if http or http and also check cloudflare
|
|
|
|
- $url = parse_url($root.$_SERVER['HTTP_HOST'].$_SERVER[$url]);
|
|
|
|
|
|
+ $url = App::get_url();
|
|
$data = file_get_contents( 'php://input','r');
|
|
$data = file_get_contents( 'php://input','r');
|
|
foreach(static::$apps as $k => $app){
|
|
foreach(static::$apps as $k => $app){
|
|
if($app instanceof App){
|
|
if($app instanceof App){
|
|
@@ -88,46 +77,61 @@
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static function shutdown_error($error){
|
|
public static function shutdown_error($error){
|
|
|
|
+ $req = new Request(App::get_url());
|
|
|
|
+ $res = new Response();
|
|
foreach(static::$apps as $k => $app){
|
|
foreach(static::$apps as $k => $app){
|
|
- if(is_callable($app->onerror)){
|
|
|
|
- $app->onerror(null, null, $error);
|
|
|
|
- }
|
|
|
|
|
|
+ $app->onerror($req, $res, $error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ public static function get_url(){
|
|
|
|
+ if(isset($_SERVER['REDIRECT_URL'])){
|
|
|
|
+ $url = 'REDIRECT_URL';
|
|
|
|
+ }else{
|
|
|
|
+ $url = 'REQUEST_URI';
|
|
|
|
+ }
|
|
|
|
+ if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
|
|
|
|
+ $root = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
|
|
|
|
+ }else{
|
|
|
|
+ $root = !empty($_SERVER['HTTPS']) ? "https://" : "http://";
|
|
|
|
+ }
|
|
|
|
+ // @todo - determine if http or http and also check cloudflare
|
|
|
|
+ return parse_url($root.$_SERVER['HTTP_HOST'].$_SERVER[$url]);
|
|
|
|
+ }
|
|
public function handle(string $verb, array $url, string $data, array $headers = null){
|
|
public function handle(string $verb, array $url, string $data, array $headers = null){
|
|
if(is_null($headers)){
|
|
if(is_null($headers)){
|
|
$headers = getallheaders();
|
|
$headers = getallheaders();
|
|
}
|
|
}
|
|
$res = new Response();
|
|
$res = new Response();
|
|
$req = new Request($url, $headers, $data);
|
|
$req = new Request($url, $headers, $data);
|
|
- $this->fire('handle', $req, $res);
|
|
|
|
- $self = $this;
|
|
|
|
- $onerror = function($res, $error) use($self){
|
|
|
|
- $self->onerror($req, $res, $error);
|
|
|
|
- };
|
|
|
|
- $handled = false;
|
|
|
|
- // Domain routers
|
|
|
|
- foreach($this->domains as $host => $router){
|
|
|
|
- if($url['host'] == $host){
|
|
|
|
- while(is_string($router)){
|
|
|
|
- $router = $this->domains[$router];
|
|
|
|
|
|
+ if($this->fire('handle', $req, $res)){
|
|
|
|
+ $self = $this;
|
|
|
|
+ $onerror = function($res, $error) use($self){
|
|
|
|
+ $self->onerror($req, $res, $error);
|
|
|
|
+ };
|
|
|
|
+ $handled = false;
|
|
|
|
+ // Domain routers
|
|
|
|
+ foreach($this->domains as $host => $router){
|
|
|
|
+ if($url['host'] == $host){
|
|
|
|
+ while(is_string($router)){
|
|
|
|
+ $router = $this->domains[$router];
|
|
|
|
+ }
|
|
|
|
+ $router->handle($url["path"], $req, $res, null, $onerror);
|
|
|
|
+ $handled = $handled || $router->handled;
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+ // Prefixed path routers
|
|
|
|
+ foreach($this->routers as $prefix => $router){
|
|
$router->handle($url["path"], $req, $res, null, $onerror);
|
|
$router->handle($url["path"], $req, $res, null, $onerror);
|
|
$handled = $handled || $router->handled;
|
|
$handled = $handled || $router->handled;
|
|
}
|
|
}
|
|
|
|
+ // Base router for non-prefixed paths
|
|
|
|
+ $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
|
|
|
|
+ if(!$handled){
|
|
|
|
+ $self->onerror($req, $res,new Error("Not Found", 404));
|
|
|
|
+ }
|
|
|
|
+ }, $onerror);
|
|
|
|
+ $this->fire('afterhandle', $req, $res);
|
|
}
|
|
}
|
|
- // Prefixed path routers
|
|
|
|
- foreach($this->routers as $prefix => $router){
|
|
|
|
- $router->handle($url["path"], $req, $res, null, $onerror);
|
|
|
|
- $handled = $handled || $router->handled;
|
|
|
|
- }
|
|
|
|
- // Base router for non-prefixed paths
|
|
|
|
- $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
|
|
|
|
- if(!$handled){
|
|
|
|
- $self->onerror($req, $res,new Error("Not Found", 404));
|
|
|
|
- }
|
|
|
|
- }, $onerror);
|
|
|
|
- $this->fire('afterhandle', $req, $res);
|
|
|
|
return $res;
|
|
return $res;
|
|
}
|
|
}
|
|
public function error(Callable $fn){
|
|
public function error(Callable $fn){
|
|
@@ -157,10 +161,11 @@
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
public function onerror(Request $req, Response $res, $error){
|
|
public function onerror(Request $req, Response $res, $error){
|
|
- $this->fire('error', $error);
|
|
|
|
- $fn = $this->_onerror;
|
|
|
|
- if(is_callable($fn)){
|
|
|
|
- $fn($req, $res, $error);
|
|
|
|
|
|
+ if($this->fire('error', $error, $req, $res)){
|
|
|
|
+ $fn = $this->_onerror;
|
|
|
|
+ if(is_callable($fn)){
|
|
|
|
+ $fn($req, $res, $error);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -179,17 +184,4 @@
|
|
'included'=> get_included_files()
|
|
'included'=> get_included_files()
|
|
]);
|
|
]);
|
|
},E_ALL);
|
|
},E_ALL);
|
|
- register_shutdown_function(function(){
|
|
|
|
- $error = error_get_last();
|
|
|
|
- if($error['type'] == 1){
|
|
|
|
- App::shutdown_error([
|
|
|
|
- 'number'=> $error['type'],
|
|
|
|
- 'msg'=> $error['message'],
|
|
|
|
- 'file'=> $error['file'],
|
|
|
|
- 'line'=> $error['line'],
|
|
|
|
- 'backtrace'=> [],
|
|
|
|
- 'included'=> get_included_files()
|
|
|
|
- ]);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
?>
|
|
?>
|