Fixes #10475: Extracted getUrlFromCache() and setRuleToCache() protected methods from yii\web\UrlManager::createUrl()

This commit is contained in:
Darking
2015-12-24 23:21:03 +03:00
committed by Alexander Makarov
parent 5dab713840
commit f75f453ec4
2 changed files with 39 additions and 12 deletions

View File

@@ -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)

View File

@@ -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.
*