AbstractOperation.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /**
  71. * {@inheritdoc}
  72. */
  73. public function getDomains()
  74. {
  75. if (null === $this->domains) {
  76. $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
  77. }
  78. return $this->domains;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getMessages(string $domain)
  84. {
  85. if (!\in_array($domain, $this->getDomains())) {
  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. /**
  94. * {@inheritdoc}
  95. */
  96. public function getNewMessages(string $domain)
  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. /**
  107. * {@inheritdoc}
  108. */
  109. public function getObsoleteMessages(string $domain)
  110. {
  111. if (!\in_array($domain, $this->getDomains())) {
  112. throw new InvalidArgumentException(sprintf('Invalid domain: "%s".', $domain));
  113. }
  114. if (!isset($this->messages[$domain][self::OBSOLETE_BATCH])) {
  115. $this->processDomain($domain);
  116. }
  117. return $this->messages[$domain][self::OBSOLETE_BATCH];
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getResult()
  123. {
  124. foreach ($this->getDomains() as $domain) {
  125. if (!isset($this->messages[$domain])) {
  126. $this->processDomain($domain);
  127. }
  128. }
  129. return $this->result;
  130. }
  131. /**
  132. * @param self::*_BATCH $batch
  133. */
  134. public function moveMessagesToIntlDomainsIfPossible(string $batch = self::ALL_BATCH): void
  135. {
  136. // If MessageFormatter class does not exists, intl domains are not supported.
  137. if (!class_exists(\MessageFormatter::class)) {
  138. return;
  139. }
  140. foreach ($this->getDomains() as $domain) {
  141. $intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
  142. switch ($batch) {
  143. case self::OBSOLETE_BATCH: $messages = $this->getObsoleteMessages($domain); break;
  144. case self::NEW_BATCH: $messages = $this->getNewMessages($domain); break;
  145. case self::ALL_BATCH: $messages = $this->getMessages($domain); break;
  146. default: throw new \InvalidArgumentException(sprintf('$batch argument must be one of ["%s", "%s", "%s"].', self::ALL_BATCH, self::NEW_BATCH, self::OBSOLETE_BATCH));
  147. }
  148. if (!$messages || (!$this->source->all($intlDomain) && $this->source->all($domain))) {
  149. continue;
  150. }
  151. $result = $this->getResult();
  152. $allIntlMessages = $result->all($intlDomain);
  153. $currentMessages = array_diff_key($messages, $result->all($domain));
  154. $result->replace($currentMessages, $domain);
  155. $result->replace($allIntlMessages + $messages, $intlDomain);
  156. }
  157. }
  158. /**
  159. * Performs operation on source and target catalogues for the given domain and
  160. * stores the results.
  161. *
  162. * @param string $domain The domain which the operation will be performed for
  163. */
  164. abstract protected function processDomain(string $domain);
  165. }