App.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace daswork;
  3. use \daswork\Route;
  4. use \daswork\Config;
  5. class App{
  6. public static $classMap = array();#用于判断类是否存在,节约性能
  7. /**
  8. * 运行控制器和方法
  9. * @throws \Exception
  10. */
  11. public static function run(){
  12. self::init();
  13. $route = new Route();
  14. $model = $route->model;
  15. $ctrlClass = '\\app\\' . $model . '\\' . "controller" . '\\'. $route->ctrl;
  16. $action = $route->action;
  17. $ctrl = new $ctrlClass();
  18. $data = $ctrl->$action();
  19. // var_dump($data);
  20. // is_null($request) && $request = Request::instance();
  21. // 请求缓存检查
  22. // $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']);
  23. // 输出数据到客户端
  24. if ($data instanceof Response) {
  25. $response = $data;
  26. } elseif (!is_null($data)) {
  27. // 默认自动识别响应输出类型
  28. // $isAjax = $request->isAjax();
  29. // $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
  30. $type = 'html';
  31. $response = Response::create($data, $type);
  32. } else {
  33. $response = Response::create();
  34. }
  35. $response->send();
  36. }
  37. public static function init()
  38. {
  39. Config::load();
  40. // return Config::get();
  41. }
  42. }
  43. // /**
  44. // * 执行应用程序
  45. // * @access public
  46. // * @param Request $request Request对象
  47. // * @return Response
  48. // * @throws Exception
  49. // */
  50. // public static function run(Request $request = null)
  51. // {
  52. // is_null($request) && $request = Request::instance();
  53. // try {
  54. // $config = self::initCommon();
  55. // if (defined('BIND_MODULE')) {
  56. // // 模块/控制器绑定
  57. // BIND_MODULE && Route::bind(BIND_MODULE);
  58. // } elseif ($config['auto_bind_module']) {
  59. // // 入口自动绑定
  60. // $name = pathinfo($request->baseFile(), PATHINFO_FILENAME);
  61. // if ($name && 'index' != $name && is_dir(APP_PATH . $name)) {
  62. // Route::bind($name);
  63. // }
  64. // }
  65. // $request->filter($config['default_filter']);
  66. // // 默认语言
  67. // Lang::range($config['default_lang']);
  68. // if ($config['lang_switch_on']) {
  69. // // 开启多语言机制 检测当前语言
  70. // Lang::detect();
  71. // }
  72. // $request->langset(Lang::range());
  73. // // 加载系统语言包
  74. // Lang::load([
  75. // THINK_PATH . 'lang' . DS . $request->langset() . EXT,
  76. // APP_PATH . 'lang' . DS . $request->langset() . EXT,
  77. // ]);
  78. // // 获取应用调度信息
  79. // $dispatch = self::$dispatch;
  80. // if (empty($dispatch)) {
  81. // // 进行URL路由检测
  82. // $dispatch = self::routeCheck($request, $config);
  83. // }
  84. // // 记录当前调度信息
  85. // $request->dispatch($dispatch);
  86. // // 记录路由和请求信息
  87. // if (self::$debug) {
  88. // Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
  89. // Log::record('[ HEADER ] ' . var_export($request->header(), true), 'info');
  90. // Log::record('[ PARAM ] ' . var_export($request->param(), true), 'info');
  91. // }
  92. // // 监听app_begin
  93. // Hook::listen('app_begin', $dispatch);
  94. // // 请求缓存检查
  95. // $request->cache($config['request_cache'], $config['request_cache_expire'], $config['request_cache_except']);
  96. // $data = self::exec($dispatch, $config);
  97. // } catch (HttpResponseException $exception) {
  98. // $data = $exception->getResponse();
  99. // }
  100. // // 清空类的实例化
  101. // Loader::clearInstance();
  102. // // 输出数据到客户端
  103. // if ($data instanceof Response) {
  104. // $response = $data;
  105. // } elseif (!is_null($data)) {
  106. // // 默认自动识别响应输出类型
  107. // $isAjax = $request->isAjax();
  108. // $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
  109. // $response = Response::create($data, $type);
  110. // } else {
  111. // $response = Response::create();
  112. // }
  113. // // 监听app_end
  114. // Hook::listen('app_end', $response);
  115. // return $response;
  116. // }