AbstractOperation.php 5.9 KB

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