-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFileLoader.php
executable file
·216 lines (188 loc) · 5.29 KB
/
FileLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Illuminate\Translation;
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use RuntimeException;
class FileLoader implements Loader
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The default paths for the loader.
*
* @var array
*/
protected $paths;
/**
* All of the registered paths to JSON translation files.
*
* @var array
*/
protected $jsonPaths = [];
/**
* All of the namespace hints.
*
* @var array
*/
protected $hints = [];
/**
* Create a new file loader instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param array|string $path
* @return void
*/
public function __construct(Filesystem $files, array|string $path)
{
$this->files = $files;
$this->paths = is_string($path) ? [$path] : $path;
}
/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string|null $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
if ($group === '*' && $namespace === '*') {
return $this->loadJsonPaths($locale);
}
if (is_null($namespace) || $namespace === '*') {
return $this->loadPaths($this->paths, $locale, $group);
}
return $this->loadNamespaced($locale, $group, $namespace);
}
/**
* Load a namespaced translation group.
*
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
protected function loadNamespaced($locale, $group, $namespace)
{
if (isset($this->hints[$namespace])) {
$lines = $this->loadPaths([$this->hints[$namespace]], $locale, $group);
return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace);
}
return [];
}
/**
* Load a local namespaced translation group for overrides.
*
* @param array $lines
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
{
return (new Collection($this->paths))
->reduce(function ($output, $path) use ($lines, $locale, $group, $namespace) {
$file = "{$path}/vendor/{$namespace}/{$locale}/{$group}.php";
if ($this->files->exists($file)) {
$lines = array_replace_recursive($lines, $this->files->getRequire($file));
}
return $lines;
}, []);
}
/**
* Load a locale from a given path.
*
* @param array $paths
* @param string $locale
* @param string $group
* @return array
*/
protected function loadPaths(array $paths, $locale, $group)
{
return (new Collection($paths))
->reduce(function ($output, $path) use ($locale, $group) {
if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
$output = array_replace_recursive($output, $this->files->getRequire($full));
}
return $output;
}, []);
}
/**
* Load a locale from the given JSON file path.
*
* @param string $locale
* @return array
*
* @throws \RuntimeException
*/
protected function loadJsonPaths($locale)
{
return (new Collection(array_merge($this->jsonPaths, $this->paths)))
->reduce(function ($output, $path) use ($locale) {
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$decoded = json_decode($this->files->get($full), true);
if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
}
$output = array_merge($output, $decoded);
}
return $output;
}, []);
}
/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
* @return void
*/
public function addNamespace($namespace, $hint)
{
$this->hints[$namespace] = $hint;
}
/**
* Get an array of all the registered namespaces.
*
* @return array
*/
public function namespaces()
{
return $this->hints;
}
/**
* Add a new path to the loader.
*
* @param string $path
* @return void
*/
public function addPath($path)
{
$this->paths[] = $path;
}
/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->jsonPaths[] = $path;
}
/**
* Get an array of all the registered paths to JSON translation files.
*
* @return array
*/
public function jsonPaths()
{
return $this->jsonPaths;
}
}