1
0

app.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require_once('base.class.php');
  3. require_once('router.class.php');
  4. require_once('events.class.php');
  5. class App implements Base, Events {
  6. private static $apps = array();
  7. private $routers;
  8. public function __construct($name){
  9. $this->name = $name;
  10. $this->routers = array();
  11. static::$apps[] $this;
  12. }
  13. public function __destruct(){
  14. $this->routers = array();
  15. $index = array_search(static::$apps, $this);
  16. if($index !== false){
  17. array_splice(static::$apps, $index, 1);
  18. }
  19. if(is_callable('parent::__destruct')){
  20. parent::__destruct();
  21. }
  22. }
  23. public static function shutdown(){
  24. $verb = $_SERVER['REQUEST_METHOD'];
  25. $url = parse_url($_SERVER['REDIRECT_URL']);
  26. $data = file_get_contents( 'php://input','r');
  27. foreach(static::$apps as $k => $app){
  28. if($app instanceof App){
  29. $app->handle($verb, $url, $data);
  30. }
  31. }
  32. if(is_callable('parent::__destruct')){
  33. parent::__destruct();
  34. }
  35. }
  36. public static function shutdown_error($error){
  37. foreach(static::$apps as $k => $app){
  38. $app->error($error);
  39. }
  40. }
  41. public function handle($verb, $url, $data){
  42. }
  43. public function error($error){
  44. }
  45. public function get_router($type, $path){
  46. }
  47. public function create_router($type, $path){
  48. $router = new Router();
  49. if(!isset($this->routers[$type]){
  50. $this->routers[$type] = array();
  51. }
  52. $this->routers[$type][] = $router;
  53. return $router;
  54. }
  55. public function route($type, $path, Callable $fn){
  56. $router = $this->get_router($type, $prefix);
  57. if($router == false){
  58. $router = $this->create_router($type, $prefix);
  59. }
  60. $fn($router);
  61. }
  62. public function prefix($prefix, Callable $fn){
  63. $this->route('prefix', $prefix, function($router){
  64. $router->base($prefix);
  65. $fn($router);
  66. });
  67. }
  68. }
  69. error_reporting(E_ALL);
  70. ini_set('display_errors', 'On');
  71. register_shutdown_function(function(){
  72. App::shutdown();
  73. });
  74. set_error_handler(function($errno, $errstr, $errfile, $errline){
  75. App::shutdown_error(array(
  76. 'number'=> $errno,
  77. 'msg'=> $errstr,
  78. 'file'=> $errfile,
  79. 'line'=> $errline,
  80. 'backtrace'=> debug_backtrace(),
  81. 'included'=> get_included_files()
  82. ));
  83. },E_ALL);
  84. register_shutdown_function(function(){
  85. $error = error_get_last();
  86. if($error['type'] == 1){
  87. App::shutdown_error(array(
  88. 'number'=> $error['type'],
  89. 'msg'=> $error['message'],
  90. 'file'=> $error['file'],
  91. 'line'=> $error['line'],
  92. 'backtrace'=> array(),
  93. 'included'=> get_included_files()
  94. ));
  95. }
  96. });
  97. ?>