1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace L10nBundle\Utils\Loader\Yaml; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Exception\FileLoaderLoadException; |
6
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
7
|
|
|
use Symfony\Component\Config\Loader\FileLoader; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Loads the L10n configuration in yaml format. |
12
|
|
|
* |
13
|
|
|
* @package L10nBundle\Loader\Yaml |
14
|
|
|
*/ |
15
|
|
|
class YamlL10nLoader extends FileLoader |
16
|
|
|
{ |
17
|
|
|
const ROOT = 'l10n'; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $config; |
21
|
|
|
|
22
|
5 |
|
public function __construct(FileLocatorInterface $locator) |
23
|
|
|
{ |
24
|
5 |
|
$this->config = array(); |
25
|
5 |
|
parent::__construct($locator); |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Loads a Yaml file. |
30
|
|
|
* |
31
|
|
|
* @param mixed $file The resource |
32
|
|
|
* @param string $type The resource type |
33
|
|
|
* |
34
|
|
|
* @throws FileLoaderLoadException |
35
|
|
|
*/ |
36
|
3 |
|
public function load($file, $type = null) |
37
|
|
|
{ |
38
|
3 |
|
$path = $this->locator->locate($file); |
39
|
3 |
|
if (is_array($path)) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
$content = $this->loadFile($path); |
44
|
|
|
|
45
|
3 |
|
if (!is_array($content)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
$this->parseImports($content, $file); |
50
|
|
|
|
51
|
3 |
|
$this->loadConfig($content); |
52
|
3 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
1 |
|
public function supports($resource, $type = null) |
58
|
|
|
{ |
59
|
1 |
|
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
4 |
|
public function getConfig() |
66
|
|
|
{ |
67
|
4 |
|
return $this->config; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Loads a YAML file. |
72
|
|
|
* |
73
|
|
|
* @param string $file |
74
|
|
|
* |
75
|
|
|
* @throws FileLoaderLoadException |
76
|
|
|
* |
77
|
|
|
* @return array The file content |
78
|
|
|
*/ |
79
|
|
|
public function loadFile($file) |
80
|
|
|
{ |
81
|
|
|
if (!$this->supports($file)) { |
82
|
|
|
throw new FileLoaderLoadException($file); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return Yaml::parse($file); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Parses all imports |
90
|
|
|
* |
91
|
|
|
* @param array $content |
92
|
|
|
* @param string $file |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
3 |
|
private function parseImports($content, $file) |
97
|
|
|
{ |
98
|
3 |
|
if (!isset($content['imports'])) { |
99
|
3 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($content['imports'] as $import) { |
103
|
|
|
$this->setCurrentDir(dirname($file)); |
104
|
|
|
$this->import($import['resource'], null, false, $file); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Recursively merges the config of content into current config. |
110
|
|
|
* |
111
|
|
|
* @param $content |
112
|
|
|
*/ |
113
|
3 |
|
private function loadConfig($content) |
114
|
|
|
{ |
115
|
3 |
|
if (isset($content[self::ROOT])) { |
116
|
2 |
|
$this->config = array_replace_recursive($this->config, $content[self::ROOT]); |
117
|
2 |
|
} |
118
|
3 |
|
} |
119
|
|
|
} |
120
|
|
|
|