Module.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\route\dispatch;
  12. use ReflectionMethod;
  13. use think\exception\ClassNotFoundException;
  14. use think\exception\HttpException;
  15. use think\Loader;
  16. use think\Request;
  17. use think\route\Dispatch;
  18. class Module extends Dispatch
  19. {
  20. protected $controller;
  21. protected $actionName;
  22. public function init()
  23. {
  24. parent::init();
  25. $result = $this->dispatch;
  26. if (is_string($result)) {
  27. $result = explode('/', $result);
  28. }
  29. if ($this->rule->getConfig('app_multi_module')) {
  30. // 多模块部署
  31. $module = strip_tags(strtolower($result[0] ?: $this->rule->getConfig('default_module')));
  32. $bind = $this->rule->getRouter()->getBind();
  33. $available = false;
  34. if ($bind && preg_match('/^[a-z]/is', $bind)) {
  35. // 绑定模块
  36. list($bindModule) = explode('/', $bind);
  37. if (empty($result[0])) {
  38. $module = $bindModule;
  39. }
  40. $available = true;
  41. } elseif (!in_array($module, $this->rule->getConfig('deny_module_list')) && is_dir($this->app->getAppPath() . $module)) {
  42. $available = true;
  43. } elseif ($this->rule->getConfig('empty_module')) {
  44. $module = $this->rule->getConfig('empty_module');
  45. $available = true;
  46. }
  47. // 模块初始化
  48. if ($module && $available) {
  49. // 初始化模块
  50. $this->request->setModule($module);
  51. $this->app->init($module);
  52. } else {
  53. throw new HttpException(404, 'module not exists:' . $module);
  54. }
  55. }
  56. // 是否自动转换控制器和操作名
  57. $convert = is_bool($this->convert) ? $this->convert : $this->rule->getConfig('url_convert');
  58. // 获取控制器名
  59. $controller = strip_tags($result[1] ?: $this->rule->getConfig('default_controller'));
  60. $this->controller = $convert ? strtolower($controller) : $controller;
  61. // 获取操作名
  62. $this->actionName = strip_tags($result[2] ?: $this->rule->getConfig('default_action'));
  63. // 设置当前请求的控制器、操作
  64. $this->request
  65. ->setController(Loader::parseName($this->controller, 1))
  66. ->setAction($this->actionName);
  67. return $this;
  68. }
  69. public function exec()
  70. {
  71. // 监听module_init
  72. $this->app['hook']->listen('module_init');
  73. try {
  74. // 实例化控制器
  75. $instance = $this->app->controller($this->controller,
  76. $this->rule->getConfig('url_controller_layer'),
  77. $this->rule->getConfig('controller_suffix'),
  78. $this->rule->getConfig('empty_controller'));
  79. } catch (ClassNotFoundException $e) {
  80. throw new HttpException(404, 'controller not exists:' . $e->getClass());
  81. }
  82. $this->app['middleware']->controller(function (Request $request, $next) use ($instance) {
  83. // 获取当前操作名
  84. $action = $this->actionName . $this->rule->getConfig('action_suffix');
  85. if (is_callable([$instance, $action])) {
  86. // 执行操作方法
  87. $call = [$instance, $action];
  88. // 严格获取当前操作方法名
  89. $reflect = new ReflectionMethod($instance, $action);
  90. $methodName = $reflect->getName();
  91. $suffix = $this->rule->getConfig('action_suffix');
  92. $actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName;
  93. $this->request->setAction($actionName);
  94. // 自动获取请求变量
  95. $vars = $this->rule->getConfig('url_param_type')
  96. ? $this->request->route()
  97. : $this->request->param();
  98. $vars = array_merge($vars, $this->param);
  99. } elseif (is_callable([$instance, '_empty'])) {
  100. // 空操作
  101. $call = [$instance, '_empty'];
  102. $vars = [$this->actionName];
  103. $reflect = new ReflectionMethod($instance, '_empty');
  104. } else {
  105. // 操作不存在
  106. throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
  107. }
  108. $this->app['hook']->listen('action_begin', $call);
  109. $data = $this->app->invokeReflectMethod($instance, $reflect, $vars);
  110. return $this->autoResponse($data);
  111. });
  112. return $this->app['middleware']->dispatch($this->request, 'controller');
  113. }
  114. }