0
0

RuleName.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. class RuleName
  13. {
  14. protected $item = [];
  15. protected $rule = [];
  16. /**
  17. * 注册路由标识
  18. * @access public
  19. * @param string $name 路由标识
  20. * @param array $value 路由规则
  21. * @param bool $first 是否置顶
  22. * @return void
  23. */
  24. public function set($name, $value, $first = false)
  25. {
  26. if ($first && isset($this->item[$name])) {
  27. array_unshift($this->item[$name], $value);
  28. } else {
  29. $this->item[$name][] = $value;
  30. }
  31. }
  32. /**
  33. * 注册路由规则
  34. * @access public
  35. * @param string $rule 路由规则
  36. * @param RuleItem $route 路由
  37. * @return void
  38. */
  39. public function setRule($rule, $route)
  40. {
  41. $this->rule[$route->getDomain()][$rule][$route->getMethod()] = $route;
  42. }
  43. /**
  44. * 根据路由规则获取路由对象(列表)
  45. * @access public
  46. * @param string $name 路由标识
  47. * @param string $domain 域名
  48. * @return array
  49. */
  50. public function getRule($rule, $domain = null)
  51. {
  52. return isset($this->rule[$domain][$rule]) ? $this->rule[$domain][$rule] : [];
  53. }
  54. /**
  55. * 获取全部路由列表
  56. * @access public
  57. * @param string $domain 域名
  58. * @return array
  59. */
  60. public function getRuleList($domain = null)
  61. {
  62. $list = [];
  63. foreach ($this->rule as $ruleDomain => $rules) {
  64. foreach ($rules as $rule => $items) {
  65. foreach ($items as $item) {
  66. $val['domain'] = $ruleDomain;
  67. foreach (['method', 'rule', 'name', 'route', 'pattern', 'option'] as $param) {
  68. $call = 'get' . $param;
  69. $val[$param] = $item->$call();
  70. }
  71. $list[$ruleDomain][] = $val;
  72. }
  73. }
  74. }
  75. if ($domain) {
  76. return isset($list[$domain]) ? $list[$domain] : [];
  77. }
  78. return $list;
  79. }
  80. /**
  81. * 导入路由标识
  82. * @access public
  83. * @param array $name 路由标识
  84. * @return void
  85. */
  86. public function import($item)
  87. {
  88. $this->item = $item;
  89. }
  90. /**
  91. * 根据路由标识获取路由信息(用于URL生成)
  92. * @access public
  93. * @param string $name 路由标识
  94. * @param string $domain 域名
  95. * @return array|null
  96. */
  97. public function get($name = null, $domain = null, $method = '*')
  98. {
  99. if (is_null($name)) {
  100. return $this->item;
  101. }
  102. $name = strtolower($name);
  103. $method = strtolower($method);
  104. if (isset($this->item[$name])) {
  105. if (is_null($domain)) {
  106. $result = $this->item[$name];
  107. } else {
  108. $result = [];
  109. foreach ($this->item[$name] as $item) {
  110. if ($item[2] == $domain && ('*' == $item[4] || $method == $item[4])) {
  111. $result[] = $item;
  112. }
  113. }
  114. }
  115. } else {
  116. $result = null;
  117. }
  118. return $result;
  119. }
  120. /**
  121. * 清空路由规则
  122. * @access public
  123. * @return void
  124. */
  125. public function clear()
  126. {
  127. $this->item = [];
  128. $this->rule = [];
  129. }
  130. }