Issues (910)

framework/web/CompositeUrlRule.php (1 issue)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\BaseObject;
12
13
/**
14
 * CompositeUrlRule is the base class for URL rule classes that consist of multiple simpler rules.
15
 *
16
 * @property-read int|null $createUrlStatus Status of the URL creation after the last [[createUrl()]] call.
17
 * `null` if rule does not provide info about create status.
18
 *
19
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
abstract class CompositeUrlRule extends BaseObject implements UrlRuleInterface
23
{
24
    /**
25
     * @var UrlRuleInterface[] the URL rules contained in this composite rule.
26
     * This property is set in [[init()]] by the return value of [[createRules()]].
27
     */
28
    protected $rules = [];
29
    /**
30
     * @var int|null status of the URL creation after the last [[createUrl()]] call.
31
     * @since 2.0.12
32
     */
33
    protected $createStatus;
34
35
36
    /**
37
     * Creates the URL rules that should be contained within this composite rule.
38
     * @return UrlRuleInterface[] the URL rules
39
     */
40
    abstract protected function createRules();
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 14
    public function init()
46
    {
47 14
        parent::init();
48 14
        $this->rules = $this->createRules();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function parseRequest($manager, $request)
55
    {
56 1
        foreach ($this->rules as $rule) {
57
            /* @var $rule UrlRule */
58 1
            $result = $rule->parseRequest($manager, $request);
59 1
            if (YII_DEBUG) {
60 1
                Yii::debug([
61 1
                    'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
62 1
                    'match' => $result !== false,
63 1
                    'parent' => self::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
                    'parent' => /** @scrutinizer ignore-deprecated */ self::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
64 1
                ], __METHOD__);
65
            }
66 1
            if ($result !== false) {
67 1
                return $result;
68
            }
69
        }
70
71 1
        return false;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1
    public function createUrl($manager, $route, $params)
78
    {
79 1
        $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
80 1
        $url = $this->iterateRules($this->rules, $manager, $route, $params);
81 1
        if ($url !== false) {
82 1
            return $url;
83
        }
84
85 1
        if ($this->createStatus === UrlRule::CREATE_STATUS_SUCCESS) {
86
            // create status was not changed - there is no rules configured
87
            $this->createStatus = UrlRule::CREATE_STATUS_PARSING_ONLY;
88
        }
89
90 1
        return false;
91
    }
92
93
    /**
94
     * Iterates through specified rules and calls [[createUrl()]] for each of them.
95
     *
96
     * @param UrlRuleInterface[] $rules rules to iterate.
97
     * @param UrlManager $manager the URL manager
98
     * @param string $route the route. It should not have slashes at the beginning or the end.
99
     * @param array $params the parameters
100
     * @return bool|string the created URL, or `false` if none of specified rules cannot be used for creating this URL.
101
     * @see createUrl()
102
     * @since 2.0.12
103
     */
104 10
    protected function iterateRules($rules, $manager, $route, $params)
105
    {
106
        /* @var $rule UrlRule */
107 10
        foreach ($rules as $rule) {
108 10
            $url = $rule->createUrl($manager, $route, $params);
109 10
            if ($url !== false) {
110 10
                $this->createStatus = UrlRule::CREATE_STATUS_SUCCESS;
111 10
                return $url;
112
            }
113
            if (
114 10
                $this->createStatus === null
115 10
                || !method_exists($rule, 'getCreateUrlStatus')
116 10
                || $rule->getCreateUrlStatus() === null
117
            ) {
118
                $this->createStatus = null;
119
            } else {
120 10
                $this->createStatus |= $rule->getCreateUrlStatus();
121
            }
122
        }
123
124 10
        return false;
125
    }
126
127
    /**
128
     * Returns status of the URL creation after the last [[createUrl()]] call.
129
     *
130
     * For multiple rules statuses will be combined by bitwise `or` operator
131
     * (e.g. `UrlRule::CREATE_STATUS_PARSING_ONLY | UrlRule::CREATE_STATUS_PARAMS_MISMATCH`).
132
     *
133
     * @return int|null Status of the URL creation after the last [[createUrl()]] call. `null` if rule does not provide
134
     * info about create status.
135
     * @see createStatus
136
     * @see https://www.php.net/manual/en/language.operators.bitwise.php
137
     * @since 2.0.12
138
     */
139 3
    public function getCreateUrlStatus()
140
    {
141 3
        return $this->createStatus;
142
    }
143
}
144