EMongoMessageSource.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * CDbMessageSource class file.
  4. *
  5. * @author Zoltan Rajcsanyi <rajcsanyiz@gmail.com>
  6. * @copyright 2013 Zoltan Rajcsanyi
  7. * @license New BSD License
  8. * @category Database
  9. * @version 1.0
  10. */
  11. /**
  12. * EMongoMessageSource represents a message source that stores translated messages in MongoDB.
  13. *
  14. * PHP version 5.2+
  15. * MongoDB version >= 1.5.3
  16. * required extensions: MongoYii (for the configuration of the mongoDB connection)*
  17. *
  18. * The YiiMessages collection contains the following schema:
  19. * <pre>
  20. * _id: mongoId(),
  21. * category: string,
  22. * message: string,
  23. * translations: [language: string, message: string]
  24. * </pre>
  25. *
  26. * The 'YiiMessages' collection can be customized by setting {@link collectionName}.
  27. *
  28. * When {@link cachingDuration} is set as a positive number, message translations will be cached.
  29. *
  30. * @property EMongoConnection $emongoConnection The DB connection used for the message source.
  31. *
  32. * @author Zoltan Rajcsanyi <rajcsanyiz@gmail.com>
  33. * @copyright 2013 Zoltan Rajcsanyi
  34. * @license New BSD License
  35. * @category Database
  36. * @version 1.0
  37. */
  38. class EMongoMessageSource extends CMessageSource
  39. {
  40. const CACHE_KEY_PREFIX = 'Yii.EMongoMessageSource.';
  41. public $connectionID;
  42. /**
  43. * @var string name of collection to store messages and translations
  44. */
  45. public $collectionName = 'YiiMessages';
  46. /**
  47. * @var integer the time in seconds that the messages can remain valid in cache.
  48. * Defaults to 0, meaning the caching is disabled.
  49. */
  50. public $cachingDuration = 0;
  51. /**
  52. * @var string the ID of the cache application component that is used to cache the messages.
  53. * Defaults to 'cache' which refers to the primary cache application component.
  54. * Set this property to false if you want to disable caching the messages.
  55. */
  56. public $cacheID = 'cache';
  57. /**
  58. * @var EMongoClient the DB connection instance
  59. */
  60. private $_db;
  61. /**
  62. * Loads the message translation for the specified language and category.
  63. * @param string $category the message category
  64. * @param string $language the target language
  65. * @return array the loaded messages
  66. */
  67. protected function loadMessages($category,$language)
  68. {
  69. if($this->cachingDuration > 0 && $this->cacheID !== false && ($cache = Yii::app()->getComponent($this->cacheID)) !== null){
  70. $key = self::CACHE_KEY_PREFIX . '.messages.' . $category . '.' . $language;
  71. if(($data = $cache->get($key)) !== false){
  72. return unserialize($data);
  73. }
  74. }
  75. $messages = $this->loadMessagesFromDb($category, $language);
  76. if(isset($cache)){
  77. $cache->set($key, serialize($messages), $this->cachingDuration);
  78. }
  79. return $messages;
  80. }
  81. /**
  82. * @return EMongoClient the DB connection instance
  83. * @throws CException if {@link connectionID} does not point to a valid application component.
  84. */
  85. protected function getDbConnection()
  86. {
  87. if($this->_db !== null){
  88. return $this->_db;
  89. }elseif(($id = $this->connectionID) !== null){
  90. if(($this->_db = Yii::app()->getComponent($id)) instanceof CDbConnection){
  91. return $this->_db;
  92. }else{
  93. throw new CException(
  94. Yii::t(
  95. 'yii',
  96. 'EMongoCache.connectionID "{id}" is invalid. Please make sure it refers to the ID of a EMongoClient application component.',
  97. array('{id}' => $id)
  98. )
  99. );
  100. }
  101. }else{
  102. return $this->_db = Yii::app()->getComponent('mongodb');
  103. }
  104. }
  105. /**
  106. * Returns current MongoCollection object
  107. *
  108. * @return MongoCollection
  109. */
  110. protected function getCollection()
  111. {
  112. return $this->getDbConnection()->{$this->collectionName};
  113. }
  114. /**
  115. * Loads the messages from database.
  116. * You may override this method to customize the message storage in the database.
  117. * @param string $category the message category
  118. * @param string $language the target language
  119. * @return array the messages loaded from database
  120. * @since 1.1.5
  121. */
  122. protected function loadMessagesFromDb($category,$language)
  123. {
  124. $criteria = array('category' => $category, "translations.language" => $language);
  125. $fields = array('message' => true, 'translations.message' => true);
  126. $messages = $this->getCollection()->find($criteria, $fields);
  127. $result = array();
  128. foreach($messages as $message){
  129. $result[$message['message']] = $message['translations'][0]['message'];
  130. }
  131. return $result;
  132. }
  133. }