sfMessageSource_File.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * sfMessageSource_File class file.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD License.
  7. *
  8. * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  9. *
  10. * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  11. * The latest version of PRADO can be obtained from:
  12. * {@link http://prado.sourceforge.net/}
  13. *
  14. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  15. * @version $Id: sfMessageSource_File.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * sfMessageSource_File class.
  21. *
  22. * This is the base class for file based message sources like XLIFF or gettext.
  23. *
  24. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  25. * @version v1.0, last update on Fri Dec 24 16:18:44 EST 2004
  26. * @package symfony
  27. * @subpackage i18n
  28. */
  29. abstract class sfMessageSource_File extends sfMessageSource
  30. {
  31. /**
  32. * Separator between culture name and source.
  33. * @var string
  34. */
  35. protected $dataSeparator = '.';
  36. /**
  37. * Constructor.
  38. *
  39. * @param string $source the directory where the messages are stored.
  40. * @see MessageSource::factory();
  41. */
  42. function __construct($source)
  43. {
  44. $this->source = (string) $source;
  45. }
  46. /**
  47. * Gets the last modified unix-time for this particular catalogue+variant.
  48. * Just use the file modified time.
  49. *
  50. * @param string $source catalogue+variant
  51. * @return int last modified in unix-time format.
  52. */
  53. public function getLastModified($source)
  54. {
  55. return is_file($source) ? filemtime($source) : 0;
  56. }
  57. /**
  58. * Gets the message file for a specific message catalogue and cultural variant.
  59. *
  60. * @param string $variant message catalogue
  61. * @return string full path to the message file.
  62. */
  63. public function getSource($variant)
  64. {
  65. return $this->source.'/'.$variant;
  66. }
  67. /**
  68. * Determines if the message file source is valid.
  69. *
  70. * @param string $source message file
  71. * @return boolean true if valid, false otherwise.
  72. */
  73. public function isValidSource($source)
  74. {
  75. return is_file($source);
  76. }
  77. /**
  78. * Gets all the variants of a particular catalogue.
  79. *
  80. * @param string $catalogue catalogue name
  81. * @return array list of all variants for this catalogue.
  82. */
  83. public function getCatalogueList($catalogue)
  84. {
  85. $variants = explode('_', $this->culture);
  86. $source = $catalogue.$this->dataExt;
  87. $catalogues = array($source);
  88. $variant = null;
  89. for ($i = 0, $max = count($variants); $i < $max; $i++)
  90. {
  91. if (strlen($variants[$i]) > 0)
  92. {
  93. $variant .= $variant ? '_'.$variants[$i] : $variants[$i];
  94. $catalogues[] = $catalogue.$this->dataSeparator.$variant.$this->dataExt;
  95. }
  96. }
  97. $byDir = $this->getCatalogueByDir($catalogue);
  98. $catalogues = array_merge($byDir, array_reverse($catalogues));
  99. return $catalogues;
  100. }
  101. /**
  102. * Traverses through the directory structure to find the catalogues.
  103. * This should only be called by getCatalogueList()
  104. *
  105. * @param string $catalogue a particular catalogue.
  106. * @return array a list of catalogues.
  107. * @see getCatalogueList()
  108. */
  109. protected function getCatalogueByDir($catalogue)
  110. {
  111. $variants = explode('_', $this->culture);
  112. $catalogues = array();
  113. $variant = null;
  114. for ($i = 0, $max = count($variants); $i < $max; $i++)
  115. {
  116. if (strlen($variants[$i]) > 0)
  117. {
  118. $variant .= $variant ? '_'.$variants[$i] : $variants[$i];
  119. $catalogues[] = $variant.'/'.$catalogue.$this->dataExt;
  120. }
  121. }
  122. return array_reverse($catalogues);
  123. }
  124. /**
  125. * Returns a list of catalogue and its culture ID.
  126. * E.g. array('messages', 'en_AU')
  127. *
  128. * @return array list of catalogues
  129. * @see getCatalogues()
  130. */
  131. public function catalogues()
  132. {
  133. return $this->getCatalogues();
  134. }
  135. /**
  136. * Returns a list of catalogue and its culture ID. This takes care
  137. * of directory structures.
  138. * E.g. array('messages', 'en_AU')
  139. *
  140. * @return array list of catalogues
  141. */
  142. protected function getCatalogues($dir = null, $variant = null)
  143. {
  144. $dir = $dir ? $dir : $this->getSource($variant);
  145. $files = scandir($dir);
  146. $catalogue = array();
  147. foreach ($files as $file)
  148. {
  149. if (is_dir($dir.'/'.$file) && preg_match('/^[a-z]{2}(_[A-Z]{2,3})?$/', $file))
  150. {
  151. $catalogue = array_merge($catalogue, $this->getCatalogues($dir.'/'.$file, $file));
  152. }
  153. $pos = strpos($file, $this->dataExt);
  154. if ($pos > 0 && substr($file, -1 * strlen($this->dataExt)) == $this->dataExt)
  155. {
  156. $name = substr($file, 0, $pos);
  157. $dot = strrpos($name, $this->dataSeparator);
  158. $culture = $variant;
  159. $cat = $name;
  160. if (is_int($dot))
  161. {
  162. $culture = substr($name, $dot + 1,strlen($name));
  163. $cat = substr($name, 0, $dot);
  164. }
  165. $details[0] = $cat;
  166. $details[1] = $culture;
  167. $catalogue[] = $details;
  168. }
  169. }
  170. sort($catalogue);
  171. return $catalogue;
  172. }
  173. public function getId()
  174. {
  175. return md5($this->source);
  176. }
  177. }