app.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. require_once('base.class.php');
  3. require_once('router.class.php');
  4. require_once('response.class.php');
  5. require_once('events.interface.php');
  6. class App extends Base implements Events {
  7. private static $apps = array();
  8. private $routers;
  9. public function __construct($name){
  10. $this->name = $name;
  11. $this->routers = array();
  12. static::$apps[] = $this;
  13. }
  14. public function __destruct(){
  15. $this->routers = array();
  16. $index = array_search(static::$apps, $this);
  17. if($index !== false){
  18. array_splice(static::$apps, $index, 1);
  19. }
  20. if(is_callable('parent::__destruct')){
  21. parent::__destruct();
  22. }
  23. }
  24. public static function shutdown(){
  25. $verb = $_SERVER['REQUEST_METHOD'];
  26. $url = parse_url($_SERVER['REDIRECT_URL']);
  27. $data = file_get_contents( 'php://input','r');
  28. foreach(static::$apps as $k => $app){
  29. if($app instanceof App){
  30. $app->handle($verb, $url, $data);
  31. }
  32. }
  33. if(is_callable('parent::__destruct')){
  34. parent::__destruct();
  35. }
  36. }
  37. public static function shutdown_error($error){
  38. foreach(static::$apps as $k => $app){
  39. $app->error($error);
  40. }
  41. }
  42. public function handle($verb, $url, $data){
  43. $res = new Response($url);
  44. $self = $this;
  45. $onerror = function($res, $error){
  46. $self->error($error);
  47. };
  48. foreach($this->routers as $type => $routers){
  49. foreach($routers as $router){
  50. switch($type){
  51. case 'prefix':
  52. $router->handle($url["path"], $res, null, $onerror);
  53. break;
  54. case 'domain':
  55. $router->handle($url["host"], $res, null, $onerror);
  56. break;
  57. }
  58. }
  59. }
  60. $res->url = $url;
  61. return $res;
  62. }
  63. public function error($error){
  64. }
  65. public function get_router($type, $path){
  66. $ret = false;
  67. if(isset($this->routers[$type])){
  68. foreach($this->routers[$type] as $k => $router){
  69. if($router->base == $path){
  70. $ret = $router;
  71. }
  72. }
  73. }
  74. return $ret;
  75. }
  76. public function create_router($type, $path){
  77. $router = new Router($path);
  78. if(!isset($this->routers[$type])){
  79. $this->routers[$type] = array();
  80. }
  81. $this->routers[$type][] = $router;
  82. return $router;
  83. }
  84. public function route($type, $path, Callable $fn){
  85. $router = $this->get_router($type, $prefix);
  86. if($router == false){
  87. $router = $this->create_router($type, $prefix);
  88. }
  89. call_user_func($fn, $router);
  90. }
  91. public function prefix($prefix, Callable $fn){
  92. $this->route('prefix', $prefix, function($router){
  93. $router->base($prefix);
  94. call_user_func($fn, $router);
  95. });
  96. }
  97. public function domain($prefix, Callable $fn){
  98. $this->route('domain', $prefix, function($router){
  99. $router->base($prefix);
  100. call_user_func($fn, $router);
  101. });
  102. }
  103. }
  104. error_reporting(E_ALL);
  105. ini_set('display_errors', 'On');
  106. register_shutdown_function(function(){
  107. App::shutdown();
  108. });
  109. set_error_handler(function($errno, $errstr, $errfile, $errline){
  110. App::shutdown_error(array(
  111. 'number'=> $errno,
  112. 'msg'=> $errstr,
  113. 'file'=> $errfile,
  114. 'line'=> $errline,
  115. 'backtrace'=> debug_backtrace(),
  116. 'included'=> get_included_files()
  117. ));
  118. },E_ALL);
  119. register_shutdown_function(function(){
  120. $error = error_get_last();
  121. if($error['type'] == 1){
  122. App::shutdown_error(array(
  123. 'number'=> $error['type'],
  124. 'msg'=> $error['message'],
  125. 'file'=> $error['file'],
  126. 'line'=> $error['line'],
  127. 'backtrace'=> array(),
  128. 'included'=> get_included_files()
  129. ));
  130. }
  131. });
  132. ?>