コード例 #1
0
 /**
  * Initializes this rule.
  */
 public function init()
 {
     if ($this->pattern === null) {
         throw new InvalidConfigException('UrlRule::pattern must be set.');
     }
     if ($this->route === null) {
         throw new InvalidConfigException('UrlRule::route must be set.');
     }
     if (is_array($this->normalizer)) {
         $normalizerConfig = array_merge(['class' => UrlNormalizer::className()], $this->normalizer);
         $this->normalizer = Yii::createObject($normalizerConfig);
     }
     if ($this->normalizer !== null && $this->normalizer !== false && !$this->normalizer instanceof UrlNormalizer) {
         throw new InvalidConfigException('Invalid config for UrlRule::normalizer.');
     }
     if ($this->verb !== null) {
         if (is_array($this->verb)) {
             foreach ($this->verb as $i => $verb) {
                 $this->verb[$i] = strtoupper($verb);
             }
         } else {
             $this->verb = [strtoupper($this->verb)];
         }
     }
     if ($this->name === null) {
         $this->name = $this->pattern;
     }
     $this->pattern = trim($this->pattern, '/');
     $this->route = trim($this->route, '/');
     if ($this->host !== null) {
         $this->host = rtrim($this->host, '/');
         $this->pattern = rtrim($this->host . '/' . $this->pattern, '/');
     } elseif ($this->pattern === '') {
         $this->_template = '';
         $this->pattern = '#^$#u';
         return;
     } elseif (($pos = strpos($this->pattern, '://')) !== false) {
         if (($pos2 = strpos($this->pattern, '/', $pos + 3)) !== false) {
             $this->host = substr($this->pattern, 0, $pos2);
         } else {
             $this->host = $this->pattern;
         }
     } else {
         $this->pattern = '/' . $this->pattern . '/';
     }
     if (strpos($this->route, '<') !== false && preg_match_all('/<([\\w._-]+)>/', $this->route, $matches)) {
         foreach ($matches[1] as $name) {
             $this->_routeParams[$name] = "<{$name}>";
         }
     }
     $tr = ['.' => '\\.', '*' => '\\*', '$' => '\\$', '[' => '\\[', ']' => '\\]', '(' => '\\(', ')' => '\\)'];
     $tr2 = [];
     if (preg_match_all('/<([\\w._-]+):?([^>]+)?>/', $this->pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
         foreach ($matches as $match) {
             $name = $match[1][0];
             $pattern = isset($match[2][0]) ? $match[2][0] : '[^\\/]+';
             $placeholder = 'a' . hash('crc32b', $name);
             // placeholder must begin with a letter
             $this->placeholders[$placeholder] = $name;
             if (array_key_exists($name, $this->defaults)) {
                 $length = strlen($match[0][0]);
                 $offset = $match[0][1];
                 if ($offset > 1 && $this->pattern[$offset - 1] === '/' && (!isset($this->pattern[$offset + $length]) || $this->pattern[$offset + $length] === '/')) {
                     $tr["/<{$name}>"] = "(/(?P<{$placeholder}>{$pattern}))?";
                 } else {
                     $tr["<{$name}>"] = "(?P<{$placeholder}>{$pattern})?";
                 }
             } else {
                 $tr["<{$name}>"] = "(?P<{$placeholder}>{$pattern})";
             }
             if (isset($this->_routeParams[$name])) {
                 $tr2["<{$name}>"] = "(?P<{$placeholder}>{$pattern})";
             } else {
                 $this->_paramRules[$name] = $pattern === '[^\\/]+' ? '' : "#^{$pattern}\$#u";
             }
         }
     }
     $this->_template = preg_replace('/<([\\w._-]+):?([^>]+)?>/', '<$1>', $this->pattern);
     $this->pattern = '#^' . trim(strtr($this->_template, $tr), '/') . '$#u';
     if (!empty($this->_routeParams)) {
         $this->_routeRule = '#^' . strtr($this->route, $tr2) . '$#u';
     }
 }
コード例 #2
0
ファイル: UrlManager.php プロジェクト: yiisoft/yii2
 /**
  * Initializes UrlManager.
  */
 public function init()
 {
     parent::init();
     if ($this->normalizer !== false) {
         $this->normalizer = Yii::createObject($this->normalizer);
         if (!$this->normalizer instanceof UrlNormalizer) {
             throw new InvalidConfigException('`' . get_class($this) . '::normalizer` should be an instance of `' . UrlNormalizer::className() . '` or its DI compatible configuration.');
         }
     }
     if (!$this->enablePrettyUrl || empty($this->rules)) {
         return;
     }
     if (is_string($this->cache)) {
         $this->cache = Yii::$app->get($this->cache, false);
     }
     if ($this->cache instanceof Cache) {
         $cacheKey = $this->cacheKey;
         $hash = md5(json_encode($this->rules));
         if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) {
             $this->rules = $data[0];
         } else {
             $this->rules = $this->buildRules($this->rules);
             $this->cache->set($cacheKey, [$this->rules, $hash]);
         }
     } else {
         $this->rules = $this->buildRules($this->rules);
     }
 }
コード例 #3
0
ファイル: UrlManager.php プロジェクト: Abbas-Hashemian/yii2
 /**
  * Initializes UrlManager.
  */
 public function init()
 {
     parent::init();
     if ($this->normalizer !== false) {
         $this->normalizer = Instance::ensure($this->normalizer, UrlNormalizer::className());
     }
     if (!$this->enablePrettyUrl || empty($this->rules)) {
         return;
     }
     if (is_string($this->cache)) {
         $this->cache = Yii::$app->get($this->cache, false);
     }
     if ($this->cache instanceof Cache) {
         $cacheKey = $this->cacheKey;
         $hash = md5(json_encode($this->rules));
         if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) {
             $this->rules = $data[0];
         } else {
             $this->rules = $this->buildRules($this->rules);
             $this->cache->set($cacheKey, [$this->rules, $hash]);
         }
     } else {
         $this->rules = $this->buildRules($this->rules);
     }
 }