123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace yii\web;
- use Yii;
- use yii\base\InvalidConfigException;
- class GroupUrlRule extends CompositeUrlRule
- {
-
- public $rules = [];
-
- public $prefix;
-
- public $routePrefix;
-
- public $ruleConfig = ['class' => 'yii\web\UrlRule'];
-
- public function init()
- {
- $this->prefix = trim($this->prefix, '/');
- $this->routePrefix = $this->routePrefix === null ? $this->prefix : trim($this->routePrefix, '/');
- parent::init();
- }
-
- protected function createRules()
- {
- $rules = [];
- foreach ($this->rules as $key => $rule) {
- if (!is_array($rule)) {
- $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
- $verb = null;
- if (preg_match("/^((?:(?:$verbs),)*(?:$verbs))\\s+(.*)$/", $key, $matches)) {
- $verb = explode(',', $matches[1]);
- $key = $matches[2];
- }
- $rule = [
- 'pattern' => ltrim($this->prefix . '/' . $key, '/'),
- 'route' => ltrim($this->routePrefix . '/' . $rule, '/'),
- 'verb' => $verb
- ];
- } elseif (isset($rule['pattern'], $rule['route'])) {
- $rule['pattern'] = ltrim($this->prefix . '/' . $rule['pattern'], '/');
- $rule['route'] = ltrim($this->routePrefix . '/' . $rule['route'], '/');
- }
- $rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
- if (!$rule instanceof UrlRuleInterface) {
- throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.');
- }
- $rules[] = $rule;
- }
- return $rules;
- }
-
- public function parseRequest($manager, $request)
- {
- $pathInfo = $request->getPathInfo();
- if ($this->prefix === '' || strpos($pathInfo . '/', $this->prefix . '/') === 0) {
- return parent::parseRequest($manager, $request);
- }
- return false;
- }
-
- public function createUrl($manager, $route, $params)
- {
- if ($this->routePrefix === '' || strpos($route, $this->routePrefix . '/') === 0) {
- return parent::createUrl($manager, $route, $params);
- }
- $this->createStatus = UrlRule::CREATE_STATUS_ROUTE_MISMATCH;
- return false;
- }
- }
|