<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\route;
use think\Route;
class Resource extends RuleGroup
{
protected $resource;
protected $route;
protected $rest = [];
protected $model = [];
protected $validate = [];
protected $middleware = [];
public function __construct(Route $router, RuleGroup $parent = null, string $name = '', string $route = '', array $rest = [])
{
$name = ltrim($name, '/');
$this->router = $router;
$this->parent = $parent;
$this->resource = $name;
$this->route = $route;
$this->name = strpos($name, '.') ? strstr($name, '.', true) : $name;
$this->setFullName();
$this->option['complete_match'] = true;
$this->rest = $rest;
if ($this->parent) {
$this->domain = $this->parent->getDomain();
$this->parent->addRuleItem($this);
}
if ($router->isTest()) {
$this->buildResourceRule();
}
}
protected function buildResourceRule(): void
{
$rule = $this->resource;
$option = $this->option;
$origin = $this->router->getGroup();
$this->router->setGroup($this);
if (strpos($rule, '.')) {
$array = explode('.', $rule);
$last = array_pop($array);
$item = [];
foreach ($array as $val) {
$item[] = $val . '/<' . ($option['var'][$val] ?? $val . '_id') . '>';
}
$rule = implode('/', $item) . '/' . $last;
}
$prefix = substr($rule, strlen($this->name) + 1);
foreach ($this->rest as $key => $val) {
if ((isset($option['only']) && !in_array($key, $option['only']))
|| (isset($option['except']) && in_array($key, $option['except']))) {
continue;
}
if (isset($last) && strpos($val[1], '<id>') && isset($option['var'][$last])) {
$val[1] = str_replace('<id>', '<' . $option['var'][$last] . '>', $val[1]);
} elseif (strpos($val[1], '<id>') && isset($option['var'][$rule])) {
$val[1] = str_replace('<id>', '<' . $option['var'][$rule] . '>', $val[1]);
}
$ruleItem = $this->addRule(trim($prefix . $val[1], '/'), $this->route . '/' . $val[2], $val[0]);
foreach (['model', 'validate', 'middleware', 'pattern'] as $name) {
if (isset($this->$name[$key])) {
call_user_func_array([$ruleItem, $name], (array) $this->$name[$key]);
}
}
}
$this->router->setGroup($origin);
}
public function only(array $only)
{
return $this->setOption('only', $only);
}
public function except(array $except)
{
return $this->setOption('except', $except);
}
public function vars(array $vars)
{
return $this->setOption('var', $vars);
}
public function withValidate($name, $validate = [])
{
if (is_array($name)) {
$this->validate = array_merge($this->validate, $name);
} else {
$this->validate[$name] = $validate;
}
return $this;
}
public function withModel($name, $model = [])
{
if (is_array($name)) {
$this->model = array_merge($this->model, $name);
} else {
$this->model[$name] = $model;
}
return $this;
}
public function withMiddleware($name, $middleware = [])
{
if (is_array($name)) {
$this->middleware = array_merge($this->middleware, $name);
} else {
$this->middleware[$name] = $middleware;
}
return $this;
}
public function rest($name, $resource = [])
{
if (is_array($name)) {
$this->rest = $resource ? $name : array_merge($this->rest, $name);
} else {
$this->rest[$name] = $resource;
}
return $this;
}
}