1
0

app.class.php 5.2 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($this, static::$apps);
  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 import_all(string $dirpath){
  59. foreach(scandir($dirpath) as $file){
  60. $path = "{$dirpath}{$file}";
  61. if(is_file($path) && pathinfo($file, PATHINFO_EXTENSION) == "php"){
  62. require_once($path);
  63. }
  64. }
  65. }
  66. public static function shutdown(){
  67. $verb = $_SERVER['REQUEST_METHOD'];
  68. $url = App::get_url();
  69. $data = file_get_contents( 'php://input','r');
  70. foreach(static::$apps as $k => $app){
  71. if($app instanceof App){
  72. $app->handle($verb, $url, $data)->shutdown();
  73. }
  74. }
  75. if(is_callable('parent::__destruct')){
  76. parent::__destruct();
  77. }
  78. }
  79. public static function shutdown_error($error){
  80. $req = new Request(App::get_url());
  81. $res = new Response();
  82. foreach(static::$apps as $k => $app){
  83. $app->onerror($req, $res, $error);
  84. }
  85. }
  86. public static function get_url(){
  87. if(isset($_SERVER['REDIRECT_URL'])){
  88. $url = 'REDIRECT_URL';
  89. }else{
  90. $url = 'REQUEST_URI';
  91. }
  92. if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
  93. $root = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
  94. }else{
  95. $root = !empty($_SERVER['HTTPS']) ? "https://" : "http://";
  96. }
  97. // @todo - determine if http or http and also check cloudflare
  98. return parse_url($root.$_SERVER['HTTP_HOST'].$_SERVER[$url]);
  99. }
  100. public function handle(string $verb, array $url, string $data, array $headers = null){
  101. if(is_null($headers)){
  102. $headers = getallheaders();
  103. }
  104. $res = new Response();
  105. $req = new Request($url, $headers, $data);
  106. if($this->fire('handle', $req, $res)){
  107. $self = $this;
  108. $onerror = function($res, $error) use($self){
  109. $self->onerror($req, $res, $error);
  110. };
  111. $handled = false;
  112. // Domain routers
  113. foreach($this->domains as $host => $router){
  114. if($url['host'] == $host){
  115. while(is_string($router)){
  116. $router = $this->domains[$router];
  117. }
  118. $router->handle($url["path"], $req, $res, null, $onerror);
  119. $handled = $handled || $router->handled;
  120. }
  121. }
  122. // Prefixed path routers
  123. foreach($this->routers as $prefix => $router){
  124. $router->handle($url["path"], $req, $res, null, $onerror);
  125. $handled = $handled || $router->handled;
  126. }
  127. // Base router for non-prefixed paths
  128. $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
  129. if(!$handled){
  130. $self->onerror($req, $res,new Error("Not Found", 404));
  131. }
  132. }, $onerror);
  133. $this->fire('afterhandle', $req, $res);
  134. }
  135. return $res;
  136. }
  137. public function error(Callable $fn){
  138. $this->_onerror = $fn;
  139. return $this;
  140. }
  141. public function route(string $path, Callable $fn){
  142. $this->router->path($path, $fn);
  143. return $this;
  144. }
  145. public function prefix(string $prefix, Callable $fn){
  146. if(!$this->routers[$prefix]){
  147. $this->routers[$prefix] = new Router($prefix);
  148. }
  149. $fn($this->routers[$prefix]);
  150. return $this;
  151. }
  152. public function domain(string $host, Callable $fn){
  153. if(!isset($this->domains[$host])){
  154. $this->domains[$host] = new Router();
  155. }
  156. $fn($this->domains[$host]);
  157. return $this;
  158. }
  159. public function map_domain(string $host1, string $host2){
  160. $this->domains[$host2] = $host1;
  161. return $this;
  162. }
  163. public function onerror(Request $req, Response $res, $error){
  164. if($this->fire('error', $error, $req, $res)){
  165. $fn = $this->_onerror;
  166. if(is_callable($fn)){
  167. $fn($req, $res, $error);
  168. }
  169. }
  170. }
  171. }
  172. error_reporting(E_ALL);
  173. ini_set('display_errors', 'On');
  174. register_shutdown_function(function(){
  175. App::shutdown();
  176. });
  177. set_error_handler(function($errno, $errstr, $errfile, $errline){
  178. App::shutdown_error([
  179. 'number'=> $errno,
  180. 'msg'=> $errstr,
  181. 'file'=> $errfile,
  182. 'line'=> $errline,
  183. 'backtrace'=> debug_backtrace(),
  184. 'included'=> get_included_files()
  185. ]);
  186. },E_ALL);
  187. ?>