]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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\Translation\Exception\InvalidResourceException; | |
15 | use Symfony\Component\Translation\Exception\NotFoundResourceException; | |
16 | use Symfony\Component\Config\Resource\FileResource; | |
17 | use Symfony\Component\Yaml\Parser as YamlParser; | |
18 | use Symfony\Component\Yaml\Exception\ParseException; | |
19 | ||
20 | /** | |
21 | * YamlFileLoader loads translations from Yaml files. | |
22 | * | |
23 | * @author Fabien Potencier <fabien@symfony.com> | |
24 | * | |
25 | * @api | |
26 | */ | |
27 | class YamlFileLoader extends ArrayLoader implements LoaderInterface | |
28 | { | |
29 | private $yamlParser; | |
30 | ||
31 | /** | |
32 | * {@inheritdoc} | |
33 | * | |
34 | * @api | |
35 | */ | |
36 | public function load($resource, $locale, $domain = 'messages') | |
37 | { | |
38 | if (!stream_is_local($resource)) { | |
39 | throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); | |
40 | } | |
41 | ||
42 | if (!file_exists($resource)) { | |
43 | throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); | |
44 | } | |
45 | ||
46 | if (null === $this->yamlParser) { | |
47 | $this->yamlParser = new YamlParser(); | |
48 | } | |
49 | ||
50 | try { | |
51 | $messages = $this->yamlParser->parse(file_get_contents($resource)); | |
52 | } catch (ParseException $e) { | |
53 | throw new InvalidResourceException('Error parsing YAML.', 0, $e); | |
54 | } | |
55 | ||
56 | // empty file | |
57 | if (null === $messages) { | |
58 | $messages = array(); | |
59 | } | |
60 | ||
61 | // not an array | |
62 | if (!is_array($messages)) { | |
63 | throw new InvalidResourceException(sprintf('The file "%s" must contain a YAML array.', $resource)); | |
64 | } | |
65 | ||
66 | $catalogue = parent::load($messages, $locale, $domain); | |
67 | $catalogue->addResource(new FileResource($resource)); | |
68 | ||
69 | return $catalogue; | |
70 | } | |
71 | } |