App.php 4.5 KB

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