Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Loader/FileLoader.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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;
17 use Symfony\Component\Translation\MessageCatalogue;
18
19 /**
20 * @author Abdellatif Ait boudad <[email protected]>
21 */
22 abstract class FileLoader extends ArrayLoader
23 {
24 public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
25 {
26 if (!stream_is_local($resource)) {
27 throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource));
28 }
29
30 if (!file_exists($resource)) {
31 throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource));
32 }
33
34 $messages = $this->loadResource($resource);
35
36 // empty resource
37 $messages ??= [];
38
39 // not an array
40 if (!\is_array($messages)) {
41 throw new InvalidResourceException(\sprintf('Unable to load file "%s".', $resource));
42 }
43
44 $catalogue = parent::load($messages, $locale, $domain);
45
46 if (class_exists(FileResource::class)) {
47 $catalogue->addResource(new FileResource($resource));
48 }
49
50 return $catalogue;
51 }
52
53 /**
54 * @throws InvalidResourceException if stream content has an invalid format
55 */
56 abstract protected function loadResource(string $resource): array;
57 }