AbstractOperation.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Catalogue;
  11. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  12. use Symfony\Component\Translation\Exception\LogicException;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. use Symfony\Component\Translation\MessageCatalogueInterface;
  15. /**
  16. * Base catalogues binary operation class.
  17. *
  18. * A catalogue binary operation performs operation on
  19. * source (the left argument) and target (the right argument) catalogues.
  20. *
  21. * @author Jean-François Simon <contact@jfsimon.fr>
  22. */
  23. abstract class AbstractOperation implements OperationInterface
  24. {
  25. public const OBSOLETE_BATCH = 'obsolete';
  26. public const NEW_BATCH = 'new';
  27. public const ALL_BATCH = 'all';
  28. protected MessageCatalogueInterface $source;
  29. protected MessageCatalogueInterface $target;
  30. protected MessageCatalogue $result;
  31. /**
  32. * This array stores 'all', 'new' and 'obsolete' messages for all valid domains.
  33. *
  34. * The data structure of this array is as follows:
  35. *
  36. * [
  37. * 'domain 1' => [
  38. * 'all' => [...],
  39. * 'new' => [...],
  40. * 'obsolete' => [...]
  41. * ],
  42. * 'domain 2' => [
  43. * 'all' => [...],
  44. * 'new' => [...],
  45. * 'obsolete' => [...]
  46. * ],
  47. * ...
  48. * ]
  49. *
  50. * @var array The array that stores 'all', 'new' and 'obsolete' messages
  51. */
  52. protected array $messages;
  53. private array $domains;
  54. /**
  55. * @throws LogicException
  56. */
  57. public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target)
  58. {
  59. if ($source->getLocale() !== $target->getLocale()) {
  60. throw new LogicException('Operated catalogues must belong to the same locale.');
  61. }
  62. $this->source = $source;
  63. $this->target = $target;
  64. $this->result = new MessageCatalogue($source->getLocale());
  65. $this->messages = [];
  66. }
  67. public function getDomains(): array
  68. {
  69. if (!isset($this->domains)) {
  70. $domains = [];
  71. foreach ([$this->source, $this->target] as $catalogue) {
  72. foreach ($catalogue->getDomains() as $domain) {
  73. $domains[$domain] = $domain;
  74. if ($catalogue->all($domainIcu = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX)) {
  75. $domains[$domainIcu] = $domainIcu;
  76. }
  77. }
  78. }
  79. $this->domains = array_values($domains);
  80. }
  81. return $this->domains;
  82. }
  83. public function getMessages(string $domain): array
  84. {
  85. if (!\in_array($domain, $this->getDomains(), true)) {
  86. throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
  87. }
  88. if (!isset($this->messages[$domain][self::ALL_BATCH])) {
  89. $this->processDomain($domain);
  90. }
  91. return $this->messages[$domain][self::ALL_BATCH];
  92. }
  93. public function getNewMessages(string $domain): array
  94. {
  95. if (!\in_array($domain, $this->getDomains(), true)) {
  96. throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
  97. }
  98. if (!isset($this->messages[$domain][self::NEW_BATCH])) {
  99. $this->processDomain($domain);
  100. }
  101. return $this->messages[$domain][self::NEW_BATCH];
  102. }
  103. public function getObsoleteMessages(string $domain): array
  104. {
  105. if (!\in_array($domain, $this->getDomains(), true)) {
  106. throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
  107. }
  108. if (!isset($this->messages[$domain][self::OBSOLETE_BATCH])) {
  109. $this->processDomain($domain);
  110. }
  111. return $this->messages[$domain][self::OBSOLETE_BATCH];
  112. }
  113. public function getResult(): MessageCatalogueInterface
  114. {
  115. foreach ($this->getDomains() as $domain) {
  116. if (!isset($this->messages[$domain])) {
  117. $this->processDomain($domain);
  118. }
  119. }
  120. return $this->result;
  121. }
  122. /**
  123. * @param self::*_BATCH $batch
  124. */
  125. public function moveMessagesToIntlDomainsIfPossible(string $batch = self::ALL_BATCH): void
  126. {
  127. // If MessageFormatter class does not exists, intl domains are not supported.
  128. if (!class_exists(\MessageFormatter::class)) {
  129. return;
  130. }
  131. foreach ($this->getDomains() as $domain) {
  132. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  133. $messages = match ($batch) {
  134. self::OBSOLETE_BATCH => $this->getObsoleteMessages($domain),
  135. self::NEW_BATCH => $this->getNewMessages($domain),
  136. self::ALL_BATCH => $this->getMessages($domain),
  137. default => throw new \InvalidArgumentException(sprintf('$batch argument must be one of ["%s", "%s", "%s"].', self::ALL_BATCH, self::NEW_BATCH, self::OBSOLETE_BATCH)),
  138. };
  139. if (!$messages || (!$this->source->all($intlDomain) && $this->source->all($domain))) {
  140. continue;
  141. }
  142. $result = $this->getResult();
  143. $allIntlMessages = $result->all($intlDomain);
  144. $currentMessages = array_diff_key($messages, $result->all($domain));
  145. $result->replace($currentMessages, $domain);
  146. $result->replace($allIntlMessages + $messages, $intlDomain);
  147. }
  148. }
  149. /**
  150. * Performs operation on source and target catalogues for the given domain and
  151. * stores the results.
  152. *
  153. * @param string $domain The domain which the operation will be performed for
  154. */
  155. abstract protected function processDomain(string $domain): void;
  156. }