PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Translation/MessageCatalogue.php

http://github.com/fabpot/symfony
PHP | 313 lines | 197 code | 44 blank | 72 comment | 36 complexity | 35dbefbd256d0c98eaeef5ef4da82006 MD5 | raw file
  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\Translation;
  11. use Symfony\Component\Config\Resource\ResourceInterface;
  12. use Symfony\Component\Translation\Exception\LogicException;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
  17. {
  18. private $messages = [];
  19. private $metadata = [];
  20. private $resources = [];
  21. private $locale;
  22. private $fallbackCatalogue;
  23. private $parent;
  24. /**
  25. * @param string $locale The locale
  26. * @param array $messages An array of messages classified by domain
  27. */
  28. public function __construct(string $locale, array $messages = [])
  29. {
  30. $this->locale = $locale;
  31. $this->messages = $messages;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getLocale()
  37. {
  38. return $this->locale;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getDomains()
  44. {
  45. $domains = [];
  46. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  47. foreach ($this->messages as $domain => $messages) {
  48. if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  49. $domain = substr($domain, 0, $i);
  50. }
  51. $domains[$domain] = $domain;
  52. }
  53. return array_values($domains);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function all(string $domain = null)
  59. {
  60. if (null !== $domain) {
  61. return ($this->messages[$domain.self::INTL_DOMAIN_SUFFIX] ?? []) + ($this->messages[$domain] ?? []);
  62. }
  63. $allMessages = [];
  64. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  65. foreach ($this->messages as $domain => $messages) {
  66. if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  67. $domain = substr($domain, 0, $i);
  68. $allMessages[$domain] = $messages + ($allMessages[$domain] ?? []);
  69. } else {
  70. $allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages;
  71. }
  72. }
  73. return $allMessages;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function set(string $id, string $translation, string $domain = 'messages')
  79. {
  80. $this->add([$id => $translation], $domain);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function has(string $id, string $domain = 'messages')
  86. {
  87. if (isset($this->messages[$domain][$id]) || isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id])) {
  88. return true;
  89. }
  90. if (null !== $this->fallbackCatalogue) {
  91. return $this->fallbackCatalogue->has($id, $domain);
  92. }
  93. return false;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function defines(string $id, string $domain = 'messages')
  99. {
  100. return isset($this->messages[$domain][$id]) || isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id]);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function get(string $id, string $domain = 'messages')
  106. {
  107. if (isset($this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id])) {
  108. return $this->messages[$domain.self::INTL_DOMAIN_SUFFIX][$id];
  109. }
  110. if (isset($this->messages[$domain][$id])) {
  111. return $this->messages[$domain][$id];
  112. }
  113. if (null !== $this->fallbackCatalogue) {
  114. return $this->fallbackCatalogue->get($id, $domain);
  115. }
  116. return $id;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function replace(array $messages, string $domain = 'messages')
  122. {
  123. unset($this->messages[$domain], $this->messages[$domain.self::INTL_DOMAIN_SUFFIX]);
  124. $this->add($messages, $domain);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function add(array $messages, string $domain = 'messages')
  130. {
  131. if (!isset($this->messages[$domain])) {
  132. $this->messages[$domain] = [];
  133. }
  134. $intlDomain = $domain;
  135. $suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
  136. if (\strlen($domain) > $suffixLength && false !== strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
  137. $intlDomain .= self::INTL_DOMAIN_SUFFIX;
  138. }
  139. foreach ($messages as $id => $message) {
  140. if (isset($this->messages[$intlDomain]) && \array_key_exists($id, $this->messages[$intlDomain])) {
  141. $this->messages[$intlDomain][$id] = $message;
  142. } else {
  143. $this->messages[$domain][$id] = $message;
  144. }
  145. }
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function addCatalogue(MessageCatalogueInterface $catalogue)
  151. {
  152. if ($catalogue->getLocale() !== $this->locale) {
  153. throw new LogicException(sprintf('Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s".', $catalogue->getLocale(), $this->locale));
  154. }
  155. foreach ($catalogue->all() as $domain => $messages) {
  156. if ($intlMessages = $catalogue->all($domain.self::INTL_DOMAIN_SUFFIX)) {
  157. $this->add($intlMessages, $domain.self::INTL_DOMAIN_SUFFIX);
  158. $messages = array_diff_key($messages, $intlMessages);
  159. }
  160. $this->add($messages, $domain);
  161. }
  162. foreach ($catalogue->getResources() as $resource) {
  163. $this->addResource($resource);
  164. }
  165. if ($catalogue instanceof MetadataAwareInterface) {
  166. $metadata = $catalogue->getMetadata('', '');
  167. $this->addMetadata($metadata);
  168. }
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
  174. {
  175. // detect circular references
  176. $c = $catalogue;
  177. while ($c = $c->getFallbackCatalogue()) {
  178. if ($c->getLocale() === $this->getLocale()) {
  179. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  180. }
  181. }
  182. $c = $this;
  183. do {
  184. if ($c->getLocale() === $catalogue->getLocale()) {
  185. throw new LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
  186. }
  187. foreach ($catalogue->getResources() as $resource) {
  188. $c->addResource($resource);
  189. }
  190. } while ($c = $c->parent);
  191. $catalogue->parent = $this;
  192. $this->fallbackCatalogue = $catalogue;
  193. foreach ($catalogue->getResources() as $resource) {
  194. $this->addResource($resource);
  195. }
  196. }
  197. /**
  198. * {@inheritdoc}
  199. */
  200. public function getFallbackCatalogue()
  201. {
  202. return $this->fallbackCatalogue;
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function getResources()
  208. {
  209. return array_values($this->resources);
  210. }
  211. /**
  212. * {@inheritdoc}
  213. */
  214. public function addResource(ResourceInterface $resource)
  215. {
  216. $this->resources[$resource->__toString()] = $resource;
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function getMetadata(string $key = '', string $domain = 'messages')
  222. {
  223. if ('' == $domain) {
  224. return $this->metadata;
  225. }
  226. if (isset($this->metadata[$domain])) {
  227. if ('' == $key) {
  228. return $this->metadata[$domain];
  229. }
  230. if (isset($this->metadata[$domain][$key])) {
  231. return $this->metadata[$domain][$key];
  232. }
  233. }
  234. return null;
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function setMetadata(string $key, $value, string $domain = 'messages')
  240. {
  241. $this->metadata[$domain][$key] = $value;
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function deleteMetadata(string $key = '', string $domain = 'messages')
  247. {
  248. if ('' == $domain) {
  249. $this->metadata = [];
  250. } elseif ('' == $key) {
  251. unset($this->metadata[$domain]);
  252. } else {
  253. unset($this->metadata[$domain][$key]);
  254. }
  255. }
  256. /**
  257. * Adds current values with the new values.
  258. *
  259. * @param array $values Values to add
  260. */
  261. private function addMetadata(array $values)
  262. {
  263. foreach ($values as $domain => $keys) {
  264. foreach ($keys as $key => $value) {
  265. $this->setMetadata($key, $value, $domain);
  266. }
  267. }
  268. }
  269. }