blob: 4725ea6d62a7d964396fd13d82071db671c2f7ca [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;
17
18/**
19 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
20 */
21abstract class FileLoader extends ArrayLoader
22{
23 /**
24 * {@inheritdoc}
25 */
26 public function load($resource, string $locale, string $domain = 'messages')
27 {
28 if (!stream_is_local($resource)) {
29 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
30 }
31
32 if (!file_exists($resource)) {
33 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
34 }
35
36 $messages = $this->loadResource($resource);
37
38 // empty resource
39 if (null === $messages) {
40 $messages = [];
41 }
42
43 // not an array
44 if (!\is_array($messages)) {
45 throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
46 }
47
48 $catalogue = parent::load($messages, $locale, $domain);
49
50 if (class_exists(FileResource::class)) {
51 $catalogue->addResource(new FileResource($resource));
52 }
53
54 return $catalogue;
55 }
56
57 /**
58 * @return array
59 *
60 * @throws InvalidResourceException if stream content has an invalid format
61 */
62 abstract protected function loadResource(string $resource);
63}