Resource.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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;
  12. use think\Route;
  13. class Resource extends RuleGroup
  14. {
  15. // 资源路由名称
  16. protected $resource;
  17. // 资源路由地址
  18. protected $route;
  19. // REST路由方法定义
  20. protected $rest = [];
  21. /**
  22. * 架构函数
  23. * @access public
  24. * @param Route $router 路由对象
  25. * @param RuleGroup $group 路由所属分组对象
  26. * @param string $name 资源名称
  27. * @param string $route 路由地址
  28. * @param array $option 路由参数
  29. * @param array $pattern 变量规则
  30. * @param array $rest 资源定义
  31. */
  32. public function __construct(Route $router, RuleGroup $group = null, $name = '', $route = '', $option = [], $pattern = [], $rest = [])
  33. {
  34. $this->router = $router;
  35. $this->parent = $group;
  36. $this->resource = $name;
  37. $this->route = $route;
  38. $this->name = strpos($name, '.') ? strstr($name, '.', true) : $name;
  39. $this->setFullName();
  40. // 资源路由默认为完整匹配
  41. $option['complete_match'] = true;
  42. $this->pattern = $pattern;
  43. $this->option = $option;
  44. $this->rest = $rest;
  45. }
  46. /**
  47. * 检测分组路由
  48. * @access public
  49. * @param Request $request 请求对象
  50. * @param string $url 访问地址
  51. * @param string $depr 路径分隔符
  52. * @param bool $completeMatch 路由是否完全匹配
  53. * @return Dispatch
  54. */
  55. public function check($request, $url, $depr = '/', $completeMatch = false)
  56. {
  57. // 生成资源路由的路由规则
  58. $this->buildResourceRule($this->resource, $this->option);
  59. return parent::check($request, $url, $depr, $completeMatch);
  60. }
  61. /**
  62. * 生成资源路由规则
  63. * @access protected
  64. * @param string $rule 路由规则
  65. * @param array $option 路由参数
  66. * @return void
  67. */
  68. protected function buildResourceRule($rule, $option = [])
  69. {
  70. if (strpos($rule, '.')) {
  71. // 注册嵌套资源路由
  72. $array = explode('.', $rule);
  73. $last = array_pop($array);
  74. $item = [];
  75. foreach ($array as $val) {
  76. $item[] = $val . '/:' . (isset($option['var'][$val]) ? $option['var'][$val] : $val . '_id');
  77. }
  78. $rule = implode('/', $item) . '/' . $last;
  79. }
  80. // 注册分组
  81. $group = $this->router->getGroup();
  82. $this->router->setGroup($this);
  83. // 注册资源路由
  84. foreach ($this->rest as $key => $val) {
  85. if ((isset($option['only']) && !in_array($key, $option['only']))
  86. || (isset($option['except']) && in_array($key, $option['except']))) {
  87. continue;
  88. }
  89. if (isset($last) && strpos($val[1], ':id') && isset($option['var'][$last])) {
  90. $val[1] = str_replace(':id', ':' . $option['var'][$last], $val[1]);
  91. } elseif (strpos($val[1], ':id') && isset($option['var'][$rule])) {
  92. $val[1] = str_replace(':id', ':' . $option['var'][$rule], $val[1]);
  93. }
  94. $option['rest'] = $key;
  95. $this->router->rule(trim($val[1], '/'), $this->route . '/' . $val[2], $val[0], $option);
  96. }
  97. $this->router->setGroup($group);
  98. }
  99. /**
  100. * rest方法定义和修改
  101. * @access public
  102. * @param string $name 方法名称
  103. * @param array|bool $resource 资源
  104. * @return $this
  105. */
  106. public function rest($name, $resource = [])
  107. {
  108. if (is_array($name)) {
  109. $this->rest = $resource ? $name : array_merge($this->rest, $name);
  110. } else {
  111. $this->rest[$name] = $resource;
  112. }
  113. return $this;
  114. }
  115. /**
  116. * 设置资源允许
  117. * @access public
  118. * @param array $only
  119. * @return $this
  120. */
  121. public function only($only)
  122. {
  123. return $this->option('only', $only);
  124. }
  125. /**
  126. * 设置资源排除
  127. * @access public
  128. * @param array $except
  129. * @return $this
  130. */
  131. public function except($except)
  132. {
  133. return $this->option('except', $except);
  134. }
  135. /**
  136. * 设置资源路由的变量
  137. * @access public
  138. * @param array $vars
  139. * @return $this
  140. */
  141. public function vars($vars)
  142. {
  143. return $this->option('var', $vars);
  144. }
  145. }