Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
111 / 111 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Sync | |
100.00% |
111 / 111 |
|
100.00% |
10 / 10 |
37 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| reconcile | |
100.00% |
40 / 40 |
|
100.00% |
1 / 1 |
6 | |||
| vanished | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| reconcileSection | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| parkSection | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| parkContexts | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| contextSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| write | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
8 | |||
| filesystemError | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Verba\Tool; |
| 6 | |
| 7 | use RuntimeException; |
| 8 | |
| 9 | /** |
| 10 | * Reconciles a domain's catalog files against the current source: fresh ids are |
| 11 | * added as untranslated, existing translations are kept, a reappearing id is |
| 12 | * restored from its ordinary or contextual obsolete section, and a vanished id |
| 13 | * is parked there unless pruning. Rewrites are deterministic. |
| 14 | * |
| 15 | * @api |
| 16 | */ |
| 17 | final class Sync |
| 18 | { |
| 19 | public function __construct( |
| 20 | private readonly Domain $domain, |
| 21 | private readonly bool $prune = false, |
| 22 | ) {} |
| 23 | |
| 24 | public function run(): SyncReport |
| 25 | { |
| 26 | $extraction = new Extractor($this->domain)->extract(); |
| 27 | $fresh = $extraction['messages']; |
| 28 | $freshContexts = $extraction['contexts']; |
| 29 | |
| 30 | $locales = []; |
| 31 | |
| 32 | foreach ($this->domain->locales as $locale) { |
| 33 | $locales[$locale] = $this->reconcile($locale, $fresh, $freshContexts); |
| 34 | } |
| 35 | |
| 36 | return new SyncReport($this->domain->name, $locales, $extraction['warnings']); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param array<array-key, Message> $fresh |
| 41 | * @param array<array-key, array<array-key, Message>> $freshContexts |
| 42 | * @return array{added: int, obsolete: int, total: int, changed: bool, vanished: list<string>} |
| 43 | */ |
| 44 | private function reconcile(string $locale, array $fresh, array $freshContexts): array |
| 45 | { |
| 46 | $file = $this->domain->file($locale); |
| 47 | $catalog = CatalogFile::load($file); |
| 48 | $section = $this->reconcileSection($fresh, $catalog->messages, $catalog->obsolete); |
| 49 | $messages = $section['messages']; |
| 50 | $added = $section['added']; |
| 51 | $contexts = []; |
| 52 | |
| 53 | foreach ($freshContexts as $context => $contextFresh) { |
| 54 | $section = $this->reconcileSection( |
| 55 | $contextFresh, |
| 56 | $catalog->contexts[$context] ?? [], |
| 57 | $catalog->obsoleteContexts[$context] ?? [], |
| 58 | ); |
| 59 | $contexts[$context] = $section['messages']; |
| 60 | $added += $section['added']; |
| 61 | } |
| 62 | |
| 63 | $vanished = self::vanished($catalog->messages, $fresh); |
| 64 | |
| 65 | foreach ($catalog->contexts as $context => $contextMessages) { |
| 66 | $vanished = [...$vanished, ...self::vanished($contextMessages, $freshContexts[$context] ?? [], $context)]; |
| 67 | } |
| 68 | |
| 69 | if ($this->prune) { |
| 70 | $obsolete = []; |
| 71 | $obsoleteContexts = []; |
| 72 | } else { |
| 73 | $obsolete = self::parkSection($catalog->messages, $catalog->obsolete, $fresh); |
| 74 | $obsoleteContexts = $this->parkContexts($catalog, $freshContexts); |
| 75 | } |
| 76 | |
| 77 | $next = new CatalogFile( |
| 78 | $messages, |
| 79 | $obsolete, |
| 80 | $catalog->plural, |
| 81 | $contexts, |
| 82 | $obsoleteContexts, |
| 83 | ); |
| 84 | $rendered = $next->render(); |
| 85 | $changed = !is_file($file) || file_get_contents($file) !== $rendered; |
| 86 | |
| 87 | if ($changed) { |
| 88 | $this->write($file, $rendered); |
| 89 | } |
| 90 | |
| 91 | return [ |
| 92 | 'added' => $added, |
| 93 | 'obsolete' => count($obsolete) + self::contextSize($obsoleteContexts), |
| 94 | 'total' => count($messages) + self::contextSize($contexts), |
| 95 | 'changed' => $changed, |
| 96 | 'vanished' => $vanished, |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * The ids of a live section that the source no longer holds, which this |
| 102 | * run parks or, when pruning, drops. A contextual id reads `[context] id`. |
| 103 | * |
| 104 | * @param array<array-key, string|list<string>|null> $messages |
| 105 | * @param array<array-key, Message> $fresh |
| 106 | * @return list<string> |
| 107 | */ |
| 108 | private static function vanished(array $messages, array $fresh, int|string|null $context = null): array |
| 109 | { |
| 110 | $ids = []; |
| 111 | |
| 112 | foreach (array_keys($messages) as $id) { |
| 113 | if (array_key_exists($id, $fresh)) { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | $ids[] = $context === null ? (string) $id : "[{$context}] {$id}"; |
| 118 | } |
| 119 | |
| 120 | return $ids; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param array<array-key, Message> $fresh |
| 125 | * @param array<array-key, string|list<string>|null> $messages |
| 126 | * @param array<array-key, string|list<string>|null> $obsolete |
| 127 | * @return array{messages: array<array-key, string|list<string>|null>, added: int} |
| 128 | */ |
| 129 | private function reconcileSection(array $fresh, array $messages, array $obsolete): array |
| 130 | { |
| 131 | $next = []; |
| 132 | $added = 0; |
| 133 | |
| 134 | foreach (array_keys($fresh) as $id) { |
| 135 | if (array_key_exists($id, $messages)) { |
| 136 | $next[$id] = $messages[$id]; |
| 137 | } elseif (array_key_exists($id, $obsolete)) { |
| 138 | $next[$id] = $obsolete[$id]; |
| 139 | } else { |
| 140 | $next[$id] = null; |
| 141 | $added++; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return ['messages' => $next, 'added' => $added]; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param array<array-key, string|list<string>|null> $messages |
| 150 | * @param array<array-key, string|list<string>|null> $parked |
| 151 | * @param array<array-key, Message> $fresh |
| 152 | * @return array<array-key, string|list<string>|null> |
| 153 | */ |
| 154 | private static function parkSection(array $messages, array $parked, array $fresh): array |
| 155 | { |
| 156 | $obsolete = []; |
| 157 | |
| 158 | foreach ($messages as $id => $value) { |
| 159 | if (array_key_exists($id, $fresh)) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | $obsolete[$id] = $value; |
| 164 | } |
| 165 | |
| 166 | foreach ($parked as $id => $value) { |
| 167 | if (array_key_exists($id, $fresh) || array_key_exists($id, $obsolete)) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | $obsolete[$id] = $value; |
| 172 | } |
| 173 | |
| 174 | return $obsolete; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param array<array-key, array<array-key, Message>> $fresh |
| 179 | * @return array<array-key, array<array-key, string|list<string>|null>> |
| 180 | */ |
| 181 | private function parkContexts(CatalogFile $catalog, array $fresh): array |
| 182 | { |
| 183 | $contexts = []; |
| 184 | $names = array_unique([ |
| 185 | ...array_keys($catalog->contexts), |
| 186 | ...array_keys($catalog->obsoleteContexts), |
| 187 | ]); |
| 188 | |
| 189 | foreach ($names as $context) { |
| 190 | $messages = self::parkSection( |
| 191 | $catalog->contexts[$context] ?? [], |
| 192 | $catalog->obsoleteContexts[$context] ?? [], |
| 193 | $fresh[$context] ?? [], |
| 194 | ); |
| 195 | |
| 196 | if ($messages !== []) { |
| 197 | $contexts[$context] = $messages; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return $contexts; |
| 202 | } |
| 203 | |
| 204 | /** @param array<array-key, array<array-key, string|list<string>|null>> $contexts */ |
| 205 | private static function contextSize(array $contexts): int |
| 206 | { |
| 207 | return array_sum(array_map(count(...), $contexts)); |
| 208 | } |
| 209 | |
| 210 | private function write(string $file, string $contents): void |
| 211 | { |
| 212 | $dir = dirname($file); |
| 213 | $temp = null; |
| 214 | $error = null; |
| 215 | |
| 216 | // Filesystem functions warn before returning failure; this method checks every result below. |
| 217 | set_error_handler(static function (int $_severity, string $message) use (&$error): bool { |
| 218 | $error = $message; |
| 219 | |
| 220 | return true; |
| 221 | }); |
| 222 | |
| 223 | try { |
| 224 | if (!is_dir($dir)) { |
| 225 | $error = null; |
| 226 | |
| 227 | if (!mkdir($dir, 0o755, true) && !is_dir($dir)) { |
| 228 | throw self::filesystemError("Cannot create catalog directory '{$dir}'", $error); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | $temp = sprintf('%s.%s.tmp', $file, bin2hex(random_bytes(6))); |
| 233 | $error = null; |
| 234 | |
| 235 | if (file_put_contents($temp, $contents) !== strlen($contents)) { |
| 236 | throw self::filesystemError("Cannot write temporary catalog for '{$file}'", $error); |
| 237 | } |
| 238 | |
| 239 | $error = null; |
| 240 | |
| 241 | if (!rename($temp, $file)) { |
| 242 | throw self::filesystemError("Cannot replace catalog '{$file}'", $error); |
| 243 | } |
| 244 | } finally { |
| 245 | if ($temp !== null && is_file($temp)) { |
| 246 | unlink($temp); |
| 247 | } |
| 248 | |
| 249 | restore_error_handler(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private static function filesystemError(string $message, ?string $cause): RuntimeException |
| 254 | { |
| 255 | return new RuntimeException($cause === null ? $message : $message . ': ' . $cause); |
| 256 | } |
| 257 | } |