AbstractOperation.php 5.8 KB

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