Rule.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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\Container;
  13. use think\Request;
  14. use think\Response;
  15. use think\route\dispatch\Callback as CallbackDispatch;
  16. use think\route\dispatch\Controller as ControllerDispatch;
  17. use think\route\dispatch\Module as ModuleDispatch;
  18. use think\route\dispatch\Redirect as RedirectDispatch;
  19. use think\route\dispatch\Response as ResponseDispatch;
  20. use think\route\dispatch\View as ViewDispatch;
  21. abstract class Rule
  22. {
  23. /**
  24. * 路由标识
  25. * @var string
  26. */
  27. protected $name;
  28. /**
  29. * 路由对象
  30. * @var Route
  31. */
  32. protected $router;
  33. /**
  34. * 路由所属分组
  35. * @var RuleGroup
  36. */
  37. protected $parent;
  38. /**
  39. * 路由规则
  40. * @var mixed
  41. */
  42. protected $rule;
  43. /**
  44. * 路由地址
  45. * @var string|\Closure
  46. */
  47. protected $route;
  48. /**
  49. * 请求类型
  50. * @var string
  51. */
  52. protected $method;
  53. /**
  54. * 路由变量
  55. * @var array
  56. */
  57. protected $vars = [];
  58. /**
  59. * 路由参数
  60. * @var array
  61. */
  62. protected $option = [];
  63. /**
  64. * 路由变量规则
  65. * @var array
  66. */
  67. protected $pattern = [];
  68. /**
  69. * 需要和分组合并的路由参数
  70. * @var array
  71. */
  72. protected $mergeOptions = ['after', 'before', 'model', 'header', 'response', 'append', 'middleware'];
  73. /**
  74. * 是否需要后置操作
  75. * @var bool
  76. */
  77. protected $doAfter;
  78. abstract public function check($request, $url, $completeMatch = false);
  79. /**
  80. * 获取Name
  81. * @access public
  82. * @return string
  83. */
  84. public function getName()
  85. {
  86. return $this->name;
  87. }
  88. /**
  89. * 获取当前路由规则
  90. * @access public
  91. * @return string
  92. */
  93. public function getRule()
  94. {
  95. return $this->rule;
  96. }
  97. /**
  98. * 获取当前路由地址
  99. * @access public
  100. * @return mixed
  101. */
  102. public function getRoute()
  103. {
  104. return $this->route;
  105. }
  106. /**
  107. * 获取当前路由的请求类型
  108. * @access public
  109. * @return string
  110. */
  111. public function getMethod()
  112. {
  113. return strtolower($this->method);
  114. }
  115. /**
  116. * 获取当前路由的变量
  117. * @access public
  118. * @return array
  119. */
  120. public function getVars()
  121. {
  122. return $this->vars;
  123. }
  124. /**
  125. * 获取路由对象
  126. * @access public
  127. * @return Route
  128. */
  129. public function getRouter()
  130. {
  131. return $this->router;
  132. }
  133. /**
  134. * 路由是否有后置操作
  135. * @access public
  136. * @return bool
  137. */
  138. public function doAfter()
  139. {
  140. return $this->doAfter;
  141. }
  142. /**
  143. * 获取路由分组
  144. * @access public
  145. * @return RuleGroup|null
  146. */
  147. public function getParent()
  148. {
  149. return $this->parent;
  150. }
  151. /**
  152. * 获取变量规则定义
  153. * @access public
  154. * @param string $name 变量名
  155. * @return mixed
  156. */
  157. public function getPattern($name = '')
  158. {
  159. if ('' === $name) {
  160. return $this->pattern;
  161. }
  162. return isset($this->pattern[$name]) ? $this->pattern[$name] : null;
  163. }
  164. /**
  165. * 获取路由参数
  166. * @access public
  167. * @param string $name 变量名
  168. * @return mixed
  169. */
  170. public function getConfig($name = '')
  171. {
  172. return $this->router->config($name);
  173. }
  174. /**
  175. * 获取路由参数定义
  176. * @access public
  177. * @param string $name 参数名
  178. * @return mixed
  179. */
  180. public function getOption($name = '')
  181. {
  182. if ('' === $name) {
  183. return $this->option;
  184. }
  185. return isset($this->option[$name]) ? $this->option[$name] : null;
  186. }
  187. /**
  188. * 注册路由参数
  189. * @access public
  190. * @param string|array $name 参数名
  191. * @param mixed $value 值
  192. * @return $this
  193. */
  194. public function option($name, $value = '')
  195. {
  196. if (is_array($name)) {
  197. $this->option = array_merge($this->option, $name);
  198. } else {
  199. $this->option[$name] = $value;
  200. }
  201. return $this;
  202. }
  203. /**
  204. * 注册变量规则
  205. * @access public
  206. * @param string|array $name 变量名
  207. * @param string $rule 变量规则
  208. * @return $this
  209. */
  210. public function pattern($name, $rule = '')
  211. {
  212. if (is_array($name)) {
  213. $this->pattern = array_merge($this->pattern, $name);
  214. } else {
  215. $this->pattern[$name] = $rule;
  216. }
  217. return $this;
  218. }
  219. /**
  220. * 设置标识
  221. * @access public
  222. * @param string $name 标识名
  223. * @return $this
  224. */
  225. public function name($name)
  226. {
  227. $this->name = $name;
  228. return $this;
  229. }
  230. /**
  231. * 设置变量
  232. * @access public
  233. * @param array $vars 变量
  234. * @return $this
  235. */
  236. public function vars($vars)
  237. {
  238. $this->vars = $vars;
  239. return $this;
  240. }
  241. /**
  242. * 设置路由请求类型
  243. * @access public
  244. * @param string $method
  245. * @return $this
  246. */
  247. public function method($method)
  248. {
  249. return $this->option('method', strtolower($method));
  250. }
  251. /**
  252. * 设置路由前置行为
  253. * @access public
  254. * @param array|\Closure $before
  255. * @return $this
  256. */
  257. public function before($before)
  258. {
  259. return $this->option('before', $before);
  260. }
  261. /**
  262. * 设置路由后置行为
  263. * @access public
  264. * @param array|\Closure $after
  265. * @return $this
  266. */
  267. public function after($after)
  268. {
  269. return $this->option('after', $after);
  270. }
  271. /**
  272. * 检查后缀
  273. * @access public
  274. * @param string $ext
  275. * @return $this
  276. */
  277. public function ext($ext = '')
  278. {
  279. return $this->option('ext', $ext);
  280. }
  281. /**
  282. * 检查禁止后缀
  283. * @access public
  284. * @param string $ext
  285. * @return $this
  286. */
  287. public function denyExt($ext = '')
  288. {
  289. return $this->option('deny_ext', $ext);
  290. }
  291. /**
  292. * 检查域名
  293. * @access public
  294. * @param string $domain
  295. * @return $this
  296. */
  297. public function domain($domain)
  298. {
  299. return $this->option('domain', $domain);
  300. }
  301. /**
  302. * 设置参数过滤检查
  303. * @access public
  304. * @param string|array $name
  305. * @param mixed $value
  306. * @return $this
  307. */
  308. public function filter($name, $value = null)
  309. {
  310. if (is_array($name)) {
  311. $this->option['filter'] = $name;
  312. } else {
  313. $this->option['filter'][$name] = $value;
  314. }
  315. return $this;
  316. }
  317. /**
  318. * 绑定模型
  319. * @access public
  320. * @param array|string $var 路由变量名 多个使用 & 分割
  321. * @param string|\Closure $model 绑定模型类
  322. * @param bool $exception 是否抛出异常
  323. * @return $this
  324. */
  325. public function model($var, $model = null, $exception = true)
  326. {
  327. if ($var instanceof \Closure) {
  328. $this->option['model'][] = $var;
  329. } elseif (is_array($var)) {
  330. $this->option['model'] = $var;
  331. } elseif (is_null($model)) {
  332. $this->option['model']['id'] = [$var, true];
  333. } else {
  334. $this->option['model'][$var] = [$model, $exception];
  335. }
  336. return $this;
  337. }
  338. /**
  339. * 附加路由隐式参数
  340. * @access public
  341. * @param array $append
  342. * @return $this
  343. */
  344. public function append(array $append = [])
  345. {
  346. if (isset($this->option['append'])) {
  347. $this->option['append'] = array_merge($this->option['append'], $append);
  348. } else {
  349. $this->option['append'] = $append;
  350. }
  351. return $this;
  352. }
  353. /**
  354. * 绑定验证
  355. * @access public
  356. * @param mixed $validate 验证器类
  357. * @param string $scene 验证场景
  358. * @param array $message 验证提示
  359. * @param bool $batch 批量验证
  360. * @return $this
  361. */
  362. public function validate($validate, $scene = null, $message = [], $batch = false)
  363. {
  364. $this->option['validate'] = [$validate, $scene, $message, $batch];
  365. return $this;
  366. }
  367. /**
  368. * 绑定Response对象
  369. * @access public
  370. * @param mixed $response
  371. * @return $this
  372. */
  373. public function response($response)
  374. {
  375. $this->option['response'][] = $response;
  376. return $this;
  377. }
  378. /**
  379. * 设置Response Header信息
  380. * @access public
  381. * @param string|array $name 参数名
  382. * @param string $value 参数值
  383. * @return $this
  384. */
  385. public function header($header, $value = null)
  386. {
  387. if (is_array($header)) {
  388. $this->option['header'] = $header;
  389. } else {
  390. $this->option['header'][$header] = $value;
  391. }
  392. return $this;
  393. }
  394. /**
  395. * 指定路由中间件
  396. * @access public
  397. * @param string|array|\Closure $middleware
  398. * @param mixed $param
  399. * @return $this
  400. */
  401. public function middleware($middleware, $param = null)
  402. {
  403. if (is_null($param) && is_array($middleware)) {
  404. $this->option['middleware'] = $middleware;
  405. } else {
  406. foreach ((array) $middleware as $item) {
  407. $this->option['middleware'][] = [$item, $param];
  408. }
  409. }
  410. return $this;
  411. }
  412. /**
  413. * 设置路由缓存
  414. * @access public
  415. * @param array|string $cache
  416. * @return $this
  417. */
  418. public function cache($cache)
  419. {
  420. return $this->option('cache', $cache);
  421. }
  422. /**
  423. * 检查URL分隔符
  424. * @access public
  425. * @param bool $depr
  426. * @return $this
  427. */
  428. public function depr($depr)
  429. {
  430. return $this->option('param_depr', $depr);
  431. }
  432. /**
  433. * 是否合并额外参数
  434. * @access public
  435. * @param bool $merge
  436. * @return $this
  437. */
  438. public function mergeExtraVars($merge = true)
  439. {
  440. return $this->option('merge_extra_vars', $merge);
  441. }
  442. /**
  443. * 设置需要合并的路由参数
  444. * @access public
  445. * @param array $option
  446. * @return $this
  447. */
  448. public function mergeOptions($option = [])
  449. {
  450. $this->mergeOptions = array_merge($this->mergeOptions, $option);
  451. return $this;
  452. }
  453. /**
  454. * 检查是否为HTTPS请求
  455. * @access public
  456. * @param bool $https
  457. * @return $this
  458. */
  459. public function https($https = true)
  460. {
  461. return $this->option('https', $https);
  462. }
  463. /**
  464. * 检查是否为AJAX请求
  465. * @access public
  466. * @param bool $ajax
  467. * @return $this
  468. */
  469. public function ajax($ajax = true)
  470. {
  471. return $this->option('ajax', $ajax);
  472. }
  473. /**
  474. * 检查是否为PJAX请求
  475. * @access public
  476. * @param bool $pjax
  477. * @return $this
  478. */
  479. public function pjax($pjax = true)
  480. {
  481. return $this->option('pjax', $pjax);
  482. }
  483. /**
  484. * 检查是否为手机访问
  485. * @access public
  486. * @param bool $mobile
  487. * @return $this
  488. */
  489. public function mobile($mobile = true)
  490. {
  491. return $this->option('mobile', $mobile);
  492. }
  493. /**
  494. * 当前路由到一个模板地址 当使用数组的时候可以传入模板变量
  495. * @access public
  496. * @param bool|array $view
  497. * @return $this
  498. */
  499. public function view($view = true)
  500. {
  501. return $this->option('view', $view);
  502. }
  503. /**
  504. * 当前路由为重定向
  505. * @access public
  506. * @param bool $redirect 是否为重定向
  507. * @return $this
  508. */
  509. public function redirect($redirect = true)
  510. {
  511. return $this->option('redirect', $redirect);
  512. }
  513. /**
  514. * 设置路由完整匹配
  515. * @access public
  516. * @param bool $match
  517. * @return $this
  518. */
  519. public function completeMatch($match = true)
  520. {
  521. return $this->option('complete_match', $match);
  522. }
  523. /**
  524. * 是否去除URL最后的斜线
  525. * @access public
  526. * @param bool $remove
  527. * @return $this
  528. */
  529. public function removeSlash($remove = true)
  530. {
  531. return $this->option('remove_slash', $remove);
  532. }
  533. /**
  534. * 设置是否允许跨域
  535. * @access public
  536. * @param bool $allow
  537. * @param array $header
  538. * @return $this
  539. */
  540. public function allowCrossDomain($allow = true, $header = [])
  541. {
  542. if (!empty($header)) {
  543. $this->header($header);
  544. }
  545. if ($allow && $this->parent) {
  546. $this->parent->addRuleItem($this, 'options');
  547. }
  548. return $this->option('cross_domain', $allow);
  549. }
  550. /**
  551. * 检查OPTIONS请求
  552. * @access public
  553. * @param Request $request
  554. * @return Dispatch|void
  555. */
  556. protected function checkCrossDomain($request)
  557. {
  558. if (!empty($this->option['cross_domain'])) {
  559. $header = [
  560. 'Access-Control-Allow-Origin' => '*',
  561. 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE',
  562. 'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
  563. ];
  564. if (!empty($this->option['header'])) {
  565. $header = array_merge($header, $this->option['header']);
  566. }
  567. $this->option['header'] = $header;
  568. if ($request->method(true) == 'OPTIONS') {
  569. return new ResponseDispatch($request, $this, Response::create()->code(204)->header($header));
  570. }
  571. }
  572. }
  573. /**
  574. * 设置路由规则全局有效
  575. * @access public
  576. * @return $this
  577. */
  578. public function crossDomainRule()
  579. {
  580. if ($this instanceof RuleGroup) {
  581. $method = '*';
  582. } else {
  583. $method = $this->method;
  584. }
  585. $this->router->setCrossDomainRule($this, $method);
  586. return $this;
  587. }
  588. /**
  589. * 合并分组参数
  590. * @access protected
  591. * @return void
  592. */
  593. protected function mergeGroupOptions()
  594. {
  595. $parentOption = $this->parent->getOption();
  596. // 合并分组参数
  597. foreach ($this->mergeOptions as $item) {
  598. if (isset($parentOption[$item]) && isset($this->option[$item])) {
  599. $this->option[$item] = array_merge($parentOption[$item], $this->option[$item]);
  600. }
  601. }
  602. $this->option = array_merge($parentOption, $this->option);
  603. return $this->option;
  604. }
  605. /**
  606. * 解析匹配到的规则路由
  607. * @access public
  608. * @param Request $request 请求对象
  609. * @param string $rule 路由规则
  610. * @param string $route 路由地址
  611. * @param string $url URL地址
  612. * @param array $option 路由参数
  613. * @param array $matches 匹配的变量
  614. * @return Dispatch
  615. */
  616. public function parseRule($request, $rule, $route, $url, $option = [], $matches = [])
  617. {
  618. if (is_string($route) && isset($option['prefix'])) {
  619. // 路由地址前缀
  620. $route = $option['prefix'] . $route;
  621. }
  622. // 替换路由地址中的变量
  623. if (is_string($route) && !empty($matches)) {
  624. foreach ($matches as $key => $val) {
  625. if (false !== strpos($route, '<' . $key . '>')) {
  626. $route = str_replace('<' . $key . '>', $val, $route);
  627. } elseif (false !== strpos($route, ':' . $key)) {
  628. $route = str_replace(':' . $key, $val, $route);
  629. }
  630. }
  631. }
  632. // 解析额外参数
  633. $count = substr_count($rule, '/');
  634. $url = array_slice(explode('|', $url), $count + 1);
  635. $this->parseUrlParams($request, implode('|', $url), $matches);
  636. $this->route = $route;
  637. $this->vars = $matches;
  638. $this->option = $option;
  639. $this->doAfter = true;
  640. // 发起路由调度
  641. return $this->dispatch($request, $route, $option);
  642. }
  643. /**
  644. * 检查路由前置行为
  645. * @access protected
  646. * @param mixed $before 前置行为
  647. * @return mixed
  648. */
  649. protected function checkBefore($before)
  650. {
  651. $hook = Container::get('hook');
  652. foreach ((array) $before as $behavior) {
  653. $result = $hook->exec($behavior);
  654. if (false === $result) {
  655. return false;
  656. }
  657. }
  658. }
  659. /**
  660. * 发起路由调度
  661. * @access protected
  662. * @param Request $request Request对象
  663. * @param mixed $route 路由地址
  664. * @param array $option 路由参数
  665. * @return Dispatch
  666. */
  667. protected function dispatch($request, $route, $option)
  668. {
  669. if ($route instanceof \Closure) {
  670. // 执行闭包
  671. $result = new CallbackDispatch($request, $this, $route);
  672. } elseif ($route instanceof Response) {
  673. $result = new ResponseDispatch($request, $this, $route);
  674. } elseif (isset($option['view']) && false !== $option['view']) {
  675. $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []);
  676. } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) {
  677. // 路由到重定向地址
  678. $result = new RedirectDispatch($request, $this, $route, [], isset($option['status']) ? $option['status'] : 301);
  679. } elseif (false !== strpos($route, '\\')) {
  680. // 路由到方法
  681. $result = $this->dispatchMethod($request, $route);
  682. } elseif (0 === strpos($route, '@')) {
  683. // 路由到控制器
  684. $result = $this->dispatchController($request, substr($route, 1));
  685. } else {
  686. // 路由到模块/控制器/操作
  687. $result = $this->dispatchModule($request, $route);
  688. }
  689. return $result;
  690. }
  691. /**
  692. * 解析URL地址为 模块/控制器/操作
  693. * @access protected
  694. * @param Request $request Request对象
  695. * @param string $route 路由地址
  696. * @return CallbackDispatch
  697. */
  698. protected function dispatchMethod($request, $route)
  699. {
  700. list($path, $var) = $this->parseUrlPath($route);
  701. $route = str_replace('/', '@', implode('/', $path));
  702. $method = strpos($route, '@') ? explode('@', $route) : $route;
  703. return new CallbackDispatch($request, $this, $method, $var);
  704. }
  705. /**
  706. * 解析URL地址为 模块/控制器/操作
  707. * @access protected
  708. * @param Request $request Request对象
  709. * @param string $route 路由地址
  710. * @return ControllerDispatch
  711. */
  712. protected function dispatchController($request, $route)
  713. {
  714. list($route, $var) = $this->parseUrlPath($route);
  715. $result = new ControllerDispatch($request, $this, implode('/', $route), $var);
  716. $request->setAction(array_pop($route));
  717. $request->setController($route ? array_pop($route) : $this->getConfig('default_controller'));
  718. $request->setModule($route ? array_pop($route) : $this->getConfig('default_module'));
  719. return $result;
  720. }
  721. /**
  722. * 解析URL地址为 模块/控制器/操作
  723. * @access protected
  724. * @param Request $request Request对象
  725. * @param string $route 路由地址
  726. * @return ModuleDispatch
  727. */
  728. protected function dispatchModule($request, $route)
  729. {
  730. list($path, $var) = $this->parseUrlPath($route);
  731. $action = array_pop($path);
  732. $controller = !empty($path) ? array_pop($path) : null;
  733. $module = $this->getConfig('app_multi_module') && !empty($path) ? array_pop($path) : null;
  734. $method = $request->method();
  735. if ($this->getConfig('use_action_prefix') && $this->router->getMethodPrefix($method)) {
  736. $prefix = $this->router->getMethodPrefix($method);
  737. // 操作方法前缀支持
  738. $action = 0 !== strpos($action, $prefix) ? $prefix . $action : $action;
  739. }
  740. // 设置当前请求的路由变量
  741. $request->setRouteVars($var);
  742. // 路由到模块/控制器/操作
  743. return new ModuleDispatch($request, $this, [$module, $controller, $action], ['convert' => false]);
  744. }
  745. /**
  746. * 路由检查
  747. * @access protected
  748. * @param array $option 路由参数
  749. * @param Request $request Request对象
  750. * @return bool
  751. */
  752. protected function checkOption($option, Request $request)
  753. {
  754. // 请求类型检测
  755. if (!empty($option['method'])) {
  756. if (is_string($option['method']) && false === stripos($option['method'], $request->method())) {
  757. return false;
  758. }
  759. }
  760. // AJAX PJAX 请求检查
  761. foreach (['ajax', 'pjax', 'mobile'] as $item) {
  762. if (isset($option[$item])) {
  763. $call = 'is' . $item;
  764. if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) {
  765. return false;
  766. }
  767. }
  768. }
  769. // 伪静态后缀检测
  770. if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|'))
  771. || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) {
  772. return false;
  773. }
  774. // 域名检查
  775. if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) {
  776. return false;
  777. }
  778. // HTTPS检查
  779. if ((isset($option['https']) && $option['https'] && !$request->isSsl())
  780. || (isset($option['https']) && !$option['https'] && $request->isSsl())) {
  781. return false;
  782. }
  783. // 请求参数检查
  784. if (isset($option['filter'])) {
  785. foreach ($option['filter'] as $name => $value) {
  786. if ($request->param($name, '', null) != $value) {
  787. return false;
  788. }
  789. }
  790. }
  791. return true;
  792. }
  793. /**
  794. * 解析URL地址中的参数Request对象
  795. * @access protected
  796. * @param Request $request
  797. * @param string $rule 路由规则
  798. * @param array $var 变量
  799. * @return void
  800. */
  801. protected function parseUrlParams($request, $url, &$var = [])
  802. {
  803. if ($url) {
  804. if ($this->getConfig('url_param_type')) {
  805. $var += explode('|', $url);
  806. } else {
  807. preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
  808. $var[$match[1]] = strip_tags($match[2]);
  809. }, $url);
  810. }
  811. }
  812. }
  813. /**
  814. * 解析URL的pathinfo参数和变量
  815. * @access protected
  816. * @param string $url URL地址
  817. * @return array
  818. */
  819. protected function parseUrlPath($url)
  820. {
  821. // 分隔符替换 确保路由定义使用统一的分隔符
  822. $url = str_replace('|', '/', $url);
  823. $url = trim($url, '/');
  824. $var = [];
  825. if (false !== strpos($url, '?')) {
  826. // [模块/控制器/操作?]参数1=值1&参数2=值2...
  827. $info = parse_url($url);
  828. $path = explode('/', $info['path']);
  829. parse_str($info['query'], $var);
  830. } elseif (strpos($url, '/')) {
  831. // [模块/控制器/操作]
  832. $path = explode('/', $url);
  833. } elseif (false !== strpos($url, '=')) {
  834. // 参数1=值1&参数2=值2...
  835. parse_str($url, $var);
  836. } else {
  837. $path = [$url];
  838. }
  839. return [$path, $var];
  840. }
  841. /**
  842. * 生成路由的正则规则
  843. * @access protected
  844. * @param string $rule 路由规则
  845. * @param array $match 匹配的变量
  846. * @param array $pattern 路由变量规则
  847. * @param array $option 路由参数
  848. * @param bool $completeMatch 路由是否完全匹配
  849. * @param string $suffix 路由正则变量后缀
  850. * @return string
  851. */
  852. protected function buildRuleRegex($rule, $match, $pattern = [], $option = [], $completeMatch = false, $suffix = '')
  853. {
  854. foreach ($match as $name) {
  855. $replace[] = $this->buildNameRegex($name, $pattern, $suffix);
  856. }
  857. // 是否区分 / 地址访问
  858. if ('/' != $rule) {
  859. if (!empty($option['remove_slash'])) {
  860. $rule = rtrim($rule, '/');
  861. } elseif (substr($rule, -1) == '/') {
  862. $rule = rtrim($rule, '/');
  863. $hasSlash = true;
  864. }
  865. }
  866. $regex = str_replace($match, $replace, $rule);
  867. $regex = str_replace([')?/', ')/', ')?-', ')-', '\\\\/'], [')\/', ')\/', ')\-', ')\-', '\/'], $regex);
  868. if (isset($hasSlash)) {
  869. $regex .= '\/';
  870. }
  871. return $regex . ($completeMatch ? '$' : '');
  872. }
  873. /**
  874. * 生成路由变量的正则规则
  875. * @access protected
  876. * @param string $name 路由变量
  877. * @param string $pattern 变量规则
  878. * @param string $suffix 路由正则变量后缀
  879. * @return string
  880. */
  881. protected function buildNameRegex($name, $pattern, $suffix)
  882. {
  883. $optional = '';
  884. $slash = substr($name, 0, 1);
  885. if (in_array($slash, ['/', '-'])) {
  886. $prefix = '\\' . $slash;
  887. $name = substr($name, 1);
  888. $slash = substr($name, 0, 1);
  889. } else {
  890. $prefix = '';
  891. }
  892. if ('<' != $slash) {
  893. return $prefix . preg_quote($name, '/');
  894. }
  895. if (strpos($name, '?')) {
  896. $name = substr($name, 1, -2);
  897. $optional = '?';
  898. } elseif (strpos($name, '>')) {
  899. $name = substr($name, 1, -1);
  900. }
  901. if (isset($pattern[$name])) {
  902. $nameRule = $pattern[$name];
  903. if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) {
  904. $nameRule = substr($nameRule, 1, -1);
  905. }
  906. } else {
  907. $nameRule = $this->getConfig('default_route_pattern');
  908. }
  909. return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional;
  910. }
  911. /**
  912. * 分析路由规则中的变量
  913. * @access protected
  914. * @param string $rule 路由规则
  915. * @return array
  916. */
  917. protected function parseVar($rule)
  918. {
  919. // 提取路由规则中的变量
  920. $var = [];
  921. if (preg_match_all('/<\w+\??>/', $rule, $matches)) {
  922. foreach ($matches[0] as $name) {
  923. $optional = false;
  924. if (strpos($name, '?')) {
  925. $name = substr($name, 1, -2);
  926. $optional = true;
  927. } else {
  928. $name = substr($name, 1, -1);
  929. }
  930. $var[$name] = $optional ? 2 : 1;
  931. }
  932. }
  933. return $var;
  934. }
  935. /**
  936. * 设置路由参数
  937. * @access public
  938. * @param string $method 方法名
  939. * @param array $args 调用参数
  940. * @return $this
  941. */
  942. public function __call($method, $args)
  943. {
  944. if (count($args) > 1) {
  945. $args[0] = $args;
  946. }
  947. array_unshift($args, $method);
  948. return call_user_func_array([$this, 'option'], $args);
  949. }
  950. public function __sleep()
  951. {
  952. return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern', 'doAfter'];
  953. }
  954. public function __wakeup()
  955. {
  956. $this->router = Container::get('route');
  957. }
  958. }