<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\route;
use Closure;
use think\Container;
use think\middleware\AllowCrossDomain;
use think\middleware\CheckRequestCache;
use think\middleware\FormTokenCheck;
use think\Request;
use think\Route;
use think\route\dispatch\Callback as CallbackDispatch;
use think\route\dispatch\Controller as ControllerDispatch;
abstract class Rule
{
protected $name;
protected $domain;
protected $router;
protected $parent;
protected $rule;
protected $route;
protected $method;
protected $vars = [];
protected $option = [];
protected $pattern = [];
protected $mergeOptions = ['model', 'append', 'middleware'];
abstract public function check(Request $request, string $url, bool $completeMatch = false);
public function option(array $option)
{
$this->option = array_merge($this->option, $option);
return $this;
}
public function setOption(string $name, $value)
{
$this->option[$name] = $value;
return $this;
}
public function pattern(array $pattern)
{
$this->pattern = array_merge($this->pattern, $pattern);
return $this;
}
public function name(string $name)
{
$this->name = $name;
return $this;
}
public function getRouter(): Route
{
return $this->router;
}
public function getName(): string
{
return $this->name ?: '';
}
public function getRule()
{
return $this->rule;
}
public function getRoute()
{
return $this->route;
}
public function getVars(): array
{
return $this->vars;
}
public function getParent()
{
return $this->parent;
}
public function getDomain(): string
{
return $this->domain ?: $this->parent->getDomain();
}
public function config(string $name = '')
{
return $this->router->config($name);
}
public function getPattern(string $name = '')
{
if ('' === $name) {
return $this->pattern;
}
return $this->pattern[$name] ?? null;
}
public function getOption(string $name = '', $default = null)
{
if ('' === $name) {
return $this->option;
}
return $this->option[$name] ?? $default;
}
public function getMethod(): string
{
return strtolower($this->method);
}
public function method(string $method)
{
return $this->setOption('method', strtolower($method));
}
public function ext(string $ext = '')
{
return $this->setOption('ext', $ext);
}
public function denyExt(string $ext = '')
{
return $this->setOption('deny_ext', $ext);
}
public function domain(string $domain)
{
$this->domain = $domain;
return $this->setOption('domain', $domain);
}
public function filter(array $filter)
{
$this->option['filter'] = $filter;
return $this;
}
public function model($var, $model = null, bool $exception = true)
{
if ($var instanceof Closure) {
$this->option['model'][] = $var;
} elseif (is_array($var)) {
$this->option['model'] = $var;
} elseif (is_null($model)) {
$this->option['model']['id'] = [$var, true];
} else {
$this->option['model'][$var] = [$model, $exception];
}
return $this;
}
public function append(array $append = [])
{
$this->option['append'] = $append;
return $this;
}
public function validate($validate, string $scene = null, array $message = [], bool $batch = false)
{
$this->option['validate'] = [$validate, $scene, $message, $batch];
return $this;
}
public function middleware($middleware, ...$params)
{
if (empty($params) && is_array($middleware)) {
$this->option['middleware'] = $middleware;
} else {
foreach ((array) $middleware as $item) {
$this->option['middleware'][] = [$item, $params];
}
}
return $this;
}
public function allowCrossDomain(array $header = [])
{
return $this->middleware(AllowCrossDomain::class, $header);
}
public function token(string $token = '__token__')
{
return $this->middleware(FormTokenCheck::class, $token);
}
public function cache($cache)
{
return $this->middleware(CheckRequestCache::class, $cache);
}
public function depr(string $depr)
{
return $this->setOption('param_depr', $depr);
}
public function mergeOptions(array $option = [])
{
$this->mergeOptions = array_merge($this->mergeOptions, $option);
return $this;
}
public function https(bool $https = true)
{
return $this->setOption('https', $https);
}
public function json(bool $json = true)
{
return $this->setOption('json', $json);
}
public function ajax(bool $ajax = true)
{
return $this->setOption('ajax', $ajax);
}
public function pjax(bool $pjax = true)
{
return $this->setOption('pjax', $pjax);
}
public function view(array $view = [])
{
return $this->setOption('view', $view);
}
public function completeMatch(bool $match = true)
{
return $this->setOption('complete_match', $match);
}
public function removeSlash(bool $remove = true)
{
return $this->setOption('remove_slash', $remove);
}
public function crossDomainRule()
{
if ($this instanceof RuleGroup) {
$method = '*';
} else {
$method = $this->method;
}
$this->router->setCrossDomainRule($this, $method);
return $this;
}
public function mergeGroupOptions(): array
{
$parentOption = $this->parent->getOption();
foreach ($this->mergeOptions as $item) {
if (isset($parentOption[$item]) && isset($this->option[$item])) {
$this->option[$item] = array_merge($parentOption[$item], $this->option[$item]);
}
}
$this->option = array_merge($parentOption, $this->option);
return $this->option;
}
public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch
{
if (is_string($route) && isset($option['prefix'])) {
$route = $option['prefix'] . $route;
}
if (is_string($route) && !empty($matches)) {
$search = $replace = [];
foreach ($matches as $key => $value) {
$search[] = '<' . $key . '>';
$replace[] = $value;
$search[] = ':' . $key;
$replace[] = $value;
}
$route = str_replace($search, $replace, $route);
}
$count = substr_count($rule, '/');
$url = array_slice(explode('|', $url), $count + 1);
$this->parseUrlParams(implode('|', $url), $matches);
$this->vars = $matches;
return $this->dispatch($request, $route, $option);
}
protected function dispatch(Request $request, $route, array $option): Dispatch
{
if (is_subclass_of($route, Dispatch::class)) {
$result = new $route($request, $this, $route, $this->vars);
} elseif ($route instanceof Closure) {
$result = new CallbackDispatch($request, $this, $route, $this->vars);
} elseif (false !== strpos($route, '@') || false !== strpos($route, '::')) {
$route = str_replace('::', '@', $route);
$result = $this->dispatchMethod($request, $route);
} else {
$result = $this->dispatchController($request, $route);
}
return $result;
}
protected function dispatchMethod(Request $request, string $route): CallbackDispatch
{
$path = $this->parseUrlPath($route);
$route = str_replace('/', '@', implode('/', $path));
$method = strpos($route, '@') ? explode('@', $route) : $route;
return new CallbackDispatch($request, $this, $method, $this->vars);
}
protected function dispatchController(Request $request, string $route): ControllerDispatch
{
$path = $this->parseUrlPath($route);
$action = array_pop($path);
$controller = !empty($path) ? array_pop($path) : null;
return new ControllerDispatch($request, $this, [$controller, $action], $this->vars);
}
protected function checkOption(array $option, Request $request): bool
{
if (!empty($option['method'])) {
if (is_string($option['method']) && false === stripos($option['method'], $request->method())) {
return false;
}
}
foreach (['ajax', 'pjax', 'json'] as $item) {
if (isset($option[$item])) {
$call = 'is' . $item;
if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) {
return false;
}
}
}
if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|'))
|| (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) {
return false;
}
if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) {
return false;
}
if ((isset($option['https']) && $option['https'] && !$request->isSsl())
|| (isset($option['https']) && !$option['https'] && $request->isSsl())) {
return false;
}
if (isset($option['filter'])) {
foreach ($option['filter'] as $name => $value) {
if ($request->param($name, '', null) != $value) {
return false;
}
}
}
return true;
}
protected function parseUrlParams(string $url, array &$var = []): void
{
if ($url) {
preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
$var[$match[1]] = strip_tags($match[2]);
}, $url);
}
}
public function parseUrlPath(string $url): array
{
$url = str_replace('|', '/', $url);
$url = trim($url, '/');
if (strpos($url, '/')) {
$path = explode('/', $url);
} else {
$path = [$url];
}
return $path;
}
protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string
{
foreach ($match as $name) {
$replace[] = $this->buildNameRegex($name, $pattern, $suffix);
}
if ('/' != $rule) {
if (!empty($option['remove_slash'])) {
$rule = rtrim($rule, '/');
} elseif (substr($rule, -1) == '/') {
$rule = rtrim($rule, '/');
$hasSlash = true;
}
}
$regex = str_replace(array_unique($match), array_unique($replace), $rule);
$regex = str_replace([')?/', ')/', ')?-', ')-', '\\\\/'], [')\/', ')\/', ')\-', ')\-', '\/'], $regex);
if (isset($hasSlash)) {
$regex .= '\/';
}
return $regex . ($completeMatch ? '$' : '');
}
protected function buildNameRegex(string $name, array $pattern, string $suffix): string
{
$optional = '';
$slash = substr($name, 0, 1);
if (in_array($slash, ['/', '-'])) {
$prefix = '\\' . $slash;
$name = substr($name, 1);
$slash = substr($name, 0, 1);
} else {
$prefix = '';
}
if ('<' != $slash) {
return $prefix . preg_quote($name, '/');
}
if (strpos($name, '?')) {
$name = substr($name, 1, -2);
$optional = '?';
} elseif (strpos($name, '>')) {
$name = substr($name, 1, -1);
}
if (isset($pattern[$name])) {
$nameRule = $pattern[$name];
if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) {
$nameRule = substr($nameRule, 1, -1);
}
} else {
$nameRule = $this->router->config('default_route_pattern');
}
return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional;
}
public function __call($method, $args)
{
if (count($args) > 1) {
$args[0] = $args;
}
array_unshift($args, $method);
return call_user_func_array([$this, 'setOption'], $args);
}
public function __sleep()
{
return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern'];
}
public function __wakeup()
{
$this->router = Container::pull('route');
}
public function __debugInfo()
{
return [
'name' => $this->name,
'rule' => $this->rule,
'route' => $this->route,
'method' => $this->method,
'vars' => $this->vars,
'option' => $this->option,
'pattern' => $this->pattern,
];
}
}