|
@@ -63,7 +63,6 @@
|
|
$fn($router);
|
|
$fn($router);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
public function path(string $path, callable $fn){
|
|
public function path(string $path, callable $fn){
|
|
$obj = false;
|
|
$obj = false;
|
|
foreach($this->_paths as $k => $p){
|
|
foreach($this->_paths as $k => $p){
|
|
@@ -73,14 +72,91 @@
|
|
}
|
|
}
|
|
if(!$obj){
|
|
if(!$obj){
|
|
$obj = new Path($path);
|
|
$obj = new Path($path);
|
|
- array_push($this->_paths,$obj);
|
|
+ array_push($this->_paths, $obj);
|
|
}
|
|
}
|
|
- return $obj->handle($fn);
|
|
+ $obj->handle($fn);
|
|
|
|
+ return $this;
|
|
|
|
+ }
|
|
|
|
+ public function get(string $path, callable $fn){
|
|
|
|
+ return $this->path($path, function($req, $res, $args) use($fn){
|
|
|
|
+ if($req->verb === 'GET'){
|
|
|
|
+ return $fn($req, $res, $args);
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ public function post(string $path, callable $fn){
|
|
|
|
+ return $this->path($path, function($req, $res, $args) use($fn){
|
|
|
|
+ if($req->verb === 'POST'){
|
|
|
|
+ return $fn($req, $res, $args);
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ public function put(string $path, callable $fn){
|
|
|
|
+ return $this->path($path, function($req, $res, $args) use($fn){
|
|
|
|
+ if($req->verb === 'PUT'){
|
|
|
|
+ return $fn($req, $res, $args);
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ public function delete(string $path, callable $fn){
|
|
|
|
+ return $this->path($path, function($req, $res, $args) use($fn){
|
|
|
|
+ if($req->verb === 'DELETE'){
|
|
|
|
+ return $fn($req, $res, $args);
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ public function patch(string $path, callable $fn){
|
|
|
|
+ return $this->path($path, function($req, $res, $args) use($fn){
|
|
|
|
+ if($req->verb === 'PATCH'){
|
|
|
|
+ return $fn($req, $res, $args);
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
public function paths(array $paths){
|
|
public function paths(array $paths){
|
|
foreach($paths as $path => $fn){
|
|
foreach($paths as $path => $fn){
|
|
- $this->path($path,$fn);
|
|
+ $this->path($path, $fn);
|
|
|
|
+ }
|
|
|
|
+ return $this;
|
|
|
|
+ }
|
|
|
|
+ public function gets(array $paths){
|
|
|
|
+ foreach($paths as $path => $fn){
|
|
|
|
+ $this->get($path, $fn);
|
|
}
|
|
}
|
|
|
|
+ return $this;
|
|
|
|
+ }
|
|
|
|
+ public function posts(array $paths){
|
|
|
|
+ foreach($paths as $path => $fn){
|
|
|
|
+ $this->post($path, $fn);
|
|
|
|
+ }
|
|
|
|
+ return $this;
|
|
|
|
+ }
|
|
|
|
+ public function puts(array $paths){
|
|
|
|
+ foreach($paths as $path => $fn){
|
|
|
|
+ $this->put($path, $fn);
|
|
|
|
+ }
|
|
|
|
+ return $this;
|
|
|
|
+ }
|
|
|
|
+ public function deletes(array $paths){
|
|
|
|
+ foreach($paths as $path => $fn){
|
|
|
|
+ $this->delete($path, $fn);
|
|
|
|
+ }
|
|
|
|
+ return $this;
|
|
|
|
+ }
|
|
|
|
+ public function patches(array $paths){
|
|
|
|
+ foreach($paths as $path => $fn){
|
|
|
|
+ $this->patch($path, $fn);
|
|
|
|
+ }
|
|
|
|
+ return $this;
|
|
}
|
|
}
|
|
public function clear(){
|
|
public function clear(){
|
|
$this->_paths = [];
|
|
$this->_paths = [];
|
|
@@ -92,7 +168,7 @@
|
|
$path = '/'.$path;
|
|
$path = '/'.$path;
|
|
}
|
|
}
|
|
if(is_null($req)){
|
|
if(is_null($req)){
|
|
- $req = new Request();
|
|
+ $req = new Request(Request::get_verb(), Request::get_url(), Request::get_headers(), Request::get_body());
|
|
}
|
|
}
|
|
if(is_null($res)){
|
|
if(is_null($res)){
|
|
$res = new Response();
|
|
$res = new Response();
|
|
@@ -111,7 +187,9 @@
|
|
if($p->matches($path)){
|
|
if($p->matches($path)){
|
|
$handled = true;
|
|
$handled = true;
|
|
try{
|
|
try{
|
|
- $p($req, $res, $p->args($path));
|
|
+ if($p($req, $res, $p->args($path)) === false){
|
|
|
|
+ $handled = false;
|
|
|
|
+ }
|
|
}catch(\Exception $e){
|
|
}catch(\Exception $e){
|
|
if(!is_null($onerror)){
|
|
if(!is_null($onerror)){
|
|
$onerror($req, $res,$e);
|
|
$onerror($req, $res,$e);
|