From f75f453ec4cfa455e738fc17acf992721cd579b1 Mon Sep 17 00:00:00 2001 From: Darking Date: Thu, 24 Dec 2015 23:21:03 +0300 Subject: [PATCH] Fixes #10475: Extracted `getUrlFromCache()` and `setRuleToCache()` protected methods from `yii\web\UrlManager::createUrl()` --- framework/CHANGELOG.md | 1 + framework/web/UrlManager.php | 50 +++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8933860b9a..1e38eede17 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -59,6 +59,7 @@ Yii Framework 2 Change Log - Enh #9893: `yii.js` handleAction enhanced to support for data-form attribute, so links can trigger specific forms (SamMousa) - Enh #10322: ActiveForm now respects formtarget attribute of submit button (AnatolyRugalev) - Enh #10451: Check of existence of `$_SERVER` in `\yii\web\Request` before using it (quantum13) +- Enh #10475: Extracted `getUrlFromCache()` and `setRuleToCache()` protected methods from `yii\web\UrlManager::createUrl()` (dmdark) - Enh #10487: `yii\helpers\BaseArrayHelper::index()` got a third parameter `$groupBy` to group the input array by the key in one or more dimensions (quantum13, silverfire, samdark) - Enh #10610: Added `BaseUrl::$urlManager` to be able to set URL manager used for creating URLs (samdark) - Enh #10631: Splitted gettng label and rendering cell in `yii\grid\DataColumn::renderHeaderCellContent()` to make code simpler (t-kanstantsin, samdark) diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index 2fd79e5e6d..a632ba02ee 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -322,28 +322,19 @@ class UrlManager extends Component } } - /* @var $rule UrlRule */ - $url = false; - if (isset($this->_ruleCache[$cacheKey])) { - foreach ($this->_ruleCache[$cacheKey] as $rule) { - if (($url = $rule->createUrl($this, $route, $params)) !== false) { - break; - } - } - } else { - $this->_ruleCache[$cacheKey] = []; - } + $url = $this->getUrlFromCache($cacheKey, $route, $params); if ($url === false) { $cacheable = true; foreach ($this->rules as $rule) { + /* @var $rule UrlRule */ if (!empty($rule->defaults) && $rule->mode !== UrlRule::PARSING_ONLY) { // if there is a rule with default values involved, the matching result may not be cached $cacheable = false; } if (($url = $rule->createUrl($this, $route, $params)) !== false) { if ($cacheable) { - $this->_ruleCache[$cacheKey][] = $rule; + $this->setRuleToCache($cacheKey, $rule); } break; } @@ -380,6 +371,41 @@ class UrlManager extends Component } } + /** + * Get URL from internal cache if exists + * @param string $cacheKey generated cache key to store data. + * @param string $route the route (e.g. `site/index`). + * @param array $params rule params. + * @return bool|string the created URL + * @see createUrl() + * @since 2.0.8 + */ + protected function getUrlFromCache($cacheKey, $route, $params) + { + if (!empty($this->_ruleCache[$cacheKey])) { + foreach ($this->_ruleCache[$cacheKey] as $rule) { + /* @var $rule UrlRule */ + if (($url = $rule->createUrl($this, $route, $params)) !== false) { + return $url; + } + } + } else { + $this->_ruleCache[$cacheKey] = []; + } + return false; + } + + /** + * Store rule (e.g. [[UrlRule]]) to internal cache + * @param $cacheKey + * @param UrlRuleInterface $rule + * @since 2.0.8 + */ + protected function setRuleToCache($cacheKey, UrlRuleInterface $rule) + { + $this->_ruleCache[$cacheKey][] = $rule; + } + /** * Creates an absolute URL using the given route and query parameters. *