123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace think\route;
- use think\Container;
- use think\Loader;
- use think\Request;
- use think\Route;
- use think\route\dispatch\Callback as CallbackDispatch;
- use think\route\dispatch\Controller as ControllerDispatch;
- use think\route\dispatch\Module as ModuleDispatch;
- class Domain extends RuleGroup
- {
-
- public function __construct(Route $router, $name = '', $rule = null, $option = [], $pattern = [])
- {
- $this->router = $router;
- $this->domain = $name;
- $this->option = $option;
- $this->rule = $rule;
- $this->pattern = $pattern;
- }
-
- public function check($request, $url, $completeMatch = false)
- {
-
- $result = $this->checkRouteAlias($request, $url);
- if (false !== $result) {
- return $result;
- }
-
- $result = $this->checkUrlBind($request, $url);
- if (!empty($this->option['append'])) {
- $request->setRouteVars($this->option['append']);
- unset($this->option['append']);
- }
- if (false !== $result) {
- return $result;
- }
-
- if (!empty($this->option['middleware'])) {
- Container::get('middleware')->import($this->option['middleware']);
- unset($this->option['middleware']);
- }
- return parent::check($request, $url, $completeMatch);
- }
-
- public function bind($bind)
- {
- $this->router->bind($bind, $this->domain);
- return $this;
- }
-
- private function checkRouteAlias($request, $url)
- {
- $alias = strpos($url, '|') ? strstr($url, '|', true) : $url;
- $item = $this->router->getAlias($alias);
- return $item ? $item->check($request, $url) : false;
- }
-
- private function checkUrlBind($request, $url)
- {
- $bind = $this->router->getBind($this->domain);
- if (!empty($bind)) {
- $this->parseBindAppendParam($bind);
-
- Container::get('app')->log('[ BIND ] ' . var_export($bind, true));
-
- $type = substr($bind, 0, 1);
- $bind = substr($bind, 1);
- $bindTo = [
- '\\' => 'bindToClass',
- '@' => 'bindToController',
- ':' => 'bindToNamespace',
- ];
- if (isset($bindTo[$type])) {
- return $this->{$bindTo[$type]}($request, $url, $bind);
- }
- }
- return false;
- }
- protected function parseBindAppendParam(&$bind)
- {
- if (false !== strpos($bind, '?')) {
- list($bind, $query) = explode('?', $bind);
- parse_str($query, $vars);
- $this->append($vars);
- }
- }
-
- protected function bindToClass($request, $url, $class)
- {
- $array = explode('|', $url, 2);
- $action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
- $param = [];
- if (!empty($array[1])) {
- $this->parseUrlParams($request, $array[1], $param);
- }
- return new CallbackDispatch($request, $this, [$class, $action], $param);
- }
-
- protected function bindToNamespace($request, $url, $namespace)
- {
- $array = explode('|', $url, 3);
- $class = !empty($array[0]) ? $array[0] : $this->router->config('default_controller');
- $method = !empty($array[1]) ? $array[1] : $this->router->config('default_action');
- $param = [];
- if (!empty($array[2])) {
- $this->parseUrlParams($request, $array[2], $param);
- }
- return new CallbackDispatch($request, $this, [$namespace . '\\' . Loader::parseName($class, 1), $method], $param);
- }
-
- protected function bindToController($request, $url, $controller)
- {
- $array = explode('|', $url, 2);
- $action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
- $param = [];
- if (!empty($array[1])) {
- $this->parseUrlParams($request, $array[1], $param);
- }
- return new ControllerDispatch($request, $this, $controller . '/' . $action, $param);
- }
-
- protected function bindToModule($request, $url, $controller)
- {
- $array = explode('|', $url, 2);
- $action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
- $param = [];
- if (!empty($array[1])) {
- $this->parseUrlParams($request, $array[1], $param);
- }
- return new ModuleDispatch($request, $this, $controller . '/' . $action, $param);
- }
- }
|