Skip to content

Commit

Permalink
Merge pull request #116 from carspending/master
Browse files Browse the repository at this point in the history
Refactor so that persistence is extendable.
  • Loading branch information
mikehaertl authored Sep 30, 2017
2 parents 973f61c + 93d1bed commit 8b65782
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,21 +353,7 @@ protected function processLocaleUrl($normalized)
Yii::$app->language = $language;
Yii::trace("Language code found in URL. Setting application language to '$language'.", __METHOD__);
if ($this->enableLanguagePersistence) {
Yii::$app->session[$this->languageSessionKey] = $language;
Yii::trace("Persisting language '$language' in session.", __METHOD__);
if ($this->languageCookieDuration) {
$cookie = new Cookie(array_merge(
['httpOnly' => true],
$this->languageCookieOptions,
[
'name' => $this->languageCookieName,
'value' => $language,
'expire' => time() + (int) $this->languageCookieDuration,
]
));
Yii::$app->getResponse()->getCookies()->add($cookie);
Yii::trace("Persisting language '$language' in cookie.", __METHOD__);
}
$this->persistLanguage($language);
}

// "Reset" case: We called e.g. /fr/demo/page so the persisted language was set back to "fr".
Expand All @@ -380,12 +366,7 @@ protected function processLocaleUrl($normalized)
} else {
$language = null;
if ($this->enableLanguagePersistence) {
$language = Yii::$app->session->get($this->languageSessionKey);
$language!==null && Yii::trace("Found persisted language '$language' in session.", __METHOD__);
if ($language===null) {
$language = $this->_request->getCookies()->getValue($this->languageCookieName);
$language!==null && Yii::trace("Found persisted language '$language' in cookie.", __METHOD__);
}
$language = $this->loadPersistedLanguage();
}
if ($language===null && $this->enableLanguageDetection) {
foreach ($this->_request->getAcceptableLanguages() as $acceptable) {
Expand Down Expand Up @@ -420,6 +401,42 @@ protected function processLocaleUrl($normalized)
}
}

/**
* @param string $language the language code to persist in session and cookie
*/
protected function persistLanguage($language)
{
Yii::$app->session[$this->languageSessionKey] = $language;
Yii::trace("Persisting language '$language' in session.", __METHOD__);
if ($this->languageCookieDuration) {
$cookie = new Cookie(array_merge(
['httpOnly' => true],
$this->languageCookieOptions,
[
'name' => $this->languageCookieName,
'value' => $language,
'expire' => time() + (int) $this->languageCookieDuration,
]
));
Yii::$app->getResponse()->getCookies()->add($cookie);
Yii::trace("Persisting language '$language' in cookie.", __METHOD__);
}
}

/**
* @return string|null the persisted language code or null if none found
*/
protected function loadPersistedLanguage()
{
$language = Yii::$app->session->get($this->languageSessionKey);
$language!==null && Yii::trace("Found persisted language '$language' in session.", __METHOD__);
if ($language===null) {
$language = $this->_request->getCookies()->getValue($this->languageCookieName);
$language!==null && Yii::trace("Found persisted language '$language' in cookie.", __METHOD__);
}
return $language;
}

/**
* Tests whether the given code matches any of the configured languages.
*
Expand Down

0 comments on commit 8b65782

Please sign in to comment.