0
0

Resource.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // REST路由方法定义
  18. protected $rest = [];
  19. /**
  20. * 架构函数
  21. * @access public
  22. * @param Route $router 路由对象
  23. * @param RuleGroup $parent 上级对象
  24. * @param string $name 资源名称
  25. * @param string $route 路由地址
  26. * @param array $option 路由参数
  27. * @param array $pattern 变量规则
  28. * @param array $rest 资源定义
  29. */
  30. public function __construct(Route $router, RuleGroup $parent = null, $name = '', $route = '', $option = [], $pattern = [], $rest = [])
  31. {
  32. $this->router = $router;
  33. $this->parent = $parent;
  34. $this->resource = $name;
  35. $this->route = $route;
  36. $this->name = strpos($name, '.') ? strstr($name, '.', true) : $name;
  37. $this->setFullName();
  38. // 资源路由默认为完整匹配
  39. $option['complete_match'] = true;
  40. $this->pattern = $pattern;
  41. $this->option = $option;
  42. $this->rest = $rest;
  43. if ($this->parent) {
  44. $this->domain = $this->parent->getDomain();
  45. $this->parent->addRuleItem($this);
  46. }
  47. if ($router->isTest()) {
  48. $this->buildResourceRule();
  49. }
  50. }
  51. /**
  52. * 生成资源路由规则
  53. * @access protected
  54. * @return void
  55. */
  56. protected function buildResourceRule()
  57. {
  58. $origin = $this->router->getGroup();
  59. $this->router->setGroup($this);
  60. $rule = $this->resource;
  61. $option = $this->option;
  62. if (strpos($rule, '.')) {
  63. // 注册嵌套资源路由
  64. $array = explode('.', $rule);
  65. $last = array_pop($array);
  66. $item = [];
  67. foreach ($array as $val) {
  68. $item[] = $val . '/<' . (isset($option['var'][$val]) ? $option['var'][$val] : $val . '_id') . '>';
  69. }
  70. $rule = implode('/', $item) . '/' . $last;
  71. }
  72. $prefix = substr($rule, strlen($this->name) + 1);
  73. // 注册资源路由
  74. foreach ($this->rest as $key => $val) {
  75. if ((isset($option['only']) && !in_array($key, $option['only']))
  76. || (isset($option['except']) && in_array($key, $option['except']))) {
  77. continue;
  78. }
  79. if (isset($last) && strpos($val[1], '<id>') && isset($option['var'][$last])) {
  80. $val[1] = str_replace('<id>', '<' . $option['var'][$last] . '>', $val[1]);
  81. } elseif (strpos($val[1], '<id>') && isset($option['var'][$rule])) {
  82. $val[1] = str_replace('<id>', '<' . $option['var'][$rule] . '>', $val[1]);
  83. }
  84. $this->addRule(trim($prefix . $val[1], '/'), $this->route . '/' . $val[2], $val[0]);
  85. }
  86. $this->router->setGroup($origin);
  87. }
  88. /**
  89. * rest方法定义和修改
  90. * @access public
  91. * @param string $name 方法名称
  92. * @param array|bool $resource 资源
  93. * @return $this
  94. */
  95. public function rest($name, $resource = [])
  96. {
  97. if (is_array($name)) {
  98. $this->rest = $resource ? $name : array_merge($this->rest, $name);
  99. } else {
  100. $this->rest[$name] = $resource;
  101. }
  102. return $this;
  103. }
  104. }