router.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Juju\App;
  3. require_once('path.class.php');
  4. $dir = dirname(__DIR__);
  5. require_once(realpath("{$dir}/events.trait.php"));
  6. $dir = "{$dir}/Http/";
  7. require_once(realpath("{$dir}/request.class.php"));
  8. require_once(realpath("{$dir}/response.class.php"));
  9. use \Juju\{Http\Request, Http\Response, Events};
  10. class Router {
  11. use Events;
  12. private $_paths = [];
  13. private $_routers = [];
  14. private $_base = '/';
  15. private $responses = [];
  16. private $_handled = false;
  17. public function __construct(string $base = null, array $paths = null){
  18. if($paths != null){
  19. $this->paths($paths);
  20. }
  21. if($base != null){
  22. $this->base($base);
  23. }
  24. }
  25. public function __get($name){
  26. switch($name){
  27. case 'base':
  28. return $this->_base;
  29. break;
  30. case 'handled':
  31. return $this->_handled;
  32. break;
  33. }
  34. }
  35. public function __clone(){
  36. // No cloning
  37. }
  38. public function __destruct(){
  39. $this->_paths = [];
  40. }
  41. public function __toString(){
  42. return "[Router]";
  43. }
  44. public function base(string $base){
  45. $this->_base = $base;
  46. }
  47. public static function url(string $url){
  48. return preg_replace('/(\/+)/','/',$url);
  49. }
  50. public function prefix(string $prefix, callable $fn){
  51. $found = false;
  52. foreach($this->_routers as $k => $router){
  53. if($router->base == $prefix){
  54. $found = true;
  55. $fn($router);
  56. break;
  57. }
  58. }
  59. if(!$found){
  60. $router= new Router($prefix);
  61. $this->_routers[] = $router;
  62. $fn($router);
  63. }
  64. }
  65. public function path(string $path, callable $fn){
  66. $obj = false;
  67. foreach($this->_paths as $k => $p){
  68. if($p->path == $path){
  69. $obj = $p;
  70. }
  71. }
  72. if(!$obj){
  73. $obj = new Path($path);
  74. array_push($this->_paths, $obj);
  75. }
  76. $obj->handle($fn);
  77. return $this;
  78. }
  79. public function get(string $path, callable $fn){
  80. return $this->path($path, function($req, $res, $args) use($fn){
  81. if($req->verb === 'GET'){
  82. return $fn($req, $res, $args);
  83. }else{
  84. return false;
  85. }
  86. });
  87. }
  88. public function post(string $path, callable $fn){
  89. return $this->path($path, function($req, $res, $args) use($fn){
  90. if($req->verb === 'POST'){
  91. return $fn($req, $res, $args);
  92. }else{
  93. return false;
  94. }
  95. });
  96. }
  97. public function put(string $path, callable $fn){
  98. return $this->path($path, function($req, $res, $args) use($fn){
  99. if($req->verb === 'PUT'){
  100. return $fn($req, $res, $args);
  101. }else{
  102. return false;
  103. }
  104. });
  105. }
  106. public function delete(string $path, callable $fn){
  107. return $this->path($path, function($req, $res, $args) use($fn){
  108. if($req->verb === 'DELETE'){
  109. return $fn($req, $res, $args);
  110. }else{
  111. return false;
  112. }
  113. });
  114. }
  115. public function patch(string $path, callable $fn){
  116. return $this->path($path, function($req, $res, $args) use($fn){
  117. if($req->verb === 'PATCH'){
  118. return $fn($req, $res, $args);
  119. }else{
  120. return false;
  121. }
  122. });
  123. }
  124. public function paths(array $paths){
  125. foreach($paths as $path => $fn){
  126. $this->path($path, $fn);
  127. }
  128. return $this;
  129. }
  130. public function gets(array $paths){
  131. foreach($paths as $path => $fn){
  132. $this->get($path, $fn);
  133. }
  134. return $this;
  135. }
  136. public function posts(array $paths){
  137. foreach($paths as $path => $fn){
  138. $this->post($path, $fn);
  139. }
  140. return $this;
  141. }
  142. public function puts(array $paths){
  143. foreach($paths as $path => $fn){
  144. $this->put($path, $fn);
  145. }
  146. return $this;
  147. }
  148. public function deletes(array $paths){
  149. foreach($paths as $path => $fn){
  150. $this->delete($path, $fn);
  151. }
  152. return $this;
  153. }
  154. public function patches(array $paths){
  155. foreach($paths as $path => $fn){
  156. $this->patch($path, $fn);
  157. }
  158. return $this;
  159. }
  160. public function clear(){
  161. $this->_paths = [];
  162. }
  163. public function handle(string $path, Request $req = null, Response $res = null, callable $fn = null, callable $onerror = null){
  164. if(strpos($path, $this->base) !== false){
  165. $path = substr($path,strpos($path,$this->base)+strlen($this->base));
  166. if($path[0] != '/'){
  167. $path = '/'.$path;
  168. }
  169. if(is_null($req)){
  170. $req = new Request(Request::get_verb(), Request::get_url(), Request::get_headers(), Request::get_body());
  171. }
  172. if(is_null($res)){
  173. $res = new Response();
  174. }
  175. if(!in_array($res,$this->responses)){
  176. array_push($this->responses,$res);
  177. }
  178. $this->fire('handle', $req, $res);
  179. $handled = false;
  180. foreach($this->_routers as $prefix => $router){
  181. $router->handle($path, $req, $res);
  182. $handled = $handled ||$router->handled;
  183. }
  184. ob_start();
  185. foreach($this->_paths as $k => $p){
  186. if($p->matches($path)){
  187. $handled = true;
  188. try{
  189. if($p($req, $res, $p->args($path)) === false){
  190. $handled = false;
  191. }
  192. }catch(\Exception $e){
  193. if(!is_null($onerror)){
  194. $onerror($req, $res,$e);
  195. }else{
  196. throw $e;
  197. }
  198. }
  199. }
  200. }
  201. $this->_handled = $handled;
  202. if(!$handled && !is_null($fn)){
  203. $fn($req, $res);
  204. }
  205. $res->output .= ob_get_contents();
  206. ob_end_clean();
  207. $this->fire('afterhandle', $req, $res);
  208. }
  209. return $res;
  210. }
  211. }
  212. ?>