app.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. require_once('base.abstract.class.php');
  3. require_once('router.class.php');
  4. require_once('response.class.php');
  5. require_once('request.class.php');
  6. require_once('uri.class.php');
  7. require_once('events.trait.php');
  8. class App extends Base {
  9. use Events;
  10. private static $apps = [];
  11. private $domains;
  12. private $routers;
  13. private $router;
  14. public $_onerror;
  15. public function __construct(string $name, Callable $fn = null, array $events = null){
  16. $this->name = $name;
  17. $this->router = new Router();
  18. $this->routers = [];
  19. $this->domains = [];
  20. $this->_onerror = function($req, $res, $error){
  21. $o = json_encode([
  22. 'message'=>$error->getMessage(),
  23. 'code'=>$error->getCode(),
  24. 'file'=>$error->getFile(),
  25. 'line'=>$error->getLine(),
  26. 'traceback'=>$error->getTrace()
  27. ]);
  28. if($res){
  29. $res->code(404);
  30. $res->header('Content-Type', 'application/json');
  31. $res->end($o);
  32. }else{
  33. http_response_code(404);
  34. header('Content-Type: application/json');
  35. print($o);
  36. }
  37. };
  38. if(!is_null($events)){
  39. foreach($events as $name => $handler){
  40. $this->on($name, $handler);
  41. }
  42. }
  43. static::$apps[] = $this;
  44. if(is_callable($fn)){
  45. $fn($this);
  46. }
  47. }
  48. public function __destruct(){
  49. $this->routers = [];
  50. $index = array_search(static::$apps, $this);
  51. if($index !== false){
  52. array_splice(static::$apps, $index, 1);
  53. }
  54. if(is_callable('parent::__destruct')){
  55. parent::__destruct();
  56. }
  57. }
  58. public static function shutdown(){
  59. $verb = $_SERVER['REQUEST_METHOD'];
  60. if(isset($_SERVER['REDIRECT_URL'])){
  61. $url = 'REDIRECT_URL';
  62. }else{
  63. $url = 'REQUEST_URI';
  64. }
  65. if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
  66. $root = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
  67. }else{
  68. $root = !empty($_SERVER['HTTPS']) ? "https://" : "http://";
  69. }
  70. // @todo - determine if http or http and also check cloudflare
  71. $url = parse_url($root.$_SERVER['HTTP_HOST'].$_SERVER[$url]);
  72. $data = file_get_contents( 'php://input','r');
  73. foreach(static::$apps as $k => $app){
  74. if($app instanceof App){
  75. $app->handle($verb, $url, $data)->shutdown();
  76. }
  77. }
  78. if(is_callable('parent::__destruct')){
  79. parent::__destruct();
  80. }
  81. }
  82. public static function shutdown_error($error){
  83. foreach(static::$apps as $k => $app){
  84. if(is_callable($app->onerror)){
  85. $app->onerror(null, null, $error);
  86. }
  87. }
  88. }
  89. public function handle(string $verb, array $url, string $data, array $headers = null){
  90. if(is_null($headers)){
  91. $headers = getallheaders();
  92. }
  93. $res = new Response();
  94. $req = new Request($url, $headers, $data);
  95. $this->fire('handle', $req, $res);
  96. $self = $this;
  97. $onerror = function($res, $error) use($self){
  98. $self->onerror($req, $res, $error);
  99. };
  100. $handled = false;
  101. // Domain routers
  102. foreach($this->domains as $host => $router){
  103. if($url['host'] == $host){
  104. $router->handle($url["path"], $req, $res, null, $onerror);
  105. $handled = $handled || $router->handled;
  106. }
  107. }
  108. // Prefixed path routers
  109. foreach($this->routers as $prefix => $router){
  110. $router->handle($url["path"], $req, $res, null, $onerror);
  111. $handled = $handled || $router->handled;
  112. }
  113. // Base router for non-prefixed paths
  114. $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
  115. if(!$handled){
  116. $self->onerror($req, $res,new Error("Not Found", 404));
  117. }
  118. }, $onerror);
  119. $this->fire('afterhandle', $req, $res);
  120. return $res;
  121. }
  122. public function error(Callable $fn){
  123. $this->_onerror = $fn;
  124. return $this;
  125. }
  126. public function route(string $path, Callable $fn){
  127. $this->router->path($path, $fn);
  128. return $this;
  129. }
  130. public function prefix(string $prefix, Callable $fn){
  131. if(!$this->routers[$prefix]){
  132. $this->routers[$prefix] = new Router($prefix);
  133. }
  134. $fn($this->routers[$prefix]);
  135. return $this;
  136. }
  137. public function domain(string $host, Callable $fn){
  138. if(!isset($this->domains[$host])){
  139. $this->domains[$host] = new Router();
  140. }
  141. $fn($this->domains[$host]);
  142. return $this;
  143. }
  144. public function onerror(Request $req, Response $res, $error){
  145. $this->fire('error', $error);
  146. $fn = $this->_onerror;
  147. if(is_callable($fn)){
  148. $fn($req, $res, $error);
  149. }
  150. }
  151. }
  152. error_reporting(E_ALL);
  153. ini_set('display_errors', 'On');
  154. register_shutdown_function(function(){
  155. App::shutdown();
  156. });
  157. set_error_handler(function($errno, $errstr, $errfile, $errline){
  158. App::shutdown_error([
  159. 'number'=> $errno,
  160. 'msg'=> $errstr,
  161. 'file'=> $errfile,
  162. 'line'=> $errline,
  163. 'backtrace'=> debug_backtrace(),
  164. 'included'=> get_included_files()
  165. ]);
  166. },E_ALL);
  167. register_shutdown_function(function(){
  168. $error = error_get_last();
  169. if($error['type'] == 1){
  170. App::shutdown_error([
  171. 'number'=> $error['type'],
  172. 'msg'=> $error['message'],
  173. 'file'=> $error['file'],
  174. 'line'=> $error['line'],
  175. 'backtrace'=> [],
  176. 'included'=> get_included_files()
  177. ]);
  178. }
  179. });
  180. ?>