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

Debug toolbar