Completed
Push — master ( 94ba74...5b4930 )
by Filip
04:55
created

NeonFileLoader::load()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
rs 8.439
c 1
b 0
f 0
cc 6
eloc 16
nc 7
nop 3
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Translation\Loader;
12
13
use Nette;
14
use Nette\Neon;
15
use Symfony\Component\Translation\Exception\InvalidResourceException;
16
use Symfony\Component\Translation\Exception\NotFoundResourceException;
17
use Symfony\Component\Config\Resource\FileResource;
18
use Symfony\Component\Translation\Loader\ArrayLoader;
19
use Symfony\Component\Translation\Loader\LoaderInterface;
20
21
22
23
/**
24
 * Loads translations from Neon files.
25
 *
26
 * @author Filip Procházka <[email protected]>
27
 */
28
class NeonFileLoader extends ArrayLoader implements LoaderInterface
29
{
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	public function load($resource, $locale, $domain = 'messages')
35
	{
36
		if (!stream_is_local($resource)) {
37
			throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
38
		}
39
40
		if (!file_exists($resource)) {
41
			throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
42
		}
43
44
		try {
45
			$messages = Neon\Neon::decode(file_get_contents($resource));
46
47
		} catch (Nette\Neon\Exception $e) {
48
			throw new InvalidResourceException(sprintf("Error parsing Neon: %s", $e->getMessage()), 0, $e);
49
		}
50
51
		if (empty($messages)) {
52
			$messages = [];
53
		}
54
55
		if (!is_array($messages)) {
56
			throw new InvalidResourceException(sprintf('The file "%s" must contain a Neon array.', $resource));
57
		}
58
59
		$catalogue = parent::load($messages, $locale, $domain);
60
		$catalogue->addResource(new FileResource($resource));
61
62
		return $catalogue;
63
	}
64
65
}
66