123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace think\route;
- use think\Route;
- class Resource extends RuleGroup
- {
-
- protected $resource;
-
- protected $rest = [];
-
- public function __construct(Route $router, RuleGroup $parent = null, $name = '', $route = '', $option = [], $pattern = [], $rest = [])
- {
- $this->router = $router;
- $this->parent = $parent;
- $this->resource = $name;
- $this->route = $route;
- $this->name = strpos($name, '.') ? strstr($name, '.', true) : $name;
- $this->setFullName();
-
- $option['complete_match'] = true;
- $this->pattern = $pattern;
- $this->option = $option;
- $this->rest = $rest;
- if ($this->parent) {
- $this->domain = $this->parent->getDomain();
- $this->parent->addRuleItem($this);
- }
- if ($router->isTest()) {
- $this->buildResourceRule();
- }
- }
-
- protected function buildResourceRule()
- {
- $origin = $this->router->getGroup();
- $this->router->setGroup($this);
- $rule = $this->resource;
- $option = $this->option;
- if (strpos($rule, '.')) {
-
- $array = explode('.', $rule);
- $last = array_pop($array);
- $item = [];
- foreach ($array as $val) {
- $item[] = $val . '/<' . (isset($option['var'][$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]);
- }
- $this->addRule(trim($prefix . $val[1], '/'), $this->route . '/' . $val[2], $val[0]);
- }
- $this->router->setGroup($origin);
- }
-
- 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;
- }
- }
|