|
@@ -2,6 +2,7 @@
|
|
|
require_once('base.class.php');
|
|
|
require_once('router.class.php');
|
|
|
require_once('response.class.php');
|
|
|
+ require_once('request.class.php');
|
|
|
require_once('uri.class.php');
|
|
|
require_once('events.interface.php');
|
|
|
class App extends Base implements Events {
|
|
@@ -10,12 +11,12 @@
|
|
|
private $routers;
|
|
|
private $router;
|
|
|
public $_onerror;
|
|
|
- public function __construct($name){
|
|
|
+ public function __construct($name, Callable $fn = null){
|
|
|
$this->name = $name;
|
|
|
$this->router = new Router();
|
|
|
$this->routers = array();
|
|
|
$this->domains = array();
|
|
|
- $this->_onerror = function($res, $error){
|
|
|
+ $this->_onerror = function($req, $res, $error){
|
|
|
$o = json_encode(array(
|
|
|
'message'=>$error->getMessage(),
|
|
|
'code'=>$error->getCode(),
|
|
@@ -34,6 +35,9 @@
|
|
|
}
|
|
|
};
|
|
|
static::$apps[] = $this;
|
|
|
+ if(is_callable($fn)){
|
|
|
+ $fn($this);
|
|
|
+ }
|
|
|
}
|
|
|
public function __destruct(){
|
|
|
$this->routers = array();
|
|
@@ -72,33 +76,37 @@
|
|
|
public static function shutdown_error($error){
|
|
|
foreach(static::$apps as $k => $app){
|
|
|
if(is_callable($app->onerror)){
|
|
|
- $app->onerror(null, $error);
|
|
|
+ $app->onerror(null, null, $error);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- public function handle($verb, $url, $data){
|
|
|
- $res = new Response((string)new Uri($url));
|
|
|
+ public function handle($verb, $url, $data, $headers = null){
|
|
|
+ if(is_null($headers)){
|
|
|
+ $headers = getallheaders();
|
|
|
+ }
|
|
|
+ $res = new Response();
|
|
|
+ $req = new Request($url, $headers, $data);
|
|
|
$self = $this;
|
|
|
$onerror = function($res, $error) use($self){
|
|
|
- $self->onerror($res, $error);
|
|
|
+ $self->onerror($req, $res, $error);
|
|
|
};
|
|
|
$handled = false;
|
|
|
// Domain routers
|
|
|
foreach($this->domains as $host => $router){
|
|
|
if($url['host'] == $host){
|
|
|
- $router->handle($url["path"], $res, null, $onerror);
|
|
|
+ $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"], $res, null, $onerror);
|
|
|
+ $router->handle($url["path"], $req, $res, null, $onerror);
|
|
|
$handled = $handled || $router->handled;
|
|
|
}
|
|
|
// Base router for non-prefixed paths
|
|
|
- $this->router->handle($url["path"], $res, function($res, $url) use($handled, $self){
|
|
|
+ $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
|
|
|
if(!$handled){
|
|
|
- $self->onerror($res,new Error("Not Found", 404));
|
|
|
+ $self->onerror($req, $res,new Error("Not Found", 404));
|
|
|
}
|
|
|
}, $onerror);
|
|
|
return $res;
|
|
@@ -125,10 +133,10 @@
|
|
|
$fn($this->domains[$host]);
|
|
|
return $this;
|
|
|
}
|
|
|
- public function onerror($res, $error){
|
|
|
+ public function onerror($req, $res, $error){
|
|
|
$fn = $this->_onerror;
|
|
|
if(is_callable($fn)){
|
|
|
- $fn($res, $error);
|
|
|
+ $fn($req, $res, $error);
|
|
|
}
|
|
|
}
|
|
|
}
|