AbstractOperation.php 6.1 KB

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