Completed
Pull Request — master (#140)
by Petr
02:19
created

NeonFileLoader::recursiveLoadResources()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 2
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\Neon\Neon;
14
use Symfony\Component\Config\Resource\FileResource;
15
16
/**
17
 * Loads translations from Neon files.
18
 */
19
class NeonFileLoader extends \Symfony\Component\Translation\Loader\ArrayLoader implements \Symfony\Component\Translation\Loader\LoaderInterface
20
{
21
22
	/**
23
	 * @internal
24
	 */
25
	const INCLUDES_KEY = 'includes';
26
27
	/**
28
	 * {@inheritdoc}
29
	 */
30
	public function load($resource, $locale, $domain = 'messages')
31
	{
32
		if (!stream_is_local($resource)) {
33
			throw new \Symfony\Component\Translation\Exception\InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
34
		}
35
36
		if (!file_exists($resource)) {
37
			throw new \Symfony\Component\Translation\Exception\NotFoundResourceException(sprintf('File "%s" not found.', $resource));
38
		}
39
40
		try {
41
			$messages = Neon::decode(file_get_contents($resource));
42
43
		} catch (\Nette\Neon\Exception $e) {
0 ignored issues
show
Bug introduced by
The class Nette\Neon\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
44
			throw new \Symfony\Component\Translation\Exception\InvalidResourceException(sprintf('Error parsing Neon: %s', $e->getMessage()), 0, $e);
45
		}
46
47
		if (empty($messages)) {
48
			$messages = [];
49
		}
50
51
		$messages = $this->recursiveLoadResources($resource, $messages);
52
53
		if (!is_array($messages)) {
54
			throw new \Symfony\Component\Translation\Exception\InvalidResourceException(sprintf('The file "%s" must contain a Neon array.', $resource));
55
		}
56
57
		$catalogue = parent::load($messages, $locale, $domain);
58
		$catalogue->addResource(new FileResource($resource));
59
60
		return $catalogue;
61
	}
62
63
	/**
64
	 * @param string $resource
65
	 * @param array $messages
66
	 * @return array
67
	 */
68
	private function recursiveLoadResources($resource, array $messages)
69
	{
70
		if (isset($messages[self::INCLUDES_KEY])) {
71
			foreach ($messages[self::INCLUDES_KEY] as $include) {
72
				if (!preg_match('#([a-z]:)?[/\\\\]#Ai', $include)) {
73
					$include = dirname($resource) . '/' . $include;
74
				}
75
76
				$parent = array_filter(Neon::decode(file_get_contents($include)));
77
				$parent = $this->recursiveLoadResources($include, $parent);
78
				$messages = array_merge($parent, array_filter($messages));
79
			}
80
81
			unset($messages[self::INCLUDES_KEY]);
82
		}
83
84
		return $messages;
85
	}
86
87
}
88