This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
forked from tyohan/MongoRecord
-
Notifications
You must be signed in to change notification settings - Fork 58
Use mongo as message source #67
Open
Alegzander
wants to merge
5
commits into
canni:master
Choose a base branch
from
Alegzander:i18n-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab32f3c
Added Message source class with model required to enable usage of mon…
Alegznder ae0a910
Deleted experimental version of file. Forgot to remove it.
Alegznder 05bf13c
Made things that forgot to make, and wrote some description. Starting…
Alegznder 6f1ee48
Defined some bugs. Fixed them.
Alegznder d296b93
Tests were successfull. Added comment.
Alegznder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* Created by JetBrains PhpStorm. | ||
* User: alegz | ||
* E-mail: [email protected] | ||
* Date: 12/1/12 | ||
* Time: 9:45 PM | ||
*/ | ||
|
||
/** | ||
* Including required model class file | ||
*/ | ||
require_once (__DIR__.DIRECTORY_SEPARATOR.'extra'.DIRECTORY_SEPARATOR.'EMongoI18nModel.php'); | ||
|
||
/** | ||
* EMongoMessageSource represents a message source that stores translate messages in mongo | ||
* | ||
* Databese should contain collection that will contain translations. | ||
* Collection name should be the same as set in $this->translateMessageCollection it is equal | ||
* to i18n by default. | ||
*/ | ||
class EMongoMessageSource extends CMessageSource | ||
{ | ||
const CACHE_KEY_PREFIX = "ext.YiiMongoDbSuite.EMongoMessageSource"; | ||
|
||
/** | ||
* @var string | ||
* @desc The id os database connection | ||
*/ | ||
public $connectionID = "mongodb"; | ||
|
||
/** | ||
* @var string | ||
* @desc Name of collection with messages and their translations | ||
*/ | ||
public $translateMessageCollection = "i18n"; | ||
|
||
/** | ||
* @var int | ||
* @desc The time in seconds that the message can remain a valid cache | ||
* default to 0, meaning that cache is disabled | ||
*/ | ||
public $cachingDuration = 0; | ||
|
||
/** | ||
* @var string | ||
* @desc cache id. Simple. Put yours if you don't like this | ||
*/ | ||
public $cacheID = 'cache'; | ||
|
||
/** | ||
* @param string $category | ||
* @param string $language | ||
* @return array|null | ||
*/ | ||
public function loadMessages($category, $language) | ||
{ | ||
if ( | ||
$this->cachingDuration > 0 && | ||
$this->cacheID !== false && | ||
($cache = Yii::app()->getComponent($this->cacheID)) !== null | ||
) | ||
{ | ||
$key = self::CACHE_KEY_PREFIX.'.messages.'.$category.'.'.$language; | ||
$data = $cache->get($key); | ||
|
||
if ($data !== false) | ||
return unserialize($data); | ||
} | ||
|
||
$messages = $this->loadMessageFromDb($category, $language); | ||
|
||
if (isset($cache)) | ||
$cache->set($key, serialize($messages), $this->cachingDuration); | ||
|
||
return $messages; | ||
} | ||
|
||
/** | ||
* @param $category | ||
* @param $language | ||
* @return array|null | ||
* @desc Method requests translation form mongo and returns request | ||
* result. Variable message contains array: | ||
* $message['someMessage'] = 'Some message in needed language' | ||
*/ | ||
protected function loadMessageFromDb($category, $language) | ||
{ | ||
//Getting model | ||
$i18n = $this->getMessageModel(); | ||
//Getting translations | ||
$translations = $i18n->getMessages($category, $language)->find(); | ||
|
||
//If we get null instead of object we might face problem trying to get messages | ||
if ($translations instanceof EMongoI18nModel) | ||
return $translations->messages; | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @return EMongoI18nModel | ||
* @throws CException | ||
* @desc Checks component and returns model | ||
*/ | ||
protected function getMessageModel() | ||
{ | ||
$_db = Yii::app()->getComponent($this->connectionID); | ||
|
||
/** | ||
* Checking wether we have correct db component | ||
*/ | ||
if (!$_db instanceof EMongoDB) | ||
{ | ||
throw new CException(Yii::t('app', 'EMongoMessageSource.connectionId is invalid. Please make sure "{id}" refers to a valid database application component.'), | ||
array("{id}" => $this->connectionID)); | ||
} | ||
|
||
return EMongoI18nModel::model(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* Created by JetBrains PhpStorm. | ||
* User: alegz | ||
* E-mail: [email protected] | ||
* Date: 12/1/12 | ||
* Time: 10:42 PM | ||
*/ | ||
|
||
class EMongoI18nModel extends EMongoDocument | ||
{ | ||
/** | ||
* @var string | ||
* @desc Category name | ||
*/ | ||
public $category; | ||
|
||
/** | ||
* @var string | ||
* @desc language code | ||
*/ | ||
public $language; | ||
|
||
/** | ||
* @var array | ||
* @desc Массив текстов | ||
*/ | ||
public $messages; | ||
|
||
/** | ||
* @param string $className | ||
* @return EMongoDocument | ||
*/ | ||
public static function model($className = __CLASS__) | ||
{ | ||
return parent::model($className); | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws EMongoException | ||
*/ | ||
public function getCollectionName() | ||
{ | ||
/** | ||
* @var CMongoMessageSource $messageComponent | ||
*/ | ||
$messageComponent = Yii::app()->getComponent("messages"); | ||
|
||
if (!$messageComponent instanceof EMongoMessageSource) | ||
throw new EMongoException(Yii::t('app', 'Component message is not the instance of EMongoMessageSource.')); | ||
|
||
return $messageComponent->translateMessageCollection; | ||
} | ||
|
||
/** | ||
* @param $category | ||
* @param $language | ||
* @return EMongoI18nModel | ||
* @desc Method adds category and language criterias to the query. | ||
*/ | ||
public function getMessages($category, $language) | ||
{ | ||
$this->getDbCriteria()->category = $category; | ||
$this->getDbCriteria()->language = $language; | ||
|
||
return $this; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove meaningless comments, please.