Module.php 5.2 KB

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