router.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. require_once('path.class.php');
  3. require_once('response.class.php');
  4. class Router {
  5. public static $paths = array();
  6. public static $base = '/';
  7. private static $responses = array();
  8. public function __construct(){
  9. // No Constructing
  10. }
  11. public function __clone(){
  12. // No cloning
  13. }
  14. public function __destruct(){
  15. $this->paths = array();
  16. }
  17. public function __toString(){
  18. return "[Router]";
  19. }
  20. public static function base($base){
  21. static::$base = $base;
  22. }
  23. public static function url($url){
  24. return preg_replace('/(\/+)/','/',$url);
  25. }
  26. // fn = function(response,args){}
  27. public static function path($path,$fn){
  28. $obj = false;
  29. foreach(static::$paths as $k => $p){
  30. if($p->path == $path){
  31. $obj = $p;
  32. }
  33. }
  34. if(!$obj){
  35. $obj = new Path($path);
  36. array_push(static::$paths,$obj);
  37. }
  38. return $obj->handle($fn);
  39. }
  40. public static function paths($paths){
  41. foreach($paths as $path => $fn){
  42. static::path($path,$fn);
  43. }
  44. }
  45. public static function clear(){
  46. static::$paths = array();
  47. }
  48. public static function handle($url,$res = null,$fn = null){
  49. if(strpos($url,static::$base) !== false){
  50. $url = rtrim(substr($url,strpos($url,static::$base)+strlen(static::$base)),'/');
  51. if(empty($url) || $url[0] != '/'){
  52. $url = '/'.$url;
  53. }
  54. if(is_null($res)){
  55. $res = new Response($url);
  56. }
  57. if(!in_array($res,static::$responses)){
  58. array_push(static::$responses,$res);
  59. }
  60. ob_start();
  61. $handled = false;
  62. foreach(static::$paths as $k => $p){
  63. if($p->matches($url)){
  64. $handled = true;
  65. $p($res,$p->args($url));
  66. }
  67. }
  68. if(!$handled && !is_null($fn)){
  69. $fn($res,$url);
  70. }
  71. $res->output = ob_get_contents();
  72. ob_end_clean();
  73. }
  74. return $res;
  75. }
  76. public static function shutdown(){
  77. foreach(static::$responses as $k => $res){
  78. if($res instanceof Response){
  79. echo $res->end()->body;
  80. }
  81. }
  82. }
  83. public static function write($chunk){
  84. if(!isset(static::$responses[0])){
  85. array_push(static::$responses,new Response($_SERVER['REQUEST_URI']));
  86. }
  87. static::$responses[0]->write($chunk);
  88. }
  89. public static function header($name,$value){
  90. static::$responses[0]->header($name,$value);
  91. }
  92. public static function json($json){
  93. static::$responses[0]->json($json);
  94. }
  95. public static function redirect($url){
  96. static::clear();
  97. static::header('Location',$url);
  98. }
  99. }
  100. register_shutdown_function(function(){
  101. Router::shutdown();
  102. });
  103. ?>