Completed
Push — unique-validator-fix ( 963824...d331b7 )
by Alexander
29:51 queued 14:14
created

GroupUrlRule::createRules()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 15
nc 7
nop 0
crap 5.009
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * GroupUrlRule represents a collection of URL rules sharing the same prefix in their patterns and routes.
15
 *
16
 * GroupUrlRule is best used by a module which often uses module ID as the prefix for the URL rules.
17
 * For example, the following code creates a rule for the `admin` module:
18
 *
19
 * ```php
20
 * new GroupUrlRule([
21
 *     'prefix' => 'admin',
22
 *     'rules' => [
23
 *         'login' => 'user/login',
24
 *         'logout' => 'user/logout',
25
 *         'dashboard' => 'default/dashboard',
26
 *     ],
27
 * ]);
28
 *
29
 * // the above rule is equivalent to the following three rules:
30
 *
31
 * [
32
 *     'admin/login' => 'admin/user/login',
33
 *     'admin/logout' => 'admin/user/logout',
34
 *     'admin/dashboard' => 'admin/default/dashboard',
35
 * ]
36
 * ```
37
 *
38
 * The above example assumes the prefix for patterns and routes are the same. They can be made different
39
 * by configuring [[prefix]] and [[routePrefix]] separately.
40
 *
41
 * Using a GroupUrlRule is more efficient than directly declaring the individual rules it contains.
42
 * This is because GroupUrlRule can quickly determine if it should process a URL parsing or creation request
43
 * by simply checking if the prefix matches.
44
 *
45
 * @author Qiang Xue <[email protected]>
46
 * @since 2.0
47
 */
48
class GroupUrlRule extends CompositeUrlRule
49
{
50
    /**
51
     * @var array the rules contained within this composite rule. Please refer to [[UrlManager::rules]]
52
     * for the format of this property.
53
     * @see prefix
54
     * @see routePrefix
55
     */
56
    public $rules = [];
57
    /**
58
     * @var string the prefix for the pattern part of every rule declared in [[rules]].
59
     * The prefix and the pattern will be separated with a slash.
60
     */
61
    public $prefix;
62
    /**
63
     * @var string the prefix for the route part of every rule declared in [[rules]].
64
     * The prefix and the route will be separated with a slash.
65
     * If this property is not set, it will take the value of [[prefix]].
66
     */
67
    public $routePrefix;
68
    /**
69
     * @var array the default configuration of URL rules. Individual rule configurations
70
     * specified via [[rules]] will take precedence when the same property of the rule is configured.
71
     */
72
    public $ruleConfig = ['class' => 'yii\web\UrlRule'];
73
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function init()
79
    {
80 2
        $this->prefix = trim($this->prefix, '/');
81 2
        $this->routePrefix = $this->routePrefix === null ? $this->prefix : trim($this->routePrefix, '/');
82 2
        parent::init();
83 2
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 2
    protected function createRules()
89
    {
90 2
        $rules = [];
91 2
        foreach ($this->rules as $key => $rule) {
92 2
            if (!is_array($rule)) {
93
                $rule = [
94 2
                    'pattern' => ltrim($this->prefix . '/' . $key, '/'),
95 2
                    'route' => ltrim($this->routePrefix . '/' . $rule, '/'),
96
                ];
97 1
            } elseif (isset($rule['pattern'], $rule['route'])) {
98 1
                $rule['pattern'] = ltrim($this->prefix . '/' . $rule['pattern'], '/');
99 1
                $rule['route'] = ltrim($this->routePrefix . '/' . $rule['route'], '/');
100
            }
101
102 2
            $rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
103 2
            if (!$rule instanceof UrlRuleInterface) {
104
                throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.');
105
            }
106 2
            $rules[] = $rule;
107
        }
108 2
        return $rules;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 1
    public function parseRequest($manager, $request)
115
    {
116 1
        $pathInfo = $request->getPathInfo();
117 1
        if ($this->prefix === '' || strpos($pathInfo . '/', $this->prefix . '/') === 0) {
118 1
            return parent::parseRequest($manager, $request);
119
        } else {
120 1
            return false;
121
        }
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 1
    public function createUrl($manager, $route, $params)
128
    {
129 1
        if ($this->routePrefix === '' || strpos($route, $this->routePrefix . '/') === 0) {
130 1
            return parent::createUrl($manager, $route, $params);
131
        } else {
132 1
            $this->createStatus = UrlRule::CREATE_STATUS_ROUTE_MISMATCH;
133 1
            return false;
134
        }
135
    }
136
}
137