0
0

Url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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;
  12. class Url
  13. {
  14. /**
  15. * 配置参数
  16. * @var array
  17. */
  18. protected $config = [];
  19. /**
  20. * ROOT地址
  21. * @var string
  22. */
  23. protected $root;
  24. /**
  25. * 绑定检查
  26. * @var bool
  27. */
  28. protected $bindCheck;
  29. /**
  30. * 应用对象
  31. * @var App
  32. */
  33. protected $app;
  34. public function __construct(App $app, array $config = [])
  35. {
  36. $this->app = $app;
  37. $this->config = $config;
  38. if (is_file($app->getRuntimePath() . 'route.php')) {
  39. // 读取路由映射文件
  40. $app['route']->setName(include $app->getRuntimePath() . 'route.php');
  41. }
  42. }
  43. /**
  44. * 初始化
  45. * @access public
  46. * @param array $config
  47. * @return void
  48. */
  49. public function init(array $config = [])
  50. {
  51. $this->config = array_merge($this->config, array_change_key_case($config));
  52. }
  53. public static function __make(App $app, Config $config)
  54. {
  55. return new static($app, $config->pull('app'));
  56. }
  57. /**
  58. * URL生成 支持路由反射
  59. * @access public
  60. * @param string $url 路由地址
  61. * @param string|array $vars 参数(支持数组和字符串)a=val&b=val2... ['a'=>'val1', 'b'=>'val2']
  62. * @param string|bool $suffix 伪静态后缀,默认为true表示获取配置值
  63. * @param boolean|string $domain 是否显示域名 或者直接传入域名
  64. * @return string
  65. */
  66. public function build($url = '', $vars = '', $suffix = true, $domain = false)
  67. {
  68. // 解析URL
  69. if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
  70. // [name] 表示使用路由命名标识生成URL
  71. $name = substr($url, 1, $pos - 1);
  72. $url = 'name' . substr($url, $pos + 1);
  73. }
  74. if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
  75. $info = parse_url($url);
  76. $url = !empty($info['path']) ? $info['path'] : '';
  77. if (isset($info['fragment'])) {
  78. // 解析锚点
  79. $anchor = $info['fragment'];
  80. if (false !== strpos($anchor, '?')) {
  81. // 解析参数
  82. list($anchor, $info['query']) = explode('?', $anchor, 2);
  83. }
  84. if (false !== strpos($anchor, '@')) {
  85. // 解析域名
  86. list($anchor, $domain) = explode('@', $anchor, 2);
  87. }
  88. } elseif (strpos($url, '@') && false === strpos($url, '\\')) {
  89. // 解析域名
  90. list($url, $domain) = explode('@', $url, 2);
  91. }
  92. }
  93. // 解析参数
  94. if (is_string($vars)) {
  95. // aaa=1&bbb=2 转换成数组
  96. parse_str($vars, $vars);
  97. }
  98. if ($url) {
  99. $checkName = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : '');
  100. $checkDomain = $domain && is_string($domain) ? $domain : null;
  101. $rule = $this->app['route']->getName($checkName, $checkDomain);
  102. if (is_null($rule) && isset($info['query'])) {
  103. $rule = $this->app['route']->getName($url);
  104. // 解析地址里面参数 合并到vars
  105. parse_str($info['query'], $params);
  106. $vars = array_merge($params, $vars);
  107. unset($info['query']);
  108. }
  109. }
  110. if (!empty($rule) && $match = $this->getRuleUrl($rule, $vars, $domain)) {
  111. // 匹配路由命名标识
  112. $url = $match[0];
  113. if ($domain) {
  114. $domain = $match[1];
  115. }
  116. if (!is_null($match[2])) {
  117. $suffix = $match[2];
  118. }
  119. } elseif (!empty($rule) && isset($name)) {
  120. throw new \InvalidArgumentException('route name not exists:' . $name);
  121. } else {
  122. // 检查别名路由
  123. $alias = $this->app['route']->getAlias();
  124. $matchAlias = false;
  125. if ($alias) {
  126. // 别名路由解析
  127. foreach ($alias as $key => $item) {
  128. $val = $item->getRoute();
  129. if (0 === strpos($url, $val)) {
  130. $url = $key . substr($url, strlen($val));
  131. $matchAlias = true;
  132. break;
  133. }
  134. }
  135. }
  136. if (!$matchAlias) {
  137. // 路由标识不存在 直接解析
  138. $url = $this->parseUrl($url);
  139. }
  140. // 检测URL绑定
  141. if (!$this->bindCheck) {
  142. $bind = $this->app['route']->getBind($domain && is_string($domain) ? $domain : null);
  143. if ($bind && 0 === strpos($url, $bind)) {
  144. $url = substr($url, strlen($bind) + 1);
  145. } else {
  146. $binds = $this->app['route']->getBind(true);
  147. foreach ($binds as $key => $val) {
  148. if (is_string($val) && 0 === strpos($url, $val) && substr_count($val, '/') > 1) {
  149. $url = substr($url, strlen($val) + 1);
  150. $domain = $key;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. if (isset($info['query'])) {
  157. // 解析地址里面参数 合并到vars
  158. parse_str($info['query'], $params);
  159. $vars = array_merge($params, $vars);
  160. }
  161. }
  162. // 还原URL分隔符
  163. $depr = $this->config['pathinfo_depr'];
  164. $url = str_replace('/', $depr, $url);
  165. // URL后缀
  166. if ('/' == substr($url, -1) || '' == $url) {
  167. $suffix = '';
  168. } else {
  169. $suffix = $this->parseSuffix($suffix);
  170. }
  171. // 锚点
  172. $anchor = !empty($anchor) ? '#' . $anchor : '';
  173. // 参数组装
  174. if (!empty($vars)) {
  175. // 添加参数
  176. if ($this->config['url_common_param']) {
  177. $vars = http_build_query($vars);
  178. $url .= $suffix . '?' . $vars . $anchor;
  179. } else {
  180. $paramType = $this->config['url_param_type'];
  181. foreach ($vars as $var => $val) {
  182. if ('' !== trim($val)) {
  183. if ($paramType) {
  184. $url .= $depr . urlencode($val);
  185. } else {
  186. $url .= $depr . $var . $depr . urlencode($val);
  187. }
  188. }
  189. }
  190. $url .= $suffix . $anchor;
  191. }
  192. } else {
  193. $url .= $suffix . $anchor;
  194. }
  195. // 检测域名
  196. $domain = $this->parseDomain($url, $domain);
  197. // URL组装
  198. $url = $domain . rtrim($this->root ?: $this->app['request']->root(), '/') . '/' . ltrim($url, '/');
  199. $this->bindCheck = false;
  200. return $url;
  201. }
  202. // 直接解析URL地址
  203. protected function parseUrl($url)
  204. {
  205. $request = $this->app['request'];
  206. if (0 === strpos($url, '/')) {
  207. // 直接作为路由地址解析
  208. $url = substr($url, 1);
  209. } elseif (false !== strpos($url, '\\')) {
  210. // 解析到类
  211. $url = ltrim(str_replace('\\', '/', $url), '/');
  212. } elseif (0 === strpos($url, '@')) {
  213. // 解析到控制器
  214. $url = substr($url, 1);
  215. } else {
  216. // 解析到 模块/控制器/操作
  217. $module = $request->module();
  218. $module = $module ? $module . '/' : '';
  219. $controller = $request->controller();
  220. if ('' == $url) {
  221. $action = $request->action();
  222. } else {
  223. $path = explode('/', $url);
  224. $action = array_pop($path);
  225. $controller = empty($path) ? $controller : array_pop($path);
  226. $module = empty($path) ? $module : array_pop($path) . '/';
  227. }
  228. if ($this->config['url_convert']) {
  229. $action = strtolower($action);
  230. $controller = Loader::parseName($controller);
  231. }
  232. $url = $module . $controller . '/' . $action;
  233. }
  234. return $url;
  235. }
  236. // 检测域名
  237. protected function parseDomain(&$url, $domain)
  238. {
  239. if (!$domain) {
  240. return '';
  241. }
  242. $rootDomain = $this->app['request']->rootDomain();
  243. if (true === $domain) {
  244. // 自动判断域名
  245. $domain = $this->config['app_host'] ?: $this->app['request']->host();
  246. $domains = $this->app['route']->getDomains();
  247. if ($domains) {
  248. $route_domain = array_keys($domains);
  249. foreach ($route_domain as $domain_prefix) {
  250. if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) {
  251. foreach ($domains as $key => $rule) {
  252. $rule = is_array($rule) ? $rule[0] : $rule;
  253. if (is_string($rule) && false === strpos($key, '*') && 0 === strpos($url, $rule)) {
  254. $url = ltrim($url, $rule);
  255. $domain = $key;
  256. // 生成对应子域名
  257. if (!empty($rootDomain)) {
  258. $domain .= $rootDomain;
  259. }
  260. break;
  261. } elseif (false !== strpos($key, '*')) {
  262. if (!empty($rootDomain)) {
  263. $domain .= $rootDomain;
  264. }
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. }
  271. } elseif (0 !== strpos($domain, $rootDomain) && false === strpos($domain, '.')) {
  272. $domain .= '.' . $rootDomain;
  273. }
  274. if (false !== strpos($domain, '://')) {
  275. $scheme = '';
  276. } else {
  277. $scheme = $this->app['request']->isSsl() || $this->config['is_https'] ? 'https://' : 'http://';
  278. }
  279. return $scheme . $domain;
  280. }
  281. // 解析URL后缀
  282. protected function parseSuffix($suffix)
  283. {
  284. if ($suffix) {
  285. $suffix = true === $suffix ? $this->config['url_html_suffix'] : $suffix;
  286. if ($pos = strpos($suffix, '|')) {
  287. $suffix = substr($suffix, 0, $pos);
  288. }
  289. }
  290. return (empty($suffix) || 0 === strpos($suffix, '.')) ? $suffix : '.' . $suffix;
  291. }
  292. // 匹配路由地址
  293. public function getRuleUrl($rule, &$vars = [], $allowDomain = '')
  294. {
  295. $port = $this->app['request']->port();
  296. foreach ($rule as $item) {
  297. list($url, $pattern, $domain, $suffix, $method) = $item;
  298. if (is_string($allowDomain) && $domain != $allowDomain) {
  299. continue;
  300. }
  301. if ($port && !in_array($port, [80, 443])) {
  302. $domain .= ':' . $port;
  303. }
  304. if (empty($pattern)) {
  305. return [rtrim($url, '?/-'), $domain, $suffix];
  306. }
  307. $type = $this->config['url_common_param'];
  308. $keys = [];
  309. foreach ($pattern as $key => $val) {
  310. if (isset($vars[$key])) {
  311. $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key, '<' . $key . '>'], $type ? $vars[$key] : urlencode($vars[$key]), $url);
  312. $keys[] = $key;
  313. $url = str_replace(['/?', '-?'], ['/', '-'], $url);
  314. $result = [rtrim($url, '?/-'), $domain, $suffix];
  315. } elseif (2 == $val) {
  316. $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
  317. $url = str_replace(['/?', '-?'], ['/', '-'], $url);
  318. $result = [rtrim($url, '?/-'), $domain, $suffix];
  319. } else {
  320. $result = null;
  321. $keys = [];
  322. break;
  323. }
  324. }
  325. $vars = array_diff_key($vars, array_flip($keys));
  326. if (isset($result)) {
  327. return $result;
  328. }
  329. }
  330. return false;
  331. }
  332. // 指定当前生成URL地址的root
  333. public function root($root)
  334. {
  335. $this->root = $root;
  336. $this->app['request']->setRoot($root);
  337. }
  338. public function __debugInfo()
  339. {
  340. $data = get_object_vars($this);
  341. unset($data['app']);
  342. return $data;
  343. }
  344. }