1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\roa\urlRules; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\{ |
7
|
|
|
base\InvalidConfigException, |
8
|
|
|
di\Instance, |
9
|
|
|
web\NotFoundHttpException, |
10
|
|
|
web\UrlManager, |
11
|
|
|
web\UrlNormalizer |
12
|
|
|
}; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Url rule that can call children rule when applicable. |
16
|
|
|
* |
17
|
|
|
* @author Angel (Faryshta) Guevara <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class Composite extends \yii\web\CompositeUrlRule |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var bool whether this rule must throw an `NotFoundHttpException` when |
23
|
|
|
* parse request fails. |
24
|
|
|
*/ |
25
|
|
|
public bool $strict = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string message used to create the `NotFoundHttpException` when |
29
|
|
|
* `$strict` equals `true` and no children rules could parse the request. |
30
|
|
|
*/ |
31
|
|
|
public string $notFoundMessage = 'Unknown route.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ?UrlNormalizer |
35
|
|
|
*/ |
36
|
|
|
protected ?UrlNormalizer $normalizer = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
21 |
|
*/ |
41
|
|
|
public function setNormalizer(UrlNormalizer|array $normalizer): void |
42
|
21 |
|
{ |
43
|
|
|
$this->normalizer = Instance::ensure($normalizer, UrlNormalizer::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param UrlManager $manager the URL manager |
48
|
21 |
|
* @return ?UrlNormalizer |
49
|
21 |
|
*/ |
50
|
|
|
protected function getNormalizer(UrlManager $manager): ?UrlNormalizer |
51
|
|
|
{ |
52
|
|
|
if (!$this->normalizer && $manager->normalizer) { |
53
|
|
|
$this->setNormalizer($manager->normalizer); |
|
|
|
|
54
|
|
|
} |
55
|
21 |
|
|
56
|
|
|
return $this->normalizer; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Determines if this rule must parse the request using the children rules |
61
|
|
|
* or return `false` inmediately. |
62
|
|
|
* |
63
|
|
|
* @param string $route |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
abstract protected function isApplicable(string $route): bool; |
67
|
|
|
|
68
|
|
|
/** |
69
|
21 |
|
* Ensures that `$rules` property is set |
70
|
|
|
*/ |
71
|
21 |
|
private function ensureRules() |
72
|
21 |
|
{ |
73
|
|
|
$this->rules ??= $this->createRules(); |
74
|
21 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
21 |
|
public function parseRequest($manager, $request) |
80
|
|
|
{ |
81
|
|
|
// only parse rules applicable rules |
82
|
21 |
|
if (!$this->isApplicable($request->pathInfo)) { |
83
|
1 |
|
return false; |
84
|
|
|
} |
85
|
21 |
|
$normalized = false; |
86
|
21 |
|
if ($this->hasNormalizer($manager)) { |
87
|
21 |
|
$request->pathInfo = $this->getNormalizer($manager) |
88
|
21 |
|
->normalizePathInfo( |
89
|
21 |
|
$request->pathInfo, |
90
|
21 |
|
'', |
91
|
21 |
|
$normalized |
92
|
|
|
); |
93
|
|
|
} |
94
|
21 |
|
$this->ensureRules(); |
95
|
21 |
|
$result = parent::parseRequest($manager, $request); |
96
|
21 |
|
if ($result === false && $this->strict === true) { |
97
|
|
|
throw $this->createNotFoundException(); |
98
|
|
|
} |
99
|
|
|
|
100
|
21 |
|
return $normalized |
101
|
1 |
|
? $this->getNormalizer($manager)->normalizeRoute($result) |
|
|
|
|
102
|
21 |
|
: $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
17 |
|
public function createUrl($manager, $route, $params) |
109
|
|
|
{ |
110
|
|
|
// only parse rules applicable rules |
111
|
17 |
|
if (!$this->isApplicable($route)) { |
112
|
16 |
|
return false; |
113
|
|
|
} |
114
|
17 |
|
$this->ensureRules(); |
115
|
|
|
|
116
|
17 |
|
return parent::createUrl($manager, $route, $params); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param UrlManager $manager the URL manager |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
21 |
|
protected function hasNormalizer($manager): bool |
124
|
|
|
{ |
125
|
21 |
|
return null !== $this->getNormalizer($manager); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return NotFoundHttpException |
130
|
|
|
*/ |
131
|
|
|
protected function createNotFoundException(): NotFoundHttpException |
132
|
21 |
|
{ |
133
|
|
|
return new NotFoundHttpException($this->notFoundMessage); |
134
|
21 |
|
} |
135
|
|
|
} |
136
|
|
|
|