|
- <?php
-
-
- namespace yii\rest;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\Inflector;
- use yii\web\CompositeUrlRule;
-
-
- class UrlRule extends CompositeUrlRule
- {
-
-
- public $prefix;
-
-
- public $suffix;
-
-
- public $controller;
-
-
- public $only = [];
-
-
- public $except = [];
-
-
- public $extraPatterns = [];
-
-
- public $tokens = [
- '{id}' => '<id:\\d[\\d,]*>',
- ];
-
-
- public $patterns = [
- 'PUT,PATCH {id}' => 'update',
- 'DELETE {id}' => 'delete',
- 'GET,HEAD {id}' => 'view',
- 'POST' => 'create',
- 'GET,HEAD' => 'index',
- '{id}' => 'options',
- '' => 'options',
- ];
-
-
- public $ruleConfig = [
- 'class' => 'yii\web\UrlRule',
- ];
-
-
- public $pluralize = true;
-
-
-
-
- public function init()
- {
- if (empty($this->controller)) {
- throw new InvalidConfigException('"controller" must be set.');
- }
-
- $controllers = [];
- foreach ((array) $this->controller as $urlName => $controller) {
- if (is_int($urlName)) {
- $urlName = $this->pluralize ? Inflector::pluralize($controller) : $controller;
- }
- $controllers[$urlName] = $controller;
- }
- $this->controller = $controllers;
-
- $this->prefix = trim($this->prefix, '/');
-
- parent::init();
- }
-
-
-
- protected function createRules()
- {
- $only = array_flip($this->only);
- $except = array_flip($this->except);
- $patterns = $this->extraPatterns + $this->patterns;
- $rules = [];
- foreach ($this->controller as $urlName => $controller) {
- $prefix = trim($this->prefix . '/' . $urlName, '/');
- foreach ($patterns as $pattern => $action) {
- if (!isset($except[$action]) && (empty($only) || isset($only[$action]))) {
- $rules[$urlName][] = $this->createRule($pattern, $prefix, $controller . '/' . $action);
- }
- }
- }
-
- return $rules;
- }
-
-
-
- protected function createRule($pattern, $prefix, $action)
- {
- $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
- if (preg_match("/^((?:($verbs),)*($verbs))(?:\\s+(.*))?$/", $pattern, $matches)) {
- $verbs = explode(',', $matches[1]);
- $pattern = isset($matches[4]) ? $matches[4] : '';
- } else {
- $verbs = [];
- }
-
- $config = $this->ruleConfig;
- $config['verb'] = $verbs;
- $config['pattern'] = rtrim($prefix . '/' . strtr($pattern, $this->tokens), '/');
- $config['route'] = $action;
- if (!in_array('GET', $verbs)) {
- $config['mode'] = \yii\web\UrlRule::PARSING_ONLY;
- }
- $config['suffix'] = $this->suffix;
-
- return Yii::createObject($config);
- }
-
-
-
- public function parseRequest($manager, $request)
- {
- $pathInfo = $request->getPathInfo();
- foreach ($this->rules as $urlName => $rules) {
- if (strpos($pathInfo, $urlName) !== false) {
- foreach ($rules as $rule) {
-
- if (($result = $rule->parseRequest($manager, $request)) !== false) {
- Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__);
-
- return $result;
- }
- }
- }
- }
-
- return false;
- }
-
-
-
- public function createUrl($manager, $route, $params)
- {
- foreach ($this->controller as $urlName => $controller) {
- if (strpos($route, $controller) !== false) {
- foreach ($this->rules[$urlName] as $rule) {
-
- if (($url = $rule->createUrl($manager, $route, $params)) !== false) {
- return $url;
- }
- }
- }
- }
-
- return false;
- }
- }
|