diff --git a/library/CM/CacheConst.php b/library/CM/CacheConst.php index 7acfb7ba7..5fd5bc613 100644 --- a/library/CM/CacheConst.php +++ b/library/CM/CacheConst.php @@ -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'; diff --git a/library/CM/Model/Language.php b/library/CM/Model/Language.php index 47ba81bad..1a845da09 100644 --- a/library/CM/Model/Language.php +++ b/library/CM/Model/Language.php @@ -9,6 +9,13 @@ public function getName() { return $this->_get('name'); } + /** + * @param string $name + */ + public function setName($name) { + $this->_set('name', $name); + } + /** * @return string */ @@ -16,6 +23,13 @@ public function getAbbreviation() { return $this->_get('abbreviation'); } + /** + * @param string $abbreviation + */ + public function setAbbreviation($abbreviation) { + $this->_set('abbreviation', $abbreviation); + } + /** * @return bool */ @@ -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 */ @@ -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(); @@ -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 */ diff --git a/library/CM/Model/LanguageKey.php b/library/CM/Model/LanguageKey.php index 89ebab23b..767d01f49 100644 --- a/library/CM/Model/LanguageKey.php +++ b/library/CM/Model/LanguageKey.php @@ -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 @@ -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) . '`', ]; @@ -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 */ @@ -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 @@ -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())); } @@ -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 @@ -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; + } } diff --git a/tests/library/CM/Model/LanguageKeyTest.php b/tests/library/CM/Model/LanguageKeyTest.php index d1ac0ddc0..d6e2343a3 100644 --- a/tests/library/CM/Model/LanguageKeyTest.php +++ b/tests/library/CM/Model/LanguageKeyTest.php @@ -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')); + } } diff --git a/tests/library/CM/Model/LanguageTest.php b/tests/library/CM/Model/LanguageTest.php index c73165d4b..2cd128c32 100644 --- a/tests/library/CM/Model/LanguageTest.php +++ b/tests/library/CM/Model/LanguageTest.php @@ -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() {