ROAResearch /
yii2-roa
| 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 | */ |
||||
| 41 | public function init() |
||||
| 42 | { |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * @inheritdoc |
||||
| 47 | */ |
||||
| 48 | public function setNormalizer(UrlNormalizer | array $normalizer): void |
||||
| 49 | { |
||||
| 50 | $this->normalizer = Instance::ensure($normalizer, UrlNormalizer::class); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * @param UrlManager $manager the URL manager |
||||
| 55 | * @return ?UrlNormalizer |
||||
| 56 | */ |
||||
| 57 | 21 | protected function getNormalizer(UrlManager $manager): ?UrlNormalizer |
|||
| 58 | { |
||||
| 59 | 21 | if (!$this->normalizer && $manager->normalizer) { |
|||
| 60 | $this->setNormalizer($manager->normalizer); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 61 | } |
||||
| 62 | |||||
| 63 | 21 | return $this->normalizer; |
|||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * Determines if this rule must parse the request using the children rules |
||||
| 68 | * or return `false` inmediately. |
||||
| 69 | * |
||||
| 70 | * @param string $route |
||||
| 71 | * @return bool |
||||
| 72 | */ |
||||
| 73 | abstract protected function isApplicable(string $route): bool; |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * Ensures that `$rules` property is set |
||||
| 77 | */ |
||||
| 78 | 21 | protected function ensureRules() |
|||
| 79 | { |
||||
| 80 | 21 | $this->rules = $this->rules ?: $this->createRules(); |
|||
| 81 | } |
||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * @inheritdoc |
||||
| 85 | */ |
||||
| 86 | 21 | public function parseRequest($manager, $request) |
|||
| 87 | { |
||||
| 88 | // only parse rules applicable rules |
||||
| 89 | 21 | if (!$this->isApplicable($request->pathInfo)) { |
|||
| 90 | 1 | return false; |
|||
| 91 | } |
||||
| 92 | |||||
| 93 | 21 | $normalized = false; |
|||
| 94 | 21 | if ($this->hasNormalizer($manager)) { |
|||
| 95 | 21 | $request->pathInfo = $this->getNormalizer($manager) |
|||
| 96 | 21 | ->normalizePathInfo( |
|||
| 97 | 21 | $request->pathInfo, |
|||
| 98 | '', |
||||
| 99 | $normalized |
||||
| 100 | ); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | 21 | $this->ensureRules(); |
|||
| 104 | 21 | $result = parent::parseRequest($manager, $request); |
|||
| 105 | |||||
| 106 | 21 | if ($result === false && $this->strict === true) { |
|||
| 107 | throw $this->createNotFoundException(); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | 21 | return $normalized |
|||
| 111 | 1 | ? $this->getNormalizer($manager)->normalizeRoute($result) |
|||
|
0 ignored issues
–
show
It seems like
$result can also be of type false; however, parameter $route of yii\web\UrlNormalizer::normalizeRoute() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 112 | 21 | : $result; |
|||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * @inheritdoc |
||||
| 117 | */ |
||||
| 118 | 15 | public function createUrl($manager, $route, $params) |
|||
| 119 | { |
||||
| 120 | // only parse rules applicable rules |
||||
| 121 | 15 | if (!$this->isApplicable($route)) { |
|||
| 122 | 14 | return false; |
|||
| 123 | } |
||||
| 124 | 15 | $this->ensureRules(); |
|||
| 125 | |||||
| 126 | 15 | return parent::createUrl($manager, $route, $params); |
|||
| 127 | } |
||||
| 128 | |||||
| 129 | /** |
||||
| 130 | * @param UrlManager $manager the URL manager |
||||
| 131 | * @return bool |
||||
| 132 | */ |
||||
| 133 | 21 | protected function hasNormalizer($manager): bool |
|||
| 134 | { |
||||
| 135 | 21 | return null !== $this->getNormalizer($manager); |
|||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * @return NotFoundHttpException |
||||
| 140 | */ |
||||
| 141 | protected function createNotFoundException(): NotFoundHttpException |
||||
| 142 | { |
||||
| 143 | return new NotFoundHttpException($this->notFoundMessage); |
||||
| 144 | } |
||||
| 145 | } |
||||
| 146 |