123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- require_once('path.class.php');
- require_once('response.class.php');
- require_once('request.class.php');
- require_once('events.trait.php');
- class Router {
- use Events;
- private $_paths = [];
- private $_routers = [];
- private $_base = '/';
- private $responses = [];
- private $_handled = false;
- public function __construct(string $base = null, array $paths = null){
- if($paths != null){
- $this->paths($paths);
- }
- if($base != null){
- $this->base($base);
- }
- }
- public function __get($name){
- switch($name){
- case 'base':
- return $this->_base;
- break;
- case 'handled':
- return $this->_handled;
- break;
- }
- }
- public function __clone(){
- // No cloning
- }
- public function __destruct(){
- $this->_paths = [];
- }
- public function __toString(){
- return "[Router]";
- }
- public function base(string $base){
- $this->_base = $base;
- }
- public function url(string $url){
- return preg_replace('/(\/+)/','/',$url);
- }
- public function prefix(string $prefix, Callable $fn){
- $found = false;
- foreach($this->_routers as $k => $router){
- if($router->base == $prefix){
- $found = true;
- $fn($router);
- break;
- }
- }
- if(!$found){
- $router= new Router($prefix);
- $this->_routers[] = $router;
- $fn($router);
- }
- }
- // fn = function(response,args){}
- public function path(string $path, Callable $fn){
- $obj = false;
- foreach($this->_paths as $k => $p){
- if($p->path == $path){
- $obj = $p;
- }
- }
- if(!$obj){
- $obj = new Path($path);
- array_push($this->_paths,$obj);
- }
- return $obj->handle($fn);
- }
- public function paths(array $paths){
- foreach($paths as $path => $fn){
- $this->path($path,$fn);
- }
- }
- public function clear(){
- $this->_paths = [];
- }
- public function handle(string $path, Request $req = null,Response $res = null, Callable $fn = null, Callable $onerror = null){
- if(strpos($path, $this->base) !== false){
- $path = substr($path,strpos($path,$this->base)+strlen($this->base));
- if($path[0] != '/'){
- $path = '/'.$path;
- }
- if(is_null($req)){
- $req = new Request();
- }
- if(is_null($res)){
- $res = new Response();
- }
- if(!in_array($res,$this->responses)){
- array_push($this->responses,$res);
- }
- $this->fire('handle', $req, $res);
- $handled = false;
- foreach($this->_routers as $prefix => $router){
- $router->handle($path, $req, $res);
- $handled = $handled ||$router->handled;
- }
- ob_start();
- foreach($this->_paths as $k => $p){
- if($p->matches($path)){
- $handled = true;
- try{
- $p($req, $res, $p->args($path));
- }catch(Exception $e){
- if(!is_null($onerror)){
- $onerror($req, $res,$e);
- }else{
- throw $e;
- }
- }
- }
- }
- $this->_handled = $handled;
- if(!$handled && !is_null($fn)){
- $fn($req, $res);
- }
- $res->output .= ob_get_contents();
- ob_end_clean();
- $this->fire('afterhandle', $req, $res);
- }
- return $res;
- }
- }
- ?>
|