| Total Complexity | 116 |
| Total Lines | 967 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Route often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Route, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Route |
||
|
1 ignored issue
–
show
|
|||
| 23 | { |
||
| 24 | /** |
||
| 25 | * REST定义 |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $rest = [ |
||
| 29 | 'index' => ['get', '', 'index'], |
||
| 30 | 'create' => ['get', '/create', 'create'], |
||
| 31 | 'edit' => ['get', '/<id>/edit', 'edit'], |
||
| 32 | 'read' => ['get', '/<id>', 'read'], |
||
| 33 | 'save' => ['post', '', 'save'], |
||
| 34 | 'update' => ['put', '/<id>', 'update'], |
||
| 35 | 'delete' => ['delete', '/<id>', 'delete'], |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * 请求方法前缀定义 |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $methodPrefix = [ |
||
| 43 | 'get' => 'get', |
||
| 44 | 'post' => 'post', |
||
| 45 | 'put' => 'put', |
||
| 46 | 'delete' => 'delete', |
||
| 47 | 'patch' => 'patch', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * 应用对象 |
||
| 52 | * @var App |
||
| 53 | */ |
||
| 54 | protected $app; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * 请求对象 |
||
| 58 | * @var Request |
||
| 59 | */ |
||
| 60 | protected $request; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * 当前HOST |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $host; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * 当前域名 |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $domain; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * 当前分组对象 |
||
| 76 | * @var RuleGroup |
||
| 77 | */ |
||
| 78 | protected $group; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * 配置参数 |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $config = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * 路由绑定 |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $bind = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * 域名对象 |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $domains = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * 跨域路由规则 |
||
| 100 | * @var RuleGroup |
||
| 101 | */ |
||
| 102 | protected $cross; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * 路由别名 |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | protected $alias = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * 路由是否延迟解析 |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $lazy = true; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * 路由是否测试模式 |
||
| 118 | * @var bool |
||
| 119 | */ |
||
| 120 | protected $isTest; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * (分组)路由规则是否合并解析 |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $mergeRuleRegex = true; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * 路由解析自动搜索多级控制器 |
||
| 130 | * @var bool |
||
| 131 | */ |
||
| 132 | protected $autoSearchController = true; |
||
| 133 | |||
| 134 | public function __construct(App $app, array $config = []) |
||
| 135 | { |
||
| 136 | $this->app = $app; |
||
| 137 | $this->request = $app['request']; |
||
| 138 | $this->config = $config; |
||
| 139 | |||
| 140 | $this->host = $this->request->host(true) ?: $config['app_host']; |
||
| 141 | |||
| 142 | $this->setDefaultDomain(); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function config($name = null) |
||
| 146 | { |
||
| 147 | if (is_null($name)) { |
||
| 148 | return $this->config; |
||
| 149 | } |
||
| 150 | |||
| 151 | return isset($this->config[$name]) ? $this->config[$name] : null; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * 配置 |
||
| 156 | * @access public |
||
| 157 | * @param array $config |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function setConfig(array $config = []) |
||
| 161 | { |
||
| 162 | $this->config = array_merge($this->config, array_change_key_case($config)); |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function __make(App $app, Config $config) |
||
|
1 ignored issue
–
show
|
|||
| 166 | { |
||
| 167 | $config = $config->pull('app'); |
||
| 168 | $route = new static($app, $config); |
||
| 169 | |||
| 170 | $route->lazy($config['url_lazy_route']) |
||
| 171 | ->autoSearchController($config['controller_auto_search']) |
||
| 172 | ->mergeRuleRegex($config['route_rule_merge']); |
||
| 173 | |||
| 174 | return $route; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * 设置路由的请求对象实例 |
||
| 179 | * @access public |
||
| 180 | * @param Request $request 请求对象实例 |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | public function setRequest($request) |
||
| 184 | { |
||
| 185 | $this->request = $request; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * 设置路由域名及分组(包括资源路由)是否延迟解析 |
||
| 190 | * @access public |
||
| 191 | * @param bool $lazy 路由是否延迟解析 |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function lazy($lazy = true) |
||
| 195 | { |
||
| 196 | $this->lazy = $lazy; |
||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * 设置路由为测试模式 |
||
| 202 | * @access public |
||
| 203 | * @param bool $test 路由是否测试模式 |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | public function setTestMode($test) |
||
| 207 | { |
||
| 208 | $this->isTest = $test; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * 检查路由是否为测试模式 |
||
| 213 | * @access public |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function isTest() |
||
| 217 | { |
||
| 218 | return $this->isTest; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * 设置路由域名及分组(包括资源路由)是否合并解析 |
||
| 223 | * @access public |
||
| 224 | * @param bool $merge 路由是否合并解析 |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function mergeRuleRegex($merge = true) |
||
| 228 | { |
||
| 229 | $this->mergeRuleRegex = $merge; |
||
| 230 | $this->group->mergeRuleRegex($merge); |
||
| 231 | |||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * 设置路由自动解析是否搜索多级控制器 |
||
| 237 | * @access public |
||
| 238 | * @param bool $auto 是否自动搜索多级控制器 |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function autoSearchController($auto = true) |
||
| 242 | { |
||
| 243 | $this->autoSearchController = $auto; |
||
| 244 | return $this; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * 初始化默认域名 |
||
| 249 | * @access protected |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | protected function setDefaultDomain() |
||
| 253 | { |
||
| 254 | // 默认域名 |
||
| 255 | $this->domain = $this->host; |
||
| 256 | |||
| 257 | // 注册默认域名 |
||
| 258 | $domain = new Domain($this, $this->host); |
||
| 259 | |||
| 260 | $this->domains[$this->host] = $domain; |
||
| 261 | |||
| 262 | // 默认分组 |
||
| 263 | $this->group = $domain; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * 设置当前域名 |
||
| 268 | * @access public |
||
| 269 | * @param RuleGroup $group 域名 |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function setGroup(RuleGroup $group) |
||
| 273 | { |
||
| 274 | $this->group = $group; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * 获取当前分组 |
||
| 279 | * @access public |
||
| 280 | * @return RuleGroup |
||
| 281 | */ |
||
| 282 | public function getGroup() |
||
| 283 | { |
||
| 284 | return $this->group; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * 注册变量规则 |
||
| 289 | * @access public |
||
| 290 | * @param string|array $name 变量名 |
||
| 291 | * @param string $rule 变量规则 |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function pattern($name, $rule = '') |
||
| 295 | { |
||
| 296 | $this->group->pattern($name, $rule); |
||
| 297 | |||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * 注册路由参数 |
||
| 303 | * @access public |
||
| 304 | * @param string|array $name 参数名 |
||
| 305 | * @param mixed $value 值 |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function option($name, $value = '') |
||
| 309 | { |
||
| 310 | $this->group->option($name, $value); |
||
| 311 | |||
| 312 | return $this; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * 注册域名路由 |
||
| 317 | * @access public |
||
| 318 | * @param string|array $name 子域名 |
||
| 319 | * @param mixed $rule 路由规则 |
||
| 320 | * @param array $option 路由参数 |
||
| 321 | * @param array $pattern 变量规则 |
||
| 322 | * @return Domain |
||
| 323 | */ |
||
| 324 | public function domain($name, $rule = '', $option = [], $pattern = []) |
||
| 325 | { |
||
| 326 | // 支持多个域名使用相同路由规则 |
||
| 327 | $domainName = is_array($name) ? array_shift($name) : $name; |
||
| 328 | |||
| 329 | if ('*' != $domainName && false === strpos($domainName, '.')) { |
||
| 330 | $domainName .= '.' . $this->request->rootDomain(); |
||
| 331 | } |
||
| 332 | |||
| 333 | if (!isset($this->domains[$domainName])) { |
||
| 334 | $domain = (new Domain($this, $domainName, $rule, $option, $pattern)) |
||
| 335 | ->lazy($this->lazy) |
||
| 336 | ->mergeRuleRegex($this->mergeRuleRegex); |
||
| 337 | |||
| 338 | $this->domains[$domainName] = $domain; |
||
| 339 | } else { |
||
| 340 | $domain = $this->domains[$domainName]; |
||
| 341 | $domain->parseGroupRule($rule); |
||
| 342 | } |
||
| 343 | |||
| 344 | if (is_array($name) && !empty($name)) { |
||
| 345 | $root = $this->request->rootDomain(); |
||
| 346 | foreach ($name as $item) { |
||
| 347 | if (false === strpos($item, '.')) { |
||
| 348 | $item .= '.' . $root; |
||
| 349 | } |
||
| 350 | |||
| 351 | $this->domains[$item] = $domainName; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | // 返回域名对象 |
||
| 356 | return $domain; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * 获取域名 |
||
| 361 | * @access public |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | public function getDomains() |
||
| 365 | { |
||
| 366 | return $this->domains; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * 设置路由绑定 |
||
| 371 | * @access public |
||
| 372 | * @param string $bind 绑定信息 |
||
| 373 | * @param string $domain 域名 |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function bind($bind, $domain = null) |
||
| 377 | { |
||
| 378 | $domain = is_null($domain) ? $this->domain : $domain; |
||
| 379 | |||
| 380 | $this->bind[$domain] = $bind; |
||
| 381 | |||
| 382 | return $this; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * 读取路由绑定 |
||
| 387 | * @access public |
||
| 388 | * @param string $domain 域名 |
||
| 389 | * @return string|null |
||
| 390 | */ |
||
| 391 | public function getBind($domain = null) |
||
| 392 | { |
||
| 393 | if (is_null($domain)) { |
||
| 394 | $domain = $this->domain; |
||
| 395 | } elseif (true === $domain) { |
||
| 396 | return $this->bind; |
||
| 397 | } elseif (false === strpos($domain, '.')) { |
||
| 398 | $domain .= '.' . $this->request->rootDomain(); |
||
| 399 | } |
||
| 400 | |||
| 401 | $subDomain = $this->request->subDomain(); |
||
| 402 | |||
| 403 | if (strpos($subDomain, '.')) { |
||
| 404 | $name = '*' . strstr($subDomain, '.'); |
||
| 405 | } |
||
| 406 | |||
| 407 | if (isset($this->bind[$domain])) { |
||
| 408 | $result = $this->bind[$domain]; |
||
| 409 | } elseif (isset($name) && isset($this->bind[$name])) { |
||
| 410 | $result = $this->bind[$name]; |
||
| 411 | } elseif (!empty($subDomain) && isset($this->bind['*'])) { |
||
| 412 | $result = $this->bind['*']; |
||
| 413 | } else { |
||
| 414 | $result = null; |
||
| 415 | } |
||
| 416 | |||
| 417 | return $result; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * 读取路由标识 |
||
| 422 | * @access public |
||
| 423 | * @param string $name 路由标识 |
||
| 424 | * @param string $domain 域名 |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | public function getName($name = null, $domain = null, $method = '*') |
||
| 428 | { |
||
| 429 | return $this->app['rule_name']->get($name, $domain, $method); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * 读取路由 |
||
| 434 | * @access public |
||
| 435 | * @param string $rule 路由规则 |
||
| 436 | * @param string $domain 域名 |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | public function getRule($rule, $domain = null) |
||
| 440 | { |
||
| 441 | if (is_null($domain)) { |
||
| 442 | $domain = $this->domain; |
||
| 443 | } |
||
| 444 | |||
| 445 | return $this->app['rule_name']->getRule($rule, $domain); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * 读取路由 |
||
| 450 | * @access public |
||
| 451 | * @param string $domain 域名 |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function getRuleList($domain = null) |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * 批量导入路由标识 |
||
| 461 | * @access public |
||
| 462 | * @param array $name 路由标识 |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | public function setName($name) |
||
| 466 | { |
||
| 467 | $this->app['rule_name']->import($name); |
||
| 468 | return $this; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * 导入配置文件的路由规则 |
||
| 473 | * @access public |
||
| 474 | * @param array $rules 路由规则 |
||
| 475 | * @param string $type 请求类型 |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | public function import(array $rules, $type = '*') |
||
| 479 | { |
||
| 480 | // 检查域名部署 |
||
| 481 | if (isset($rules['__domain__'])) { |
||
| 482 | foreach ($rules['__domain__'] as $key => $rule) { |
||
| 483 | $this->domain($key, $rule); |
||
| 484 | } |
||
| 485 | unset($rules['__domain__']); |
||
| 486 | } |
||
| 487 | |||
| 488 | // 检查变量规则 |
||
| 489 | if (isset($rules['__pattern__'])) { |
||
| 490 | $this->pattern($rules['__pattern__']); |
||
| 491 | unset($rules['__pattern__']); |
||
| 492 | } |
||
| 493 | |||
| 494 | // 检查路由别名 |
||
| 495 | if (isset($rules['__alias__'])) { |
||
| 496 | foreach ($rules['__alias__'] as $key => $val) { |
||
| 497 | $this->alias($key, $val); |
||
| 498 | } |
||
| 499 | unset($rules['__alias__']); |
||
| 500 | } |
||
| 501 | |||
| 502 | // 检查资源路由 |
||
| 503 | if (isset($rules['__rest__'])) { |
||
| 504 | foreach ($rules['__rest__'] as $key => $rule) { |
||
| 505 | $this->resource($key, $rule); |
||
| 506 | } |
||
| 507 | unset($rules['__rest__']); |
||
| 508 | } |
||
| 509 | |||
| 510 | // 检查路由规则(包含分组) |
||
| 511 | foreach ($rules as $key => $val) { |
||
| 512 | if (is_numeric($key)) { |
||
| 513 | $key = array_shift($val); |
||
| 514 | } |
||
| 515 | |||
| 516 | if (empty($val)) { |
||
| 517 | continue; |
||
| 518 | } |
||
| 519 | |||
| 520 | if (is_string($key) && 0 === strpos($key, '[')) { |
||
| 521 | $key = substr($key, 1, -1); |
||
| 522 | $this->group($key, $val); |
||
| 523 | } elseif (is_array($val)) { |
||
| 524 | $this->rule($key, $val[0], $type, $val[1], isset($val[2]) ? $val[2] : []); |
||
| 525 | } else { |
||
| 526 | $this->rule($key, $val, $type); |
||
| 527 | } |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * 注册路由规则 |
||
| 533 | * @access public |
||
| 534 | * @param string $rule 路由规则 |
||
| 535 | * @param mixed $route 路由地址 |
||
| 536 | * @param string $method 请求类型 |
||
| 537 | * @param array $option 路由参数 |
||
| 538 | * @param array $pattern 变量规则 |
||
| 539 | * @return RuleItem |
||
| 540 | */ |
||
| 541 | public function rule($rule, $route, $method = '*', array $option = [], array $pattern = []) |
||
| 542 | { |
||
| 543 | return $this->group->addRule($rule, $route, $method, $option, $pattern); |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * 设置跨域有效路由规则 |
||
| 548 | * @access public |
||
| 549 | * @param Rule $rule 路由规则 |
||
| 550 | * @param string $method 请求类型 |
||
| 551 | * @return $this |
||
| 552 | */ |
||
| 553 | public function setCrossDomainRule($rule, $method = '*') |
||
| 554 | { |
||
| 555 | if (!isset($this->cross)) { |
||
| 556 | $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex); |
||
| 557 | } |
||
| 558 | |||
| 559 | $this->cross->addRuleItem($rule, $method); |
||
| 560 | |||
| 561 | return $this; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * 批量注册路由规则 |
||
| 566 | * @access public |
||
| 567 | * @param array $rules 路由规则 |
||
| 568 | * @param string $method 请求类型 |
||
| 569 | * @param array $option 路由参数 |
||
| 570 | * @param array $pattern 变量规则 |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | public function rules($rules, $method = '*', array $option = [], array $pattern = []) |
||
| 574 | { |
||
| 575 | $this->group->addRules($rules, $method, $option, $pattern); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * 注册路由分组 |
||
| 580 | * @access public |
||
| 581 | * @param string|array $name 分组名称或者参数 |
||
| 582 | * @param array|\Closure $route 分组路由 |
||
| 583 | * @param array $option 路由参数 |
||
| 584 | * @param array $pattern 变量规则 |
||
| 585 | * @return RuleGroup |
||
| 586 | */ |
||
| 587 | public function group($name, $route, array $option = [], array $pattern = []) |
||
| 588 | { |
||
| 589 | if (is_array($name)) { |
||
| 590 | $option = $name; |
||
| 591 | $name = isset($option['name']) ? $option['name'] : ''; |
||
| 592 | } |
||
| 593 | |||
| 594 | return (new RuleGroup($this, $this->group, $name, $route, $option, $pattern)) |
||
| 595 | ->lazy($this->lazy) |
||
| 596 | ->mergeRuleRegex($this->mergeRuleRegex); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * 注册路由 |
||
| 601 | * @access public |
||
| 602 | * @param string $rule 路由规则 |
||
| 603 | * @param mixed $route 路由地址 |
||
| 604 | * @param array $option 路由参数 |
||
| 605 | * @param array $pattern 变量规则 |
||
| 606 | * @return RuleItem |
||
| 607 | */ |
||
| 608 | public function any($rule, $route = '', array $option = [], array $pattern = []) |
||
| 609 | { |
||
| 610 | return $this->rule($rule, $route, '*', $option, $pattern); |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * 注册GET路由 |
||
| 615 | * @access public |
||
| 616 | * @param string $rule 路由规则 |
||
| 617 | * @param mixed $route 路由地址 |
||
| 618 | * @param array $option 路由参数 |
||
| 619 | * @param array $pattern 变量规则 |
||
| 620 | * @return RuleItem |
||
| 621 | */ |
||
| 622 | public function get($rule, $route = '', array $option = [], array $pattern = []) |
||
| 623 | { |
||
| 624 | return $this->rule($rule, $route, 'GET', $option, $pattern); |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * 注册POST路由 |
||
| 629 | * @access public |
||
| 630 | * @param string $rule 路由规则 |
||
| 631 | * @param mixed $route 路由地址 |
||
| 632 | * @param array $option 路由参数 |
||
| 633 | * @param array $pattern 变量规则 |
||
| 634 | * @return RuleItem |
||
| 635 | */ |
||
| 636 | public function post($rule, $route = '', array $option = [], array $pattern = []) |
||
| 637 | { |
||
| 638 | return $this->rule($rule, $route, 'POST', $option, $pattern); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * 注册PUT路由 |
||
| 643 | * @access public |
||
| 644 | * @param string $rule 路由规则 |
||
| 645 | * @param mixed $route 路由地址 |
||
| 646 | * @param array $option 路由参数 |
||
| 647 | * @param array $pattern 变量规则 |
||
| 648 | * @return RuleItem |
||
| 649 | */ |
||
| 650 | public function put($rule, $route = '', array $option = [], array $pattern = []) |
||
| 651 | { |
||
| 652 | return $this->rule($rule, $route, 'PUT', $option, $pattern); |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * 注册DELETE路由 |
||
| 657 | * @access public |
||
| 658 | * @param string $rule 路由规则 |
||
| 659 | * @param mixed $route 路由地址 |
||
| 660 | * @param array $option 路由参数 |
||
| 661 | * @param array $pattern 变量规则 |
||
| 662 | * @return RuleItem |
||
| 663 | */ |
||
| 664 | public function delete($rule, $route = '', array $option = [], array $pattern = []) |
||
| 665 | { |
||
| 666 | return $this->rule($rule, $route, 'DELETE', $option, $pattern); |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * 注册PATCH路由 |
||
| 671 | * @access public |
||
| 672 | * @param string $rule 路由规则 |
||
| 673 | * @param mixed $route 路由地址 |
||
| 674 | * @param array $option 路由参数 |
||
| 675 | * @param array $pattern 变量规则 |
||
| 676 | * @return RuleItem |
||
| 677 | */ |
||
| 678 | public function patch($rule, $route = '', array $option = [], array $pattern = []) |
||
| 679 | { |
||
| 680 | return $this->rule($rule, $route, 'PATCH', $option, $pattern); |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * 注册资源路由 |
||
| 685 | * @access public |
||
| 686 | * @param string $rule 路由规则 |
||
| 687 | * @param string $route 路由地址 |
||
| 688 | * @param array $option 路由参数 |
||
| 689 | * @param array $pattern 变量规则 |
||
| 690 | * @return Resource |
||
| 691 | */ |
||
| 692 | public function resource($rule, $route = '', array $option = [], array $pattern = []) |
||
| 693 | { |
||
| 694 | return (new Resource($this, $this->group, $rule, $route, $option, $pattern, $this->rest)) |
||
| 695 | ->lazy($this->lazy); |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * 注册控制器路由 操作方法对应不同的请求前缀 |
||
| 700 | * @access public |
||
| 701 | * @param string $rule 路由规则 |
||
| 702 | * @param string $route 路由地址 |
||
| 703 | * @param array $option 路由参数 |
||
| 704 | * @param array $pattern 变量规则 |
||
| 705 | * @return RuleGroup |
||
| 706 | */ |
||
| 707 | public function controller($rule, $route = '', array $option = [], array $pattern = []) |
||
| 708 | { |
||
| 709 | $group = new RuleGroup($this, $this->group, $rule, null, $option, $pattern); |
||
| 710 | |||
| 711 | foreach ($this->methodPrefix as $type => $val) { |
||
| 712 | $group->addRule('<action>', $val . '<action>', $type); |
||
| 713 | } |
||
| 714 | |||
| 715 | return $group->prefix($route . '/'); |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * 注册视图路由 |
||
| 720 | * @access public |
||
| 721 | * @param string|array $rule 路由规则 |
||
| 722 | * @param string $template 路由模板地址 |
||
| 723 | * @param array $vars 模板变量 |
||
| 724 | * @param array $option 路由参数 |
||
| 725 | * @param array $pattern 变量规则 |
||
| 726 | * @return RuleItem |
||
| 727 | */ |
||
| 728 | public function view($rule, $template = '', array $vars = [], array $option = [], array $pattern = []) |
||
| 729 | { |
||
| 730 | return $this->rule($rule, $template, 'GET', $option, $pattern)->view($vars); |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * 注册重定向路由 |
||
| 735 | * @access public |
||
| 736 | * @param string|array $rule 路由规则 |
||
| 737 | * @param string $route 路由地址 |
||
| 738 | * @param array $status 状态码 |
||
| 739 | * @param array $option 路由参数 |
||
| 740 | * @param array $pattern 变量规则 |
||
| 741 | * @return RuleItem |
||
| 742 | */ |
||
| 743 | public function redirect($rule, $route = '', $status = 301, array $option = [], array $pattern = []) |
||
| 744 | { |
||
| 745 | return $this->rule($rule, $route, '*', $option, $pattern)->redirect()->status($status); |
||
| 746 | } |
||
| 747 | |||
| 748 | /** |
||
| 749 | * 注册别名路由 |
||
| 750 | * @access public |
||
| 751 | * @param string $rule 路由别名 |
||
| 752 | * @param string $route 路由地址 |
||
| 753 | * @param array $option 路由参数 |
||
| 754 | * @return AliasRule |
||
| 755 | */ |
||
| 756 | public function alias($rule, $route, array $option = []) |
||
| 757 | { |
||
| 758 | $aliasRule = new AliasRule($this, $this->group, $rule, $route, $option); |
||
| 759 | |||
| 760 | $this->alias[$rule] = $aliasRule; |
||
| 761 | |||
| 762 | return $aliasRule; |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * 获取别名路由定义 |
||
| 767 | * @access public |
||
| 768 | * @param string $name 路由别名 |
||
| 769 | * @return string|array|null |
||
| 770 | */ |
||
| 771 | public function getAlias($name = null) |
||
| 772 | { |
||
| 773 | if (is_null($name)) { |
||
| 774 | return $this->alias; |
||
| 775 | } |
||
| 776 | |||
| 777 | return isset($this->alias[$name]) ? $this->alias[$name] : null; |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * 设置不同请求类型下面的方法前缀 |
||
| 782 | * @access public |
||
| 783 | * @param string|array $method 请求类型 |
||
| 784 | * @param string $prefix 类型前缀 |
||
| 785 | * @return $this |
||
| 786 | */ |
||
| 787 | public function setMethodPrefix($method, $prefix = '') |
||
| 788 | { |
||
| 789 | if (is_array($method)) { |
||
| 790 | $this->methodPrefix = array_merge($this->methodPrefix, array_change_key_case($method)); |
||
| 791 | } else { |
||
| 792 | $this->methodPrefix[strtolower($method)] = $prefix; |
||
| 793 | } |
||
| 794 | |||
| 795 | return $this; |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * 获取请求类型的方法前缀 |
||
| 800 | * @access public |
||
| 801 | * @param string $method 请求类型 |
||
| 802 | * @param string $prefix 类型前缀 |
||
| 803 | * @return string|null |
||
| 804 | */ |
||
| 805 | public function getMethodPrefix($method) |
||
| 806 | { |
||
| 807 | $method = strtolower($method); |
||
| 808 | |||
| 809 | return isset($this->methodPrefix[$method]) ? $this->methodPrefix[$method] : null; |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * rest方法定义和修改 |
||
| 814 | * @access public |
||
| 815 | * @param string $name 方法名称 |
||
| 816 | * @param array|bool $resource 资源 |
||
| 817 | * @return $this |
||
| 818 | */ |
||
| 819 | public function rest($name, $resource = []) |
||
| 820 | { |
||
| 821 | if (is_array($name)) { |
||
| 822 | $this->rest = $resource ? $name : array_merge($this->rest, $name); |
||
| 823 | } else { |
||
| 824 | $this->rest[$name] = $resource; |
||
| 825 | } |
||
| 826 | |||
| 827 | return $this; |
||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * 获取rest方法定义的参数 |
||
| 832 | * @access public |
||
| 833 | * @param string $name 方法名称 |
||
| 834 | * @return array|null |
||
| 835 | */ |
||
| 836 | public function getRest($name = null) |
||
| 837 | { |
||
| 838 | if (is_null($name)) { |
||
| 839 | return $this->rest; |
||
| 840 | } |
||
| 841 | |||
| 842 | return isset($this->rest[$name]) ? $this->rest[$name] : null; |
||
| 843 | } |
||
| 844 | |||
| 845 | /** |
||
| 846 | * 注册未匹配路由规则后的处理 |
||
| 847 | * @access public |
||
| 848 | * @param string $route 路由地址 |
||
| 849 | * @param string $method 请求类型 |
||
| 850 | * @param array $option 路由参数 |
||
| 851 | * @return RuleItem |
||
| 852 | */ |
||
| 853 | public function miss($route, $method = '*', array $option = []) |
||
| 854 | { |
||
| 855 | return $this->group->addMissRule($route, $method, $option); |
||
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * 注册一个自动解析的URL路由 |
||
| 860 | * @access public |
||
| 861 | * @param string $route 路由地址 |
||
| 862 | * @return RuleItem |
||
| 863 | */ |
||
| 864 | public function auto($route) |
||
| 865 | { |
||
| 866 | return $this->group->addAutoRule($route); |
||
| 867 | } |
||
| 868 | |||
| 869 | /** |
||
| 870 | * 检测URL路由 |
||
| 871 | * @access public |
||
| 872 | * @param string $url URL地址 |
||
| 873 | * @param bool $must 是否强制路由 |
||
| 874 | * @return Dispatch |
||
| 875 | * @throws RouteNotFoundException |
||
| 876 | */ |
||
| 877 | public function check($url, $must = false) |
||
| 878 | { |
||
| 879 | // 自动检测域名路由 |
||
| 880 | $domain = $this->checkDomain(); |
||
| 881 | $url = str_replace($this->config['pathinfo_depr'], '|', $url); |
||
| 882 | |||
| 883 | $completeMatch = $this->config['route_complete_match']; |
||
| 884 | |||
| 885 | $result = $domain->check($this->request, $url, $completeMatch); |
||
| 886 | |||
| 887 | if (false === $result && !empty($this->cross)) { |
||
| 888 | // 检测跨域路由 |
||
| 889 | $result = $this->cross->check($this->request, $url, $completeMatch); |
||
| 890 | } |
||
| 891 | |||
| 892 | if (false !== $result) { |
||
| 893 | // 路由匹配 |
||
| 894 | return $result; |
||
| 895 | } elseif ($must) { |
||
| 896 | // 强制路由不匹配则抛出异常 |
||
| 897 | throw new RouteNotFoundException(); |
||
| 898 | } |
||
| 899 | |||
| 900 | // 默认路由解析 |
||
| 901 | return new UrlDispatch($this->request, $this->group, $url, [ |
||
| 902 | 'auto_search' => $this->autoSearchController, |
||
| 903 | ]); |
||
| 904 | } |
||
| 905 | |||
| 906 | /** |
||
| 907 | * 检测域名的路由规则 |
||
| 908 | * @access protected |
||
| 909 | * @return Domain |
||
| 910 | */ |
||
| 911 | protected function checkDomain() |
||
| 958 | } |
||
| 959 | |||
| 960 | /** |
||
| 961 | * 清空路由规则 |
||
| 962 | * @access public |
||
| 963 | * @return void |
||
| 964 | */ |
||
| 965 | public function clear() |
||
| 966 | { |
||
| 967 | $this->app['rule_name']->clear(); |
||
| 968 | $this->group->clear(); |
||
| 969 | } |
||
| 970 | |||
| 971 | /** |
||
| 972 | * 设置全局的路由分组参数 |
||
| 973 | * @access public |
||
| 974 | * @param string $method 方法名 |
||
| 975 | * @param array $args 调用参数 |
||
| 976 | * @return RuleGroup |
||
| 977 | */ |
||
| 978 | public function __call($method, $args) |
||
| 979 | { |
||
| 980 | return call_user_func_array([$this->group, $method], $args); |
||
| 981 | } |
||
| 982 | |||
| 983 | public function __debugInfo() |
||
| 984 | { |
||
| 985 | $data = get_object_vars($this); |
||
| 986 | unset($data['app'], $data['request']); |
||
| 987 | |||
| 988 | return $data; |
||
| 989 | } |
||
| 990 | } |
||
| 991 |