app.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Juju;
  3. require_once('base.abstract.class.php');
  4. require_once('App/router.class.php');
  5. require_once('App/exception.class.php');
  6. require_once('App/controller.abstract.class.php');
  7. use \Juju\{App\Controller, App\Exception, App\Router, Http\Response, Http\Request};
  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 $request = null;
  16. public $response;
  17. public function __construct(string $name, callable $fn = null, array $events = null){
  18. $this->name = $name;
  19. $this->router = new Router();
  20. $this->response = new Response();
  21. $this->routers = [];
  22. $this->domains = [];
  23. $this->_onerror = function($req, $res, $error){
  24. $o = json_encode([
  25. 'message'=>$error->getMessage(),
  26. 'code'=>$error->getCode(),
  27. 'file'=>$error->getFile(),
  28. 'line'=>$error->getLine(),
  29. 'traceback'=>$error->getTrace()
  30. ]);
  31. $res->code(404);
  32. $res->header('Content-Type', 'application/json');
  33. $res->end($o);
  34. $res->shutdown();
  35. };
  36. if(!is_null($events)){
  37. foreach($events as $name => $handler){
  38. $this->on($name, $handler);
  39. }
  40. }
  41. static::$apps[] = $this;
  42. if(is_callable($fn)){
  43. $fn($this);
  44. }
  45. }
  46. public function __destruct(){
  47. $this->routers = [];
  48. $index = array_search($this, static::$apps);
  49. if($index !== false){
  50. array_splice(static::$apps, $index, 1);
  51. }
  52. if(is_callable('parent::__destruct')){
  53. parent::__destruct();
  54. }
  55. }
  56. public static function import_all(string $dirpath){
  57. foreach(scandir($dirpath) as $file){
  58. $path = "{$dirpath}/{$file}";
  59. if(is_file($path) && pathinfo($path, PATHINFO_EXTENSION) == "php"){
  60. require_once($path);
  61. }
  62. }
  63. }
  64. public static function shutdown(){
  65. $verb = $_SERVER['REQUEST_METHOD'];
  66. $url = App::get_url();
  67. $data = file_get_contents( 'php://input','r');
  68. foreach(static::$apps as $k => $app){
  69. if($app instanceof App){
  70. $app->handle($verb, $url, $data)->shutdown();
  71. }
  72. }
  73. if(is_callable('parent::__destruct')){
  74. parent::__destruct();
  75. }
  76. }
  77. public static function shutdown_error($error){
  78. foreach(static::$apps as $k => $app){
  79. if(is_null($app->request)){
  80. $app->request = new Request(static::get_url(), getallheaders(), file_get_contents( 'php://input','r'));
  81. }
  82. $app->onerror($app->request, $app->response, $error);
  83. $app->response->shutdown();
  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 = $this->response;
  105. $this->request = $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, $url){
  129. if(!$handled){
  130. $self->onerror($req, $res, new Exception("{$url['scheme']}://{$url['host']}{$url["path"]} 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 $host, $handle){
  160. if(!is_array($handle)){
  161. $handle = [$handle];
  162. }
  163. foreach($handle as $host2){
  164. $this->domains[$host2] = $host;
  165. }
  166. return $this;
  167. }
  168. public function bind(string $hosts, callable $fn = null){
  169. $hosts = array_map(function($item){
  170. return trim($item);
  171. }, explode(',', $hosts));
  172. $host = array_pop($hosts);
  173. if(is_null($fn)){
  174. $fn = function(Router $router){
  175. Controller::handle_all($router);
  176. };
  177. }
  178. return $this->map_domain($host, $hosts)->domain($host, $fn);
  179. }
  180. public function onerror(Request $req, Response $res, $error){
  181. $this->fire('error', $error);
  182. $fn = $this->_onerror;
  183. if(is_callable($fn)){
  184. $fn($req, $res, $error);
  185. }
  186. }
  187. }
  188. error_reporting(E_ALL);
  189. ini_set('display_errors', 'On');
  190. register_shutdown_function(function(){
  191. App::shutdown();
  192. });
  193. set_error_handler(function($errno, $errstr, $errfile, $errline){
  194. App::shutdown_error(new App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace(), get_included_files()));
  195. },E_ALL);
  196. ?>