<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\route;
use think\Exception;
use think\Request;
use think\Route;
class RuleItem extends Rule
{
protected $miss = false;
protected $autoOption = false;
public function __construct(Route $router, RuleGroup $parent, string $name = null, string $rule = '', $route = null, string $method = '*')
{
$this->router = $router;
$this->parent = $parent;
$this->name = $name;
$this->route = $route;
$this->method = $method;
$this->setRule($rule);
$this->router->setRule($this->rule, $this);
}
public function setMiss()
{
$this->miss = true;
return $this;
}
public function isMiss(): bool
{
return $this->miss;
}
public function setAutoOptions()
{
$this->autoOption = true;
return $this;
}
public function isAutoOptions(): bool
{
return $this->autoOption;
}
public function getSuffix()
{
if (isset($this->option['ext'])) {
$suffix = $this->option['ext'];
} elseif ($this->parent->getOption('ext')) {
$suffix = $this->parent->getOption('ext');
} else {
$suffix = null;
}
return $suffix;
}
public function setRule(string $rule): void
{
if ('', '<\1>'], $rule);
} else {
$this->rule = $rule;
}
$this->setRuleName();
}
public function name(string $name)
{
$this->name = $name;
$this->setRuleName(true);
return $this;
}
protected function setRuleName(bool $first = false): void
{
if ($this->name) {
$this->router->setName($this->name, $this, $first);
}
}
public function checkRule(Request $request, string $url, $match = null, bool $completeMatch = false)
{
if (!$this->checkOption($this->option, $request)) {
return false;
}
$option = $this->getOption();
$pattern = $this->getPattern();
$url = $this->urlSuffixCheck($request, $url, $option);
if (is_null($match)) {
$match = $this->match($url, $option, $pattern, $completeMatch);
}
if (false !== $match) {
return $this->parseRule($request, $this->rule, $this->route, $url, $option, $match);
}
return false;
}
public function check(Request $request, string $url, bool $completeMatch = false)
{
return $this->checkRule($request, $url, null, $completeMatch);
}
protected function urlSuffixCheck(Request $request, string $url, array $option = []): string
{
if (!empty($option['remove_slash']) && '/' != $this->rule) {
$this->rule = rtrim($this->rule, '/');
$url = rtrim($url, '|');
}
if (isset($option['ext'])) {
$url = preg_replace('/\.(' . $request->ext() . ')$/i', '', $url);
}
return $url;
}
private function match(string $url, array $option, array $pattern, bool $completeMatch)
{
if (isset($option['complete_match'])) {
$completeMatch = $option['complete_match'];
}
$depr = $this->router->config('pathinfo_depr');
if (isset($pattern['__url__']) && !preg_match(0 === strpos($pattern['__url__'], '/') ? $pattern['__url__'] : '/^' . $pattern['__url__'] . ($completeMatch ? '$' : '') . '/', str_replace('|', $depr, $url))) {
return false;
}
$var = [];
$url = $depr . str_replace('|', $depr, $url);
$rule = $depr . str_replace('/', $depr, $this->rule);
if ($depr == $rule && $depr != $url) {
return false;
}
if (false === strpos($rule, '<')) {
if (0 === strcasecmp($rule, $url) || (!$completeMatch && 0 === strncasecmp($rule . $depr, $url . $depr, strlen($rule . $depr)))) {
return $var;
}
return false;
}
$slash = preg_quote('/-' . $depr, '/');
if ($matchRule = preg_split('/[' . $slash . ']?<\w+\??>/', $rule, 2)) {
if ($matchRule[0] && 0 !== strncasecmp($rule, $url, strlen($matchRule[0]))) {
return false;
}
}
if (preg_match_all('/[' . $slash . ']?<?\w+\??>?/', $rule, $matches)) {
$regex = $this->buildRuleRegex($rule, $matches[0], $pattern, $option, $completeMatch);
try {
if (!preg_match('~^' . $regex . '~u', $url, $match)) {
return false;
}
} catch (\Exception $e) {
throw new Exception('route pattern error');
}
foreach ($match as $key => $val) {
if (is_string($key)) {
$var[$key] = $val;
}
}
}
return $var;
}
public function group(string $name)
{
$group = $this->router->getRuleName()->getGroup($name);
if ($group) {
$this->parent = $group;
$this->setRule($this->rule);
}
return $this;
}
}
== substr($rule, -1, 1)) {
$rule = substr($rule, 0, -1);
$this->option['complete_match'] = true;
}
$rule = '/' != $rule ? ltrim($rule, '/') : '';
if ($this->parent && $prefix = $this->parent->getFullName()) {
$rule = $prefix . ($rule ? '/' . ltrim($rule, '/') : '');
}
if (false !== strpos($rule, ':')) {
$this->rule = preg_replace(['/\[\:(\w+)\]/', '/\:(\w+)/'], ['<\1?>', '<\1>'], $rule);
} else {
$this->rule = $rule;
}
$this->setRuleName();
}
public function name(string $name)
{
$this->name = $name;
$this->setRuleName(true);
return $this;
}
protected function setRuleName(bool $first = false): void
{
if ($this->name) {
$this->router->setName($this->name, $this, $first);
}
}
public function checkRule(Request $request, string $url, $match = null, bool $completeMatch = false)
{
if (!$this->checkOption($this->option, $request)) {
return false;
}
$option = $this->getOption();
$pattern = $this->getPattern();
$url = $this->urlSuffixCheck($request, $url, $option);
if (is_null($match)) {
$match = $this->match($url, $option, $pattern, $completeMatch);
}
if (false !== $match) {
return $this->parseRule($request, $this->rule, $this->route, $url, $option, $match);
}
return false;
}
public function check(Request $request, string $url, bool $completeMatch = false)
{
return $this->checkRule($request, $url, null, $completeMatch);
}
protected function urlSuffixCheck(Request $request, string $url, array $option = []): string
{
if (!empty($option['remove_slash']) && '/' != $this->rule) {
$this->rule = rtrim($this->rule, '/');
$url = rtrim($url, '|');
}
if (isset($option['ext'])) {
$url = preg_replace('/\.(' . $request->ext() . ')$/i', '', $url);
}
return $url;
}
private function match(string $url, array $option, array $pattern, bool $completeMatch)
{
if (isset($option['complete_match'])) {
$completeMatch = $option['complete_match'];
}
$depr = $this->router->config('pathinfo_depr');
if (isset($pattern['__url__']) && !preg_match(0 === strpos($pattern['__url__'], '/') ? $pattern['__url__'] : '/^' . $pattern['__url__'] . ($completeMatch ? '$' : '') . '/', str_replace('|', $depr, $url))) {
return false;
}
$var = [];
$url = $depr . str_replace('|', $depr, $url);
$rule = $depr . str_replace('/', $depr, $this->rule);
if ($depr == $rule && $depr != $url) {
return false;
}
if (false === strpos($rule, '<')) {
if (0 === strcasecmp($rule, $url) || (!$completeMatch && 0 === strncasecmp($rule . $depr, $url . $depr, strlen($rule . $depr)))) {
return $var;
}
return false;
}
$slash = preg_quote('/-' . $depr, '/');
if ($matchRule = preg_split('/[' . $slash . ']?<\w+\??>/', $rule, 2)) {
if ($matchRule[0] && 0 !== strncasecmp($rule, $url, strlen($matchRule[0]))) {
return false;
}
}
if (preg_match_all('/[' . $slash . ']?{{{PHP2}}}?/', $rule, $matches)) {
$regex = $this->buildRuleRegex($rule, $matches[0], $pattern, $option, $completeMatch);
try {
if (!preg_match('~^' . $regex . '~u', $url, $match)) {
return false;
}
} catch (\Exception $e) {
throw new Exception('route pattern error');
}
foreach ($match as $key => $val) {
if (is_string($key)) {
$var[$key] = $val;
}
}
}
return $var;
}
public function group(string $name)
{
$group = $this->router->getRuleName()->getGroup($name);
if ($group) {
$this->parent = $group;
$this->setRule($this->rule);
}
return $this;
}
}