blob: e170d761708bc5366799a48042bfb99a4a386fad [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Loader;
13
14use Symfony\Component\Config\Resource\FileResource;
15use Symfony\Component\Translation\Exception\InvalidResourceException;
16use Symfony\Component\Translation\Exception\NotFoundResourceException;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010017use Symfony\Component\Translation\MessageCatalogue;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020018
19/**
20 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
21 */
22abstract class FileLoader extends ArrayLoader
23{
24 /**
25 * {@inheritdoc}
26 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010027 public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020028 {
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 Benkard7b2a3a12021-08-16 10:57:25 +020059 * @throws InvalidResourceException if stream content has an invalid format
60 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010061 abstract protected function loadResource(string $resource): array;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020062}