| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Catalogue; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Translation\Exception\LogicException; |
| 16 |
use Symfony\Component\Translation\MessageCatalogue; |
| 17 |
use Symfony\Component\Translation\MessageCatalogueInterface; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
> |
| 26 |
|
| 27 |
abstract class AbstractOperation implements OperationInterface |
| 28 |
{ |
| 29 |
public const OBSOLETE_BATCH = 'obsolete'; |
| 30 |
public const NEW_BATCH = 'new'; |
| 31 |
public const ALL_BATCH = 'all'; |
| 32 |
|
| 33 |
protected MessageCatalogue $result; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected array $messages; |
| 57 |
|
| 58 |
private array $domains; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( |
| 64 |
protected MessageCatalogueInterface $source, |
| 65 |
protected MessageCatalogueInterface $target, |
| 66 |
) { |
| 67 |
if ($source->getLocale() !== $target->getLocale()) { |
| 68 |
throw new LogicException('Operated catalogues must belong to the same locale.'); |
| 69 |
} |
| 70 |
|
| 71 |
$this->result = new MessageCatalogue($source->getLocale()); |
| 72 |
$this->messages = []; |
| 73 |
} |
| 74 |
|
| 75 |
public function getDomains(): array |
| 76 |
{ |
| 77 |
if (!isset($this->domains)) { |
| 78 |
$domains = []; |
| 79 |
foreach ([$this->source, $this->target] as $catalogue) { |
| 80 |
foreach ($catalogue->getDomains() as $domain) { |
| 81 |
$domains[$domain] = $domain; |
| 82 |
|
| 83 |
if ($catalogue->all($domainIcu = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX)) { |
| 84 |
$domains[$domainIcu] = $domainIcu; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$this->domains = array_values($domains); |
| 90 |
} |
| 91 |
|
| 92 |
return $this->domains; |
| 93 |
} |
| 94 |
|
| 95 |
public function getMessages(string $domain): array |
| 96 |
{ |
| 97 |
if (!\in_array($domain, $this->getDomains(), true)) { |
| 98 |
throw new InvalidArgumentException(\sprintf('Invalid domain: "%s".', $domain)); |
| 99 |
} |
| 100 |
|
| 101 |
if (!isset($this->messages[$domain][self::ALL_BATCH])) { |
| 102 |
$this->processDomain($domain); |
| 103 |
} |
| 104 |
|
| 105 |
return $this->messages[$domain][self::ALL_BATCH]; |
| 106 |
} |
| 107 |
|
| 108 |
public function getNewMessages(string $domain): array |
| 109 |
{ |
| 110 |
if (!\in_array($domain, $this->getDomains(), true)) { |
| 111 |
throw new InvalidArgumentException(\sprintf('Invalid domain: "%s".', $domain)); |
| 112 |
} |
| 113 |
|
| 114 |
if (!isset($this->messages[$domain][self::NEW_BATCH])) { |
| 115 |
$this->processDomain($domain); |
| 116 |
} |
| 117 |
|
| 118 |
return $this->messages[$domain][self::NEW_BATCH]; |
| 119 |
} |
| 120 |
|
| 121 |
public function getObsoleteMessages(string $domain): array |
| 122 |
{ |
| 123 |
if (!\in_array($domain, $this->getDomains(), true)) { |
| 124 |
throw new InvalidArgumentException(\sprintf('Invalid domain: "%s".', $domain)); |
| 125 |
} |
| 126 |
|
| 127 |
if (!isset($this->messages[$domain][self::OBSOLETE_BATCH])) { |
| 128 |
$this->processDomain($domain); |
| 129 |
} |
| 130 |
|
| 131 |
return $this->messages[$domain][self::OBSOLETE_BATCH]; |
| 132 |
} |
| 133 |
|
| 134 |
public function getResult(): MessageCatalogueInterface |
| 135 |
{ |
| 136 |
foreach ($this->getDomains() as $domain) { |
| 137 |
if (!isset($this->messages[$domain])) { |
| 138 |
$this->processDomain($domain); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $this->result; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function moveMessagesToIntlDomainsIfPossible(string $batch = self::ALL_BATCH): void |
| 149 |
{ |
| 150 |
|
| 151 |
if (!class_exists(\MessageFormatter::class)) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
foreach ($this->getDomains() as $domain) { |
| 156 |
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX; |
| 157 |
$messages = match ($batch) { |
| 158 |
self::OBSOLETE_BATCH => $this->getObsoleteMessages($domain), |
| 159 |
self::NEW_BATCH => $this->getNewMessages($domain), |
| 160 |
self::ALL_BATCH => $this->getMessages($domain), |
| 161 |
default => throw new \InvalidArgumentException(\sprintf('$batch argument must be one of ["%s", "%s", "%s"].', self::ALL_BATCH, self::NEW_BATCH, self::OBSOLETE_BATCH)), |
| 162 |
}; |
| 163 |
|
| 164 |
if (!$messages || (!$this->source->all($intlDomain) && $this->source->all($domain))) { |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
$result = $this->getResult(); |
| 169 |
$allIntlMessages = $result->all($intlDomain); |
| 170 |
$currentMessages = array_diff_key($messages, $result->all($domain)); |
| 171 |
$result->replace($currentMessages, $domain); |
| 172 |
$result->replace($allIntlMessages + $messages, $intlDomain); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
abstract protected function processDomain(string $domain): void; |
| 183 |
} |