app.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 $name;
  18. public $textdomain;
  19. public $textdomain_folder;
  20. public $textdomain_codeset;
  21. public function __construct(string $name, callable $fn = null, array $events = null){
  22. $this->name = $name;
  23. $this->textdomain = $name;
  24. $this->textdomain_folder = __DIR__.'/locale/';
  25. $this->textdomain_codeset = 'UFT-8';
  26. $this->router = new Router();
  27. $this->response = new Response();
  28. $this->routers = [];
  29. $this->domains = [];
  30. $this->_onerror = function($req, $res, $error){
  31. $o = json_encode([
  32. 'message'=>$error->getMessage(),
  33. 'code'=>$error->getCode(),
  34. 'file'=>$error->getFile(),
  35. 'line'=>$error->getLine(),
  36. 'traceback'=>$error->getTrace()
  37. ]);
  38. $res->code(404);
  39. $res->header('Content-Type', 'application/json');
  40. $res->end($o);
  41. $res->shutdown();
  42. };
  43. if(!is_null($events)){
  44. foreach($events as $name => $handler){
  45. $this->on($name, $handler);
  46. }
  47. }
  48. static::$apps[] = $this;
  49. if(is_callable($fn)){
  50. $fn($this);
  51. }
  52. }
  53. public function __destruct(){
  54. $this->routers = [];
  55. $index = array_search($this, static::$apps);
  56. if($index !== false){
  57. array_splice(static::$apps, $index, 1);
  58. }
  59. if(is_callable('parent::__destruct')){
  60. parent::__destruct();
  61. }
  62. }
  63. public static function import_all(string $dirpath){
  64. foreach(scandir($dirpath) as $file){
  65. $path = "{$dirpath}/{$file}";
  66. if(is_file($path) && pathinfo($path, PATHINFO_EXTENSION) == "php"){
  67. require_once($path);
  68. }
  69. }
  70. }
  71. public static function shutdown(){
  72. $verb = Request::get_verb();
  73. $url = Request::get_url();
  74. $data = Request::get_body();
  75. foreach(static::$apps as $k => $app){
  76. if($app instanceof App){
  77. $app->handle($verb, $url, $data)->shutdown();
  78. }
  79. }
  80. if(is_callable('parent::__destruct')){
  81. parent::__destruct();
  82. }
  83. }
  84. public static function shutdown_error($error){
  85. foreach(static::$apps as $k => $app){
  86. if(is_null($app->request)){
  87. $app->request = new Request(Request::get_verb(), Request::get_url(), Request::get_headers(), Request::get_body());
  88. }
  89. $app->onerror($app->request, $app->response, $error);
  90. $app->response->shutdown();
  91. }
  92. }
  93. public function handle(string $verb, array $url, string $data, array $headers = null){
  94. if(is_null($headers)){
  95. $headers = Request::get_headers();
  96. }
  97. $res = $this->response;
  98. $this->request = $req = new Request($verb, $url, $headers, $data);
  99. Response::locale($req->locale, $this->textdomain, $this->textdomain_folder, $this->textdomain_codeset);
  100. if($this->fire('handle', $req, $res)){
  101. $self = $this;
  102. $onerror = function($res, $error) use($self){
  103. $self->onerror($req, $res, $error);
  104. };
  105. $handled = false;
  106. // Domain routers
  107. foreach($this->domains as $host => $router){
  108. if($url['host'] == $host){
  109. while(is_string($router)){
  110. $router = $this->domains[$router];
  111. }
  112. $router->handle($url["path"], $req, $res, null, $onerror);
  113. $handled = $handled || $router->handled;
  114. }
  115. }
  116. // Prefixed path routers
  117. foreach($this->routers as $prefix => $router){
  118. if(strpos($url["path"], $prefix, 0 ) == 0){
  119. $router->handle($url["path"], $req, $res, null, $onerror);
  120. $handled = $handled || $router->handled;
  121. }
  122. }
  123. // Base router for non-prefixed paths
  124. $this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self, $url){
  125. if(!$handled){
  126. $self->onerror($req, $res, new Exception("{$url['scheme']}://{$url['host']}{$url["path"]} Not Found", 404));
  127. }
  128. }, $onerror);
  129. $this->fire('afterhandle', $req, $res);
  130. }
  131. return $res;
  132. }
  133. public function error(callable $fn){
  134. $this->_onerror = $fn;
  135. return $this;
  136. }
  137. public function base(string $base = null){
  138. if(is_null($base)){
  139. return $this->router->base;
  140. }else{
  141. foreach($this->domains as &$router){
  142. if($router instanceof Router){
  143. $router->base($base);
  144. }
  145. }
  146. foreach($this->routers as $prefix => &$router){
  147. $router->base("{$base}/{$prefix}");
  148. }
  149. $this->router->base($base);
  150. return $this;
  151. }
  152. }
  153. public function route(string $path, callable $fn){
  154. $this->router->path($path, $fn);
  155. return $this;
  156. }
  157. public function prefix(string $prefix, callable $fn){
  158. if(!$this->routers[$prefix]){
  159. $this->routers[$prefix] = new Router("{$this->router->base}/{$prefix}");
  160. }
  161. $fn($this->routers[$prefix]);
  162. return $this;
  163. }
  164. public function domain(string $host, callable $fn){
  165. if(!isset($this->domains[$host])){
  166. $this->domains[$host] = new Router($this->router->base);
  167. }
  168. $fn($this->domains[$host]);
  169. return $this;
  170. }
  171. public function map_domain(string $host, $handle){
  172. if(!is_array($handle)){
  173. $handle = [$handle];
  174. }
  175. foreach($handle as $host2){
  176. $this->domains[$host2] = $host;
  177. }
  178. return $this;
  179. }
  180. public function bind(string $hosts, callable $fn = null){
  181. $hosts = array_map(function($item){
  182. return trim($item);
  183. }, explode(',', $hosts));
  184. $host = array_pop($hosts);
  185. if(is_null($fn)){
  186. $fn = function(Router $router){
  187. Controller::handle_all($router);
  188. };
  189. }
  190. return $this->map_domain($host, $hosts)->domain($host, $fn);
  191. }
  192. public function onerror(Request $req, Response $res, $error){
  193. $this->fire('error', $error);
  194. $fn = $this->_onerror;
  195. if(is_callable($fn)){
  196. $fn($req, $res, $error);
  197. }
  198. }
  199. public function locale(string $domain, string $folder, string $codeset = 'UTF-8'){
  200. $this->textdomain = $domain;
  201. $this->textdomain_codeset = $codeset;
  202. $this->textdomain_folder = $folder;
  203. return $this;
  204. }
  205. }
  206. set_error_handler(function($errno, $errstr, $errfile, $errline){
  207. App::shutdown_error(new App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace()));
  208. }, E_ALL);
  209. register_shutdown_function(function(){
  210. error_reporting(E_ALL);
  211. ini_set('display_errors', 'On');
  212. try{
  213. App::shutdown();
  214. }catch(Exception $error){
  215. App::shutdown_error(new App\Exception($error->getMessage(), $error->getCode(), $error, $error->getFile(), $error->getLine(), $error->getTrace()));
  216. }
  217. });
  218. gc_enable();
  219. ?>