-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLocaleController.php
More file actions
193 lines (158 loc) · 5.39 KB
/
GetLocaleController.php
File metadata and controls
193 lines (158 loc) · 5.39 KB
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
<?php
namespace Empuxa\LocaleViaApi\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class GetLocaleController extends Controller
{
private const LOCALE_NOT_FOUND = 'Locale not found.';
private bool $flatten;
/**
* Handle the incoming request.
*
* @throws \Throwable
*/
public function __invoke(Request $request, string $locale): JsonResponse
{
$this->flatten = $request->query('flatten', config('locale-via-api.flatten', false));
// Ensure locale is valid and exists
$this->ensureLocaleIsValid($locale);
$this->ensureLocaleExists($locale);
// Get cached data or generate it if not present
$data = Cache::driver(config('locale-via-api.cache.driver', 'array'))->remember(
$this->getCacheKey($locale),
config('locale-via-api.cache.duration', 3600),
function () use ($locale) {
return $this->getMergedLocaleData($locale);
}
);
// Return JSON response
return $this->createJsonResponse($data);
}
/**
* Ensure the locale is valid.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
private function ensureLocaleIsValid(string $locale): void
{
abort_unless(in_array($locale, config('locale-via-api.locales'), true), 404, self::LOCALE_NOT_FOUND);
}
/**
* Ensure the locale directory exists.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
private function ensureLocaleExists(string $locale): void
{
abort_unless(File::exists(lang_path($locale)), 404, self::LOCALE_NOT_FOUND);
}
/**
* Get the cache key for the given locale.
*/
private function getCacheKey(string $locale): string
{
return sprintf('%s%s', config('locale-via-api.cache.prefix', 'locale-via-api:'), $locale);
}
/**
* Get merged locale data.
*/
private function getMergedLocaleData(string $locale): array
{
$data = $this->getLocaleData($locale);
if (config('locale-via-api.load_vendor_files', true)) {
// Get vendor directories
$vendorLocales = File::directories(lang_path('vendor'));
$safelist = config('locale-via-api.vendor_safelist');
foreach ($vendorLocales as $vendorLocale) {
$vendorName = basename($vendorLocale);
// Skip if safelist is set and vendor is not in it
if (is_array($safelist) && ! in_array($vendorName, $safelist, true)) {
continue;
}
$data = array_merge_recursive(
$data,
$this->getVendorLocaleData(sprintf('vendor/%s/%s', $vendorName, $locale), $vendorName)
);
}
}
ksort($data);
return $data;
}
/**
* Get locale data from files.
*/
protected function getLocaleData(string $locale): array
{
return $this->loadLocaleFiles(lang_path($locale));
}
/**
* Get vendor locale data from files.
*/
protected function getVendorLocaleData(string $path, string $vendorName): array
{
return $this->loadLocaleFiles(lang_path($path), sprintf('vendor.%s', $vendorName));
}
/**
* Load locale files from a given path.
*/
protected function loadLocaleFiles(string $directory, string $prefix = ''): array
{
$data = [];
if (! File::exists($directory)) {
return $data;
}
$files = File::allFiles($directory);
foreach ($files as $file) {
if ($file->getExtension() !== 'php') {
continue;
}
$relativePath = Str::replaceFirst($directory . DIRECTORY_SEPARATOR, '', $file->getPathname());
$fileName = Str::before($relativePath, '.');
// Convert the relative path to a dot notation key
$key = Str::replace(DIRECTORY_SEPARATOR, '.', $fileName);
if ($prefix) {
$key = sprintf('%s.%s', $prefix, $key);
}
$fileData = File::getRequire($file);
if ($this->flatten) {
$flattenedData = $this->flattenArray($fileData, $key);
$data = array_merge($data, $flattenedData);
} else {
$data[$key] = $fileData;
}
}
return $data;
}
/**
* Flatten a multi-dimensional associative array with dot notation keys.
*/
protected function flattenArray(array $array, string $prefix = ''): array
{
$result = [];
foreach ($array as $key => $value) {
$newKey = $prefix ? $prefix . '.' . $key : $key;
if (is_array($value)) {
$result = array_merge($result, $this->flattenArray($value, $newKey));
} else {
$result[$newKey] = $value;
}
}
return $result;
}
/**
* Create a JSON response.
*/
private function createJsonResponse(array $data): JsonResponse
{
return response()->json([
'data' => $data,
'meta' => [
'hash' => hash('sha256', json_encode($data)),
],
]);
}
}