app.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. while(is_string($router)){
  105. $router = $this->domains[$router];
  106. }
  107. $router->handle($url["path"], $req, $res, null, $onerror);
  108. $handled = $handled || $router->handled;
  109. }
  110. }
  111. // Prefixed path routers
  112. foreach($this->routers as $prefix => $router){
  113. $router->handle($url["path"], $req, $res, null, $onerror);
  114. $handled = $handled || $router->handled;
  115. }
  116. // Base router for non-prefixed paths
  117. $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
  118. if(!$handled){
  119. $self->onerror($req, $res,new Error("Not Found", 404));
  120. }
  121. }, $onerror);
  122. $this->fire('afterhandle', $req, $res);
  123. return $res;
  124. }
  125. public function error(Callable $fn){
  126. $this->_onerror = $fn;
  127. return $this;
  128. }
  129. public function route(string $path, Callable $fn){
  130. $this->router->path($path, $fn);
  131. return $this;
  132. }
  133. public function prefix(string $prefix, Callable $fn){
  134. if(!$this->routers[$prefix]){
  135. $this->routers[$prefix] = new Router($prefix);
  136. }
  137. $fn($this->routers[$prefix]);
  138. return $this;
  139. }
  140. public function domain(string $host, Callable $fn){
  141. if(!isset($this->domains[$host])){
  142. $this->domains[$host] = new Router();
  143. }
  144. $fn($this->domains[$host]);
  145. return $this;
  146. }
  147. public function map_domain(string $host1, string $host2){
  148. $this->domains[$host2] = $host1;
  149. return $this;
  150. }
  151. public function onerror(Request $req, Response $res, $error){
  152. $this->fire('error', $error);
  153. $fn = $this->_onerror;
  154. if(is_callable($fn)){
  155. $fn($req, $res, $error);
  156. }
  157. }
  158. }
  159. error_reporting(E_ALL);
  160. ini_set('display_errors', 'On');
  161. register_shutdown_function(function(){
  162. App::shutdown();
  163. });
  164. set_error_handler(function($errno, $errstr, $errfile, $errline){
  165. App::shutdown_error([
  166. 'number'=> $errno,
  167. 'msg'=> $errstr,
  168. 'file'=> $errfile,
  169. 'line'=> $errline,
  170. 'backtrace'=> debug_backtrace(),
  171. 'included'=> get_included_files()
  172. ]);
  173. },E_ALL);
  174. register_shutdown_function(function(){
  175. $error = error_get_last();
  176. if($error['type'] == 1){
  177. App::shutdown_error([
  178. 'number'=> $error['type'],
  179. 'msg'=> $error['message'],
  180. 'file'=> $error['file'],
  181. 'line'=> $error['line'],
  182. 'backtrace'=> [],
  183. 'included'=> get_included_files()
  184. ]);
  185. }
  186. });
  187. ?>