1
|
|
|
<?php namespace App\Lang; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Addon; |
4
|
|
|
use Anomaly\Streams\Platform\Addon\AddonCollection; |
5
|
|
|
use Anomaly\Streams\Platform\Application\Application; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Translation\FileLoader; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Loader |
11
|
|
|
* |
12
|
|
|
* @link http://pyrocms.com/ |
13
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Loader extends FileLoader |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The runtime cache. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected static $disabled = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The streams path. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $streams; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The addon collection instance. |
35
|
|
|
* |
36
|
|
|
* @var AddonCollection |
37
|
|
|
*/ |
38
|
|
|
protected $addons; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The application instance. |
42
|
|
|
* |
43
|
|
|
* @var Application |
44
|
|
|
*/ |
45
|
|
|
protected $application; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a new Loader instance. |
49
|
|
|
* |
50
|
|
|
* @param Filesystem $files |
51
|
|
|
* @param string $path |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Filesystem $files, $path) |
54
|
|
|
{ |
55
|
|
|
$this->streams = base_path('vendor/visiosoft/streams-platform/resources/lang'); |
56
|
|
|
|
57
|
|
|
$this->application = app(Application::class); |
58
|
|
|
$this->addons = app(AddonCollection::class); |
59
|
|
|
|
60
|
|
|
parent::__construct($files, $path); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Load a locale from a given path. |
65
|
|
|
* |
66
|
|
|
* Keep streams overrides in place |
67
|
|
|
* that are NOT namespaced cause |
68
|
|
|
* we're overriding Laravel's |
69
|
|
|
* base language files too. |
70
|
|
|
* |
71
|
|
|
* @param string $path |
72
|
|
|
* @param string $locale |
73
|
|
|
* @param string $group |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function loadPath($path, $locale, $group) |
77
|
|
|
{ |
78
|
|
|
$lines = parent::loadPath($path, $locale, $group); |
79
|
|
|
|
80
|
|
|
if ($path == $this->streams && $lines) { |
|
|
|
|
81
|
|
|
$lines = $this->loadAddonOverrides($lines, $locale, $group); |
82
|
|
|
$lines = $this->loadSystemOverrides($lines, $locale, $group); |
83
|
|
|
$lines = $this->loadApplicationOverrides($lines, $locale, $group); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $lines; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Load namespaced overrides from |
91
|
|
|
* system AND application paths. |
92
|
|
|
* |
93
|
|
|
* @param array $lines |
94
|
|
|
* @param string $locale |
95
|
|
|
* @param string $group |
96
|
|
|
* @param string $namespace |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace) |
100
|
|
|
{ |
101
|
|
|
/** |
102
|
|
|
* @deprecated since 1.6; Use manual loading or publishing. |
103
|
|
|
*/ |
104
|
|
|
if (env('AUTOMATIC_ADDON_OVERRIDES', true)) { |
105
|
|
|
$lines = $this->loadAddonOverrides($lines, $locale, $group, $namespace); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$lines = $this->loadSystemOverrides($lines, $locale, $group, $namespace); |
109
|
|
|
$lines = $this->loadApplicationOverrides($lines, $locale, $group, $namespace); |
110
|
|
|
|
111
|
|
|
return parent::loadNamespaceOverrides($lines, $locale, $group, $namespace); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Load system overrides. |
116
|
|
|
* |
117
|
|
|
* @param array $lines |
118
|
|
|
* @param $locale |
119
|
|
|
* @param $group |
120
|
|
|
* @param $namespace |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function loadSystemOverrides(array $lines, $locale, $group, $namespace = null) |
124
|
|
|
{ |
125
|
|
|
if (!$namespace || $namespace == 'streams') { |
126
|
|
|
|
127
|
|
|
$file = base_path("resources/streams/lang/{$locale}/{$group}.php"); |
128
|
|
|
|
129
|
|
|
if (is_dir(base_path("resources/streams/lang")) && $this->files->exists($file)) { |
130
|
|
|
$lines = array_replace_recursive($lines, $this->files->getRequire($file)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (str_is('*.*.*', $namespace)) { |
|
|
|
|
135
|
|
|
|
136
|
|
|
list($vendor, $type, $slug) = explode('.', $namespace); |
137
|
|
|
|
138
|
|
|
$file = base_path("resources/addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php"); |
139
|
|
|
|
140
|
|
|
if (is_dir(base_path("resources/addons/{$vendor}/{$slug}-{$type}/lang")) && $this->files->exists($file)) { |
141
|
|
|
$lines = array_replace_recursive($lines, $this->files->getRequire($file)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $lines; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Load system overrides. |
150
|
|
|
* |
151
|
|
|
* @param array $lines |
152
|
|
|
* @param $locale |
153
|
|
|
* @param $group |
154
|
|
|
* @param $namespace |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
protected function loadApplicationOverrides(array $lines, $locale, $group, $namespace = null) |
158
|
|
|
{ |
159
|
|
|
if (!$namespace || $namespace == 'streams') { |
160
|
|
|
|
161
|
|
|
$file = $this->application->getResourcesPath("streams/lang/{$locale}/{$group}.php"); |
162
|
|
|
|
163
|
|
|
if (is_dir($this->application->getResourcesPath("streams/lang")) && $this->files->exists($file)) { |
164
|
|
|
$lines = array_replace_recursive($lines, $this->files->getRequire($file)); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (str_is('*.*.*', $namespace)) { |
|
|
|
|
169
|
|
|
|
170
|
|
|
list($vendor, $type, $slug) = explode('.', $namespace); |
171
|
|
|
|
172
|
|
|
$file = $this->application->getResourcesPath( |
173
|
|
|
"addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php" |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
if ( |
177
|
|
|
is_dir($this->application->getResourcesPath("addons/{$vendor}/{$slug}-{$type}/lang")) |
178
|
|
|
&& $this->files->exists($file) |
179
|
|
|
) { |
180
|
|
|
$lines = array_replace_recursive($lines, $this->files->getRequire($file)); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
if (config()->has('override_text')) { |
184
|
|
|
foreach (config()->get('override_text') as $override) { |
185
|
|
|
$override = explode(':', $override); |
186
|
|
|
|
187
|
|
|
$lines = $this->findArrayValue($override[0], $override[1], $lines); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $lines; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
function replaceNewValue($find_value, $new_value, $arr) |
195
|
|
|
{ |
196
|
|
|
if (is_array($arr)) { |
197
|
|
|
foreach ($arr as $key => $item) { |
198
|
|
|
$arr[$key] = $this->replaceNewValue($find_value, $new_value, $item); |
199
|
|
|
} |
200
|
|
|
return $arr; |
201
|
|
|
} |
202
|
|
|
if (strtolower($arr) == strtolower($find_value)) { |
203
|
|
|
return $new_value; |
204
|
|
|
} |
205
|
|
|
return $arr; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
function findArrayValue($find_value, $new_value, $arr) |
209
|
|
|
{ |
210
|
|
|
foreach ($arr as $key => $item) { |
211
|
|
|
$arr[$key] = $this->replaceNewValue($find_value, $new_value, $item); |
212
|
|
|
} |
213
|
|
|
return $arr; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param array $lines |
219
|
|
|
* @param $locale |
220
|
|
|
* @param $group |
221
|
|
|
* @param null $namespace |
|
|
|
|
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
protected function loadAddonOverrides(array $lines, $locale, $group, $namespace = null) |
225
|
|
|
{ |
226
|
|
|
/** @var Addon $addon */ |
227
|
|
|
foreach ($this->addons->enabled() as $addon) { |
228
|
|
|
|
229
|
|
|
$disabled = array_get(self::$disabled, $key = $addon->getNamespace('streams'), false); |
|
|
|
|
230
|
|
|
|
231
|
|
|
if (!$disabled && !$this->files->isDirectory($addon->getPath('resources/streams'))) { |
232
|
|
|
self::$disabled[$key] = $disabled = true; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (!$disabled && (!$namespace || $namespace == 'streams')) { |
236
|
|
|
|
237
|
|
|
$file = $addon->getPath("resources/streams/lang/{$locale}/{$group}.php"); |
238
|
|
|
|
239
|
|
|
if ($this->files->exists($file)) { |
240
|
|
|
$lines = array_replace_recursive($lines, $this->files->getRequire($file)); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$disabled = array_get(self::$disabled, $key = $addon->getNamespace('addons'), false); |
|
|
|
|
245
|
|
|
|
246
|
|
|
if (!$disabled && !$this->files->isDirectory($addon->getPath('resources/addons'))) { |
247
|
|
|
self::$disabled[$key] = $disabled = true; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (!$disabled && str_is('*.*.*', $namespace)) { |
|
|
|
|
251
|
|
|
|
252
|
|
|
list($vendor, $type, $slug) = explode('.', $namespace); |
253
|
|
|
|
254
|
|
|
$file = $addon->getPath( |
255
|
|
|
"resources/addons/{$vendor}/{$slug}-{$type}/lang/{$locale}/{$group}.php" |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
if ($this->files->exists($file)) { |
259
|
|
|
$lines = array_replace_recursive($lines, $this->files->getRequire($file)); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $lines; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.