RuleName.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  16. * 注册路由标识
  17. * @access public
  18. * @param string $name 路由标识
  19. * @param array $value 路由规则
  20. * @param bool $first 是否置顶
  21. * @return void
  22. */
  23. public function set($name, $value, $first = false)
  24. {
  25. if ($first && isset($this->item[$name])) {
  26. array_unshift($this->item[$name], $value);
  27. } else {
  28. $this->item[$name][] = $value;
  29. }
  30. }
  31. /**
  32. * 导入路由标识
  33. * @access public
  34. * @param array $name 路由标识
  35. * @return void
  36. */
  37. public function import($item)
  38. {
  39. $this->item = $item;
  40. }
  41. /**
  42. * 根据路由标识获取路由信息(用于URL生成)
  43. * @access public
  44. * @param string $name 路由标识
  45. * @param string $domain 域名
  46. * @return array|null
  47. */
  48. public function get($name = null, $domain = null)
  49. {
  50. if (is_null($name)) {
  51. return $this->item;
  52. }
  53. $name = strtolower($name);
  54. if (isset($this->item[$name])) {
  55. if (is_null($domain)) {
  56. $result = $this->item[$name];
  57. } else {
  58. $result = [];
  59. foreach ($this->item[$name] as $item) {
  60. if ($item[2] == $domain) {
  61. $result[] = $item;
  62. }
  63. }
  64. }
  65. } else {
  66. $result = null;
  67. }
  68. return $result;
  69. }
  70. }