From e3c49f243ce65f7c9123a00e3b8b620194cc2e18 Mon Sep 17 00:00:00 2001 From: SilverFire - Dima Naumenko Date: Mon, 9 Nov 2015 19:22:51 +0200 Subject: [PATCH] UrlRule::placeholders, UrlRule::substitutePlaceholderNames are now protected. Updated PHPDoc and UPDATE.md --- framework/UPGRADE.md | 5 +++++ framework/web/UrlRule.php | 49 +++++++++++++++++++---------------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index 1232e55..e21796c 100644 --- a/framework/UPGRADE.md +++ b/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`. +* 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 ---------------------- diff --git a/framework/web/UrlRule.php b/framework/web/UrlRule.php index daabcac..a95a500 100644 --- a/framework/web/UrlRule.php +++ b/framework/web/UrlRule.php @@ -43,8 +43,10 @@ class UrlRule extends Object implements UrlRuleInterface */ 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. * @see host + * @see placeholders */ public $pattern; /** @@ -87,7 +89,18 @@ class UrlRule extends Object implements UrlRuleInterface * @var boolean a value indicating if parameters should be url encoded. */ 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. */ @@ -104,18 +117,6 @@ class UrlRule extends Object implements UrlRuleInterface * @var array list of parameters used in the route. */ 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]; $pattern = isset($match[2][0]) ? $match[2][0] : '[^\/]+'; $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)) { $length = strlen($match[0][0]); $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. * Used in [[parseRequest()]], [[createUrl()]]. * * @param array $matches result of `preg_match()` call * @return array input array with replaced placeholder keys - * @see _placeholders + * @see placeholders * @since 2.0.7 */ - private function substitutePlaceholderNames (array $matches) { - foreach ($this->_placeholders as $placeholder => $name) { + protected function substitutePlaceholderNames (array $matches) { + foreach ($this->placeholders as $placeholder => $name) { if (isset($matches[$placeholder])) { $matches[$name] = $matches[$placeholder]; unset($matches[$placeholder]); @@ -390,14 +391,4 @@ class UrlRule extends Object implements UrlRuleInterface } 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; - } -} \ No newline at end of file +}