Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1360 from tomaszdurka/issue-1360
Browse files Browse the repository at this point in the history
Add Language setters, clear Language tree on changes
  • Loading branch information
tomaszdurka committed Aug 11, 2014
2 parents dde051f + 03ce478 commit d25453a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 60 deletions.
2 changes: 1 addition & 1 deletion library/CM/CacheConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class CM_CacheConst {

const Language_Default = 'Language_Default';

const Language_Tree = 'Language_Tree';
const LanguageKey_Tree = 'LanguageKey_Tree';

// _className:X_abstracts:X
const ClassChildren = 'ClassChildren';
Expand Down
71 changes: 35 additions & 36 deletions library/CM/Model/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ public function getName() {
return $this->_get('name');
}

/**
* @param string $name
*/
public function setName($name) {
$this->_set('name', $name);
}

/**
* @return string
*/
public function getAbbreviation() {
return $this->_get('abbreviation');
}

/**
* @param string $abbreviation
*/
public function setAbbreviation($abbreviation) {
$this->_set('abbreviation', $abbreviation);
}

/**
* @return bool
*/
Expand All @@ -37,6 +51,27 @@ public function getBackup() {
return $this->_get('backupId');
}

/**
* @param CM_Model_Language|null $language
*/
public function setBackup(CM_Model_Language $language = null) {
$this->_set('backupId', $language);
}

/**
* @param CM_Model_Language $language
* @return bool
*/
public function isBackingUp(CM_Model_Language $language) {
while (null !== $language) {
$language = $language->getBackup();
if ($this->equals($language)) {
return true;
}
}
return false;
}

/**
* @return CM_Paging_Translation_Language
*/
Expand Down Expand Up @@ -98,29 +133,6 @@ public function setTranslation($phrase, $value = null, array $variables = null)
$this->getTranslations()->set($phrase, $value, $variables);
}

/**
* @param CM_Model_Language|null $language
* @throws CM_Exception_Invalid
*/
public function setBackup(CM_Model_Language $language = null) {
$this->_set('backupId', $language);
$this->_change();
}

/**
* @param CM_Model_Language $language
* @return bool
*/
public function isBackingUp(CM_Model_Language $language) {
while (!is_null($language)) {
if ($this->equals($language)) {
return true;
}
$language = $language->getBackup();
}
return false;
}

public function toArray() {
$array = parent::toArray();
$array['abbreviation'] = $this->getAbbreviation();
Expand Down Expand Up @@ -203,19 +215,6 @@ public static function findDefault() {
return new static($languageId);
}

/**
* @return CM_Tree_Language
*/
public static function getTree() {
$cacheKey = CM_CacheConst::Language_Tree;
$cache = CM_Cache_Local::getInstance();
if (false === ($tree = $cache->get($cacheKey))) {
$tree = new CM_Tree_Language();
$cache->set($cacheKey, $tree);
}
return $tree;
}

/**
* @return int
*/
Expand Down
70 changes: 47 additions & 23 deletions library/CM/Model/LanguageKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ class CM_Model_LanguageKey extends CM_Model_Abstract {

const MAX_UPDATE_COUNT = 50;

/**
* @return string
*/
public function getName() {
return $this->_get('name');
}

/**
* @return string[]
*/
public function getVariables() {
if (!$this->_has('variables')) {
return array();
}
$variablesEncoded = $this->_get('variables');
return CM_Params::jsonDecode($variablesEncoded);
}

/**
* @param string[]|null $variables
* @throws CM_Exception_Invalid
Expand All @@ -20,8 +38,7 @@ public function setVariables(array $variables = null) {
$this->_increaseUpdateCount();
if ($this->_getUpdateCount() > self::MAX_UPDATE_COUNT) {
$message = [
'Variables for languageKey `' . $this->_get('name') . '` have been updated over ' . self::MAX_UPDATE_COUNT .
' times since release.',
'Variables for languageKey `' . $this->getName() . '` have been updated over ' . self::MAX_UPDATE_COUNT . ' times since release.',
'Previous variables: `' . CM_Util::var_line($previousVariables) . '`',
'Current variables: `' . CM_Util::var_line($variables) . '`',
];
Expand All @@ -30,17 +47,6 @@ public function setVariables(array $variables = null) {
}
}

/**
* @return string[]
*/
public function getVariables() {
if (!$this->_has('variables')) {
return array();
}
$variablesEncoded = $this->_get('variables');
return CM_Params::jsonDecode($variablesEncoded);
}

/**
* @return bool
*/
Expand All @@ -62,16 +68,6 @@ protected function _getUpdateCount() {
return $this->_get('updateCount');
}

protected function _getSchema() {
return new CM_Model_Schema_Definition(array(
'name' => array('type' => 'string'),
'variables' => array('type' => 'string', 'optional' => true),
'updateCountResetVersion' => array('type' => 'int', 'optional' => true),
'updateCount' => array('type' => 'int'),
'javascript' => array('type' => 'bool'),
));
}

protected function _increaseUpdateCount() {
$data = [
'updateCount' => $this->_getUpdateCount() + 1
Expand All @@ -89,6 +85,16 @@ protected function _getDeployVersion() {
return CM_App::getInstance()->getDeployVersion();
}

protected function _getSchema() {
return new CM_Model_Schema_Definition(array(
'name' => array('type' => 'string'),
'variables' => array('type' => 'string', 'optional' => true),
'updateCountResetVersion' => array('type' => 'int', 'optional' => true),
'updateCount' => array('type' => 'int'),
'javascript' => array('type' => 'bool'),
));
}

protected function _onDeleteBefore() {
CM_Db_Db::delete('cm_languageValue', array('languageKeyId' => $this->getId()));
}
Expand All @@ -101,6 +107,11 @@ protected function _getContainingCacheables() {
return $cacheables;
}

protected function _changeContainingCacheables() {
parent::_changeContainingCacheables();
CM_Cache_Local::getInstance()->delete(CM_CacheConst::LanguageKey_Tree);
}

/**
* @param string $name
* @param array|null $variables
Expand Down Expand Up @@ -173,4 +184,17 @@ public static function deleteByName($name) {
public static function getPersistenceClass() {
return 'CM_Model_StorageAdapter_Database';
}

/**
* @return CM_Tree_Language
*/
public static function getTree() {
$cacheKey = CM_CacheConst::LanguageKey_Tree;
$cache = CM_Cache_Local::getInstance();
if (false === ($tree = $cache->get($cacheKey))) {
$tree = new CM_Tree_Language();
$cache->set($cacheKey, $tree);
}
return $tree;
}
}
6 changes: 6 additions & 0 deletions tests/library/CM/Model/LanguageKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ public function testIncreaseUpdateCountNewDeploy() {
$this->assertSame($now + 1, $languageKey->_get('updateCountResetVersion'));
$this->assertSame(1, $languageKey->_get('updateCount'));
}

public function testGetTreeCaching() {
$this->assertNull(CM_Model_LanguageKey::getTree()->findNodeById('.foo'));
CM_Model_LanguageKey::create('.foo');
$this->assertNotNull(CM_Model_LanguageKey::getTree()->findNodeById('.foo'));
}
}
1 change: 1 addition & 0 deletions tests/library/CM/Model/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function testIsBackingUp() {
$backedUpLanguage = CM_Model_Language::create('Polish', 'pl', true, $language);
$this->assertTrue($language->isBackingUp($backedUpLanguage));
$this->assertFalse($backedUpLanguage->isBackingUp($language));
$this->assertFalse($language->isBackingUp($language));
}

public function testFindDefault() {
Expand Down

0 comments on commit d25453a

Please sign in to comment.