Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\Translation\Loader; |
| 13 | |
| 14 | use Symfony\Component\Config\Resource\FileResource; |
| 15 | use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 16 | use Symfony\Component\Translation\Exception\NotFoundResourceException; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 17 | use Symfony\Component\Translation\MessageCatalogue; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> |
| 21 | */ |
| 22 | abstract class FileLoader extends ArrayLoader |
| 23 | { |
| 24 | /** |
| 25 | * {@inheritdoc} |
| 26 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 27 | public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 28 | { |
| 29 | if (!stream_is_local($resource)) { |
| 30 | throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); |
| 31 | } |
| 32 | |
| 33 | if (!file_exists($resource)) { |
| 34 | throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); |
| 35 | } |
| 36 | |
| 37 | $messages = $this->loadResource($resource); |
| 38 | |
| 39 | // empty resource |
| 40 | if (null === $messages) { |
| 41 | $messages = []; |
| 42 | } |
| 43 | |
| 44 | // not an array |
| 45 | if (!\is_array($messages)) { |
| 46 | throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource)); |
| 47 | } |
| 48 | |
| 49 | $catalogue = parent::load($messages, $locale, $domain); |
| 50 | |
| 51 | if (class_exists(FileResource::class)) { |
| 52 | $catalogue->addResource(new FileResource($resource)); |
| 53 | } |
| 54 | |
| 55 | return $catalogue; |
| 56 | } |
| 57 | |
| 58 | /** |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 59 | * @throws InvalidResourceException if stream content has an invalid format |
| 60 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 61 | abstract protected function loadResource(string $resource): array; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 62 | } |