BundleEntryReader.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Data\Bundle\Reader;
  11. use Symfony\Component\Intl\Exception\MissingResourceException;
  12. use Symfony\Component\Intl\Exception\OutOfBoundsException;
  13. use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
  14. use Symfony\Component\Intl\Locale;
  15. use Symfony\Component\Intl\Data\Util\RecursiveArrayAccess;
  16. /**
  17. * Default implementation of {@link BundleEntryReaderInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @see BundleEntryReaderInterface
  22. *
  23. * @internal
  24. */
  25. class BundleEntryReader implements BundleEntryReaderInterface
  26. {
  27. /**
  28. * @var BundleReaderInterface
  29. */
  30. private $reader;
  31. /**
  32. * A mapping of locale aliases to locales.
  33. *
  34. * @var array
  35. */
  36. private $localeAliases = array();
  37. /**
  38. * Creates an entry reader based on the given resource bundle reader.
  39. *
  40. * @param BundleReaderInterface $reader A resource bundle reader to use
  41. */
  42. public function __construct(BundleReaderInterface $reader)
  43. {
  44. $this->reader = $reader;
  45. }
  46. /**
  47. * Stores a mapping of locale aliases to locales.
  48. *
  49. * This mapping is used when reading entries and merging them with their
  50. * fallback locales. If an entry is read for a locale alias (e.g. "mo")
  51. * that points to a locale with a fallback locale ("ro_MD"), the reader
  52. * can continue at the correct fallback locale ("ro").
  53. *
  54. * @param array $localeAliases A mapping of locale aliases to locales
  55. */
  56. public function setLocaleAliases($localeAliases)
  57. {
  58. $this->localeAliases = $localeAliases;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function read($path, $locale)
  64. {
  65. return $this->reader->read($path, $locale);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function readEntry($path, $locale, array $indices, $fallback = true)
  71. {
  72. $entry = null;
  73. $isMultiValued = false;
  74. $readSucceeded = false;
  75. $exception = null;
  76. $currentLocale = $locale;
  77. $testedLocales = array();
  78. while (null !== $currentLocale) {
  79. // Resolve any aliases to their target locales
  80. if (isset($this->localeAliases[$currentLocale])) {
  81. $currentLocale = $this->localeAliases[$currentLocale];
  82. }
  83. try {
  84. $data = $this->reader->read($path, $currentLocale);
  85. $currentEntry = RecursiveArrayAccess::get($data, $indices);
  86. $readSucceeded = true;
  87. $isCurrentTraversable = $currentEntry instanceof \Traversable;
  88. $isCurrentMultiValued = $isCurrentTraversable || is_array($currentEntry);
  89. // Return immediately if fallback is disabled or we are dealing
  90. // with a scalar non-null entry
  91. if (!$fallback || (!$isCurrentMultiValued && null !== $currentEntry)) {
  92. return $currentEntry;
  93. }
  94. // =========================================================
  95. // Fallback is enabled, entry is either multi-valued or NULL
  96. // =========================================================
  97. // If entry is multi-valued, convert to array
  98. if ($isCurrentTraversable) {
  99. $currentEntry = iterator_to_array($currentEntry);
  100. }
  101. // If previously read entry was multi-valued too, merge them
  102. if ($isCurrentMultiValued && $isMultiValued) {
  103. $currentEntry = array_merge($currentEntry, $entry);
  104. }
  105. // Keep the previous entry if the current entry is NULL
  106. if (null !== $currentEntry) {
  107. $entry = $currentEntry;
  108. }
  109. // If this or the previous entry was multi-valued, we are dealing
  110. // with a merged, multi-valued entry now
  111. $isMultiValued = $isMultiValued || $isCurrentMultiValued;
  112. } catch (ResourceBundleNotFoundException $e) {
  113. // Continue if there is a fallback locale for the current
  114. // locale
  115. $exception = $e;
  116. } catch (OutOfBoundsException $e) {
  117. // Remember exception and rethrow if we cannot find anything in
  118. // the fallback locales either
  119. $exception = $e;
  120. }
  121. // Remember which locales we tried
  122. $testedLocales[] = $currentLocale;
  123. // Check whether fallback is allowed
  124. if (!$fallback) {
  125. break;
  126. }
  127. // Then determine fallback locale
  128. $currentLocale = Locale::getFallback($currentLocale);
  129. }
  130. // Multi-valued entry was merged
  131. if ($isMultiValued) {
  132. return $entry;
  133. }
  134. // Entry is still NULL, but no read error occurred
  135. if ($readSucceeded) {
  136. return $entry;
  137. }
  138. // Entry is still NULL, read error occurred. Throw an exception
  139. // containing the detailed path and locale
  140. $errorMessage = sprintf(
  141. 'Couldn\'t read the indices [%s] for the locale "%s" in "%s".',
  142. implode('][', $indices),
  143. $locale,
  144. $path
  145. );
  146. // Append fallback locales, if any
  147. if (count($testedLocales) > 1) {
  148. // Remove original locale
  149. array_shift($testedLocales);
  150. $errorMessage .= sprintf(
  151. ' The indices also couldn\'t be found for the fallback locale(s) "%s".',
  152. implode('", "', $testedLocales)
  153. );
  154. }
  155. throw new MissingResourceException($errorMessage, 0, $exception);
  156. }
  157. }