|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of skillshare/formatphp |
| 5 | + * |
| 6 | + * skillshare/formatphp is open source software: you can distribute |
| 7 | + * it and/or modify it under the terms of the MIT License |
| 8 | + * (the "License"). You may not use this file except in |
| 9 | + * compliance with the License. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | + * implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + * |
| 17 | + * @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com> |
| 18 | + * @license https://opensource.org/licenses/MIT MIT License |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace FormatPHP; |
| 24 | + |
| 25 | +use FormatPHP\Exception\InvalidArgumentException; |
| 26 | +use FormatPHP\Exception\InvalidMessageShapeException; |
| 27 | +use FormatPHP\Exception\LocaleNotFoundException; |
| 28 | +use FormatPHP\Exception\UnableToProcessFileException; |
| 29 | +use FormatPHP\Intl\Locale; |
| 30 | +use FormatPHP\Intl\LocaleInterface; |
| 31 | +use FormatPHP\Reader\FormatInterface; |
| 32 | +use FormatPHP\Util\FileSystemHelper; |
| 33 | + |
| 34 | +use function array_filter; |
| 35 | +use function array_unique; |
| 36 | +use function array_values; |
| 37 | +use function implode; |
| 38 | +use function sprintf; |
| 39 | + |
| 40 | +use const DIRECTORY_SEPARATOR; |
| 41 | + |
| 42 | +/** |
| 43 | + * Loads messages for a given locale from the file system or cache |
| 44 | + */ |
| 45 | +final class MessageLoader |
| 46 | +{ |
| 47 | + private Config $config; |
| 48 | + private FileSystemHelper $fileSystemHelper; |
| 49 | + private FormatInterface $formatReader; |
| 50 | + private string $messagesDirectory; |
| 51 | + |
| 52 | + /** |
| 53 | + * @throws InvalidArgumentException |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + string $messagesDirectory, |
| 57 | + Config $config, |
| 58 | + FormatInterface $formatReader, |
| 59 | + ?FileSystemHelper $fileSystemHelper = null |
| 60 | + ) { |
| 61 | + $this->config = $config; |
| 62 | + $this->formatReader = $formatReader; |
| 63 | + $this->fileSystemHelper = $fileSystemHelper ?? new FileSystemHelper(); |
| 64 | + $this->messagesDirectory = $this->fileSystemHelper->getRealPath($messagesDirectory); |
| 65 | + |
| 66 | + if (!$this->fileSystemHelper->isDirectory($this->messagesDirectory)) { |
| 67 | + throw new InvalidArgumentException(sprintf( |
| 68 | + 'Messages directory "%s" is not a valid directory', |
| 69 | + $messagesDirectory, |
| 70 | + )); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns a MessageCollection according to the configuration provided to |
| 76 | + * this MessageLoader |
| 77 | + * |
| 78 | + * @throws InvalidArgumentException |
| 79 | + * @throws InvalidMessageShapeException |
| 80 | + * @throws LocaleNotFoundException |
| 81 | + */ |
| 82 | + public function loadMessages(): MessageCollection |
| 83 | + { |
| 84 | + [$messagesData, $resolvedLocale] = $this->getLocaleMessages(); |
| 85 | + |
| 86 | + return ($this->formatReader)($this->config, $messagesData, $resolvedLocale); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return array{0: array<array-key, mixed>, 1: LocaleInterface} |
| 91 | + * |
| 92 | + * @throws InvalidArgumentException |
| 93 | + * @throws LocaleNotFoundException |
| 94 | + */ |
| 95 | + private function getLocaleMessages(): array |
| 96 | + { |
| 97 | + $messagesContents = false; |
| 98 | + $localeResolved = null; |
| 99 | + |
| 100 | + foreach ($this->getFallbackLocales() as $locale) { |
| 101 | + try { |
| 102 | + $messagesFile = $this->messagesDirectory . DIRECTORY_SEPARATOR . $locale . '.json'; |
| 103 | + $messagesContents = $this->fileSystemHelper->getJsonContents($messagesFile); |
| 104 | + $localeResolved = new Locale($locale); |
| 105 | + |
| 106 | + break; |
| 107 | + } catch (UnableToProcessFileException $exception) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if ($messagesContents === false || $localeResolved === null) { |
| 113 | + throw new LocaleNotFoundException(sprintf( |
| 114 | + 'Unable to find a suitable locale for "%s"; please set a default locale', |
| 115 | + $this->config->getLocale()->toString(), |
| 116 | + )); |
| 117 | + } |
| 118 | + |
| 119 | + return [$messagesContents, $localeResolved]; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return string[] |
| 124 | + */ |
| 125 | + private function getFallbackLocales(): array |
| 126 | + { |
| 127 | + $locale = $this->config->getLocale(); |
| 128 | + $defaultLocale = $this->config->getDefaultLocale(); |
| 129 | + |
| 130 | + $fallbacks = [ |
| 131 | + $locale->toString(), |
| 132 | + $locale->baseName(), |
| 133 | + implode('-', array_filter([$locale->language(), $locale->region()])), |
| 134 | + $locale->language(), |
| 135 | + $defaultLocale ? $defaultLocale->toString() : null, |
| 136 | + ]; |
| 137 | + |
| 138 | + /** @var string[] */ |
| 139 | + return array_values(array_unique(array_filter($fallbacks))); |
| 140 | + } |
| 141 | +} |
0 commit comments