Browse Source

UrlRule::placeholders, UrlRule::substitutePlaceholderNames are now protected. Updated PHPDoc and UPDATE.md

tags/3.0.0-alpha1
SilverFire - Dima Naumenko 9 years ago
parent
commit
e3c49f243c
  1. 5
      framework/UPGRADE.md
  2. 49
      framework/web/UrlRule.php

5
framework/UPGRADE.md

@ -18,6 +18,11 @@ Upgrade from Yii 2.0.6
---------------------- ----------------------
* The signature of `yii\helpers\BaseInflector::transliterate()` was changed. The method is now public and has an extra optional parameter `$transliterator`. * The signature of `yii\helpers\BaseInflector::transliterate()` was changed. The method is now public and has an extra optional parameter `$transliterator`.
* If you override `yii\web\UrlRule` you have to know that the `pattern` matching group names are being replaced with the
placeholders on class initialization to support wider range of allowed characters. If you implement `parseRequest()` or
`createUrl()` and rely on parameter names, call `substitutePlaceholderNames()` in order to replace temporary IDs with
parameter names after doing matching.
Upgrade from Yii 2.0.5 Upgrade from Yii 2.0.5
---------------------- ----------------------

49
framework/web/UrlRule.php

@ -43,8 +43,10 @@ class UrlRule extends Object implements UrlRuleInterface
*/ */
public $name; public $name;
/** /**
* On the rule initialization, the [[pattern]] matching parameters names will be replaced with [[placeholders]].
* @var string the pattern used to parse and create the path info part of a URL. * @var string the pattern used to parse and create the path info part of a URL.
* @see host * @see host
* @see placeholders
*/ */
public $pattern; public $pattern;
/** /**
@ -87,7 +89,18 @@ class UrlRule extends Object implements UrlRuleInterface
* @var boolean a value indicating if parameters should be url encoded. * @var boolean a value indicating if parameters should be url encoded.
*/ */
public $encodeParams = true; public $encodeParams = true;
/**
* @var array list of placeholders for matching parameters names. Used in [[parseRequest()]], [[createUrl()]]
* On the rule initialization, the [[pattern]] parameters names will be replaced with placeholders.
* This array contains relations between the original parameters names and their placeholders.
* key - placeholder
* value - original name
*
* @see parseRequest()
* @see createUrl()
* @since 2.0.7
*/
protected $placeholders = [];
/** /**
* @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL. * @var string the template for generating a new URL. This is derived from [[pattern]] and is used in generating URL.
*/ */
@ -104,18 +117,6 @@ class UrlRule extends Object implements UrlRuleInterface
* @var array list of parameters used in the route. * @var array list of parameters used in the route.
*/ */
private $_routeParams = []; private $_routeParams = [];
/**
* @var array list of placeholders for matching parameters names. Used in [[parseRequest()]], [[createUrl()]]
* On the rule initialization, the [[pattern]] parameters names will be replaced with placeholders.
* This array contains relations between the original parameters names and their placeholders.
* key - placeholder
* value - original name
*
* @see parseRequest()
* @see createUrl()
* @since 2.0.7
*/
private $_placeholders = [];
/** /**
@ -185,7 +186,7 @@ class UrlRule extends Object implements UrlRuleInterface
$name = $match[1][0]; $name = $match[1][0];
$pattern = isset($match[2][0]) ? $match[2][0] : '[^\/]+'; $pattern = isset($match[2][0]) ? $match[2][0] : '[^\/]+';
$placeholder = 'a' . hash('crc32b', $name); // placeholder must begin with a letter $placeholder = 'a' . hash('crc32b', $name); // placeholder must begin with a letter
$this->_placeholders[$placeholder] = $name; $this->placeholders[$placeholder] = $name;
if (array_key_exists($name, $this->defaults)) { if (array_key_exists($name, $this->defaults)) {
$length = strlen($match[0][0]); $length = strlen($match[0][0]);
$offset = $match[0][1]; $offset = $match[0][1];
@ -372,17 +373,17 @@ class UrlRule extends Object implements UrlRuleInterface
} }
/** /**
* Iterates over [[_placeholders]] and checks whether each placeholder exists as a key in $matches array. * Iterates over [[placeholders]] and checks whether each placeholder exists as a key in $matches array.
* When found - replaces this placeholder key with a appropriate name of matching parameter. * When found - replaces this placeholder key with a appropriate name of matching parameter.
* Used in [[parseRequest()]], [[createUrl()]]. * Used in [[parseRequest()]], [[createUrl()]].
* *
* @param array $matches result of `preg_match()` call * @param array $matches result of `preg_match()` call
* @return array input array with replaced placeholder keys * @return array input array with replaced placeholder keys
* @see _placeholders * @see placeholders
* @since 2.0.7 * @since 2.0.7
*/ */
private function substitutePlaceholderNames (array $matches) { protected function substitutePlaceholderNames (array $matches) {
foreach ($this->_placeholders as $placeholder => $name) { foreach ($this->placeholders as $placeholder => $name) {
if (isset($matches[$placeholder])) { if (isset($matches[$placeholder])) {
$matches[$name] = $matches[$placeholder]; $matches[$name] = $matches[$placeholder];
unset($matches[$placeholder]); unset($matches[$placeholder]);
@ -390,14 +391,4 @@ class UrlRule extends Object implements UrlRuleInterface
} }
return $matches; return $matches;
} }
}
/**
* Returns list of placeholders and original names for matching parameters.
* @return array
* @since 2.0.7
* @see _placeholders
*/
protected function getPlaceholders() {
return $this->_placeholders;
}
}

Loading…
Cancel
Save