sfMessageSource_Aggregate.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfMessageSource_Aggregate aggregates several message source objects.
  11. *
  12. * @package symfony
  13. * @subpackage i18n
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfMessageSource_Aggregate.class.php 13401 2008-11-27 11:18:39Z fabien $
  16. */
  17. class sfMessageSource_Aggregate extends sfMessageSource
  18. {
  19. protected
  20. $messageSources = array();
  21. /**
  22. * Constructor.
  23. *
  24. * The order of the messages sources in the array is important.
  25. * This class will take the first translation found in the message sources.
  26. *
  27. * @param array $messageSources An array of message sources.
  28. *
  29. * @see MessageSource::factory();
  30. */
  31. function __construct($messageSources)
  32. {
  33. $this->messageSources = $messageSources;
  34. }
  35. public function setCulture($culture)
  36. {
  37. parent::setCulture($culture);
  38. foreach ($this->messageSources as $messageSource)
  39. {
  40. $messageSource->setCulture($culture);
  41. }
  42. }
  43. protected function getLastModified($sources)
  44. {
  45. $lastModified = time();
  46. foreach ($sources as $source)
  47. {
  48. $lastModified = min($lastModified, $source[0]->getLastModified($source[1]));
  49. }
  50. return $lastModified;
  51. }
  52. public function isValidSource($sources)
  53. {
  54. foreach ($sources as $source)
  55. {
  56. if (false === $source[0]->isValidSource($source[1]))
  57. {
  58. continue;
  59. }
  60. return true;
  61. }
  62. return false;
  63. }
  64. public function getSource($variant)
  65. {
  66. $sources = array();
  67. foreach ($this->messageSources as $messageSource)
  68. {
  69. $sources[] = array($messageSource, $messageSource->getSource(str_replace($messageSource->getId(), '', $variant)));
  70. }
  71. return $sources;
  72. }
  73. public function &loadData($sources)
  74. {
  75. $messages = array();
  76. foreach ($sources as $source)
  77. {
  78. if (false === $source[0]->isValidSource($source[1]))
  79. {
  80. continue;
  81. }
  82. $data = $source[0]->loadData($source[1]);
  83. if (is_array($data))
  84. {
  85. $messages = array_merge($data, $messages);
  86. }
  87. }
  88. return $messages;
  89. }
  90. public function getCatalogueList($catalogue)
  91. {
  92. $variants = array();
  93. foreach ($this->messageSources as $messageSource)
  94. {
  95. foreach ($messageSource->getCatalogueList($catalogue) as $variant)
  96. {
  97. $variants[] = $messageSource->getId().$variant;
  98. }
  99. }
  100. return $variants;
  101. }
  102. public function append($message)
  103. {
  104. // Append to the first message source only
  105. if (count($this->messageSources))
  106. {
  107. $this->messageSources[0]->append($message);
  108. }
  109. }
  110. public function update($text, $target, $comments, $catalogue = 'messages')
  111. {
  112. // Only update one message source
  113. foreach ($this->messageSources as $messageSource)
  114. {
  115. if ($messageSource->update($text, $target, $comments, $catalogue))
  116. {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. public function delete($message, $catalogue = 'messages')
  123. {
  124. $retval = false;
  125. foreach ($this->messageSources as $messageSource)
  126. {
  127. if ($messageSource->delete($message, $catalogue))
  128. {
  129. $retval = true;
  130. }
  131. }
  132. return $retval;
  133. }
  134. public function save($catalogue = 'messages')
  135. {
  136. $retval = false;
  137. foreach ($this->messageSources as $messageSource)
  138. {
  139. if ($messageSource->save($catalogue))
  140. {
  141. $retval = true;
  142. }
  143. }
  144. return $retval;
  145. }
  146. public function getId()
  147. {
  148. $id = '';
  149. foreach ($this->messageSources as $messageSource)
  150. {
  151. $id .= $messageSource->getId();
  152. }
  153. return md5($id);
  154. }
  155. public function catalogues()
  156. {
  157. throw new sfException('The "catalogues()" method is not implemented for this message source.');
  158. }
  159. }