Merge branch 'feat-meta-hints' of github.com:barryvdh/laravel-ide-helper into feat-meta-hints

This commit is contained in:
Barry vd. Heuvel
2024-12-29 19:31:53 +01:00
+31 -31
View File
@@ -8,31 +8,31 @@ function vsCodeGetTranslationsFromFile($file, $path, $namespace)
$key = "{$namespace}::{$key}"; $key = "{$namespace}::{$key}";
} }
$lang = collect(explode(DIRECTORY_SEPARATOR, str_replace($path, "", $file))) $lang = collect(explode(DIRECTORY_SEPARATOR, str_replace($path, '', $file)))
->filter() ->filter()
->first(); ->first();
$fileLines = \Illuminate\Support\Facades\File::lines($file); $fileLines = Illuminate\Support\Facades\File::lines($file);
$lines = []; $lines = [];
$inComment = false; $inComment = false;
foreach ($fileLines as $index => $line) { foreach ($fileLines as $index => $line) {
$trimmed = trim($line); $trimmed = trim($line);
if (substr($trimmed, 0, 2) === "/*") { if (substr($trimmed, 0, 2) === '/*') {
$inComment = true; $inComment = true;
continue; continue;
} }
if ($inComment) { if ($inComment) {
if (substr($trimmed, -2) !== "*/") { if (substr($trimmed, -2) !== '*/') {
continue; continue;
} }
$inComment = false; $inComment = false;
} }
if (substr($trimmed, 0, 2) === "//") { if (substr($trimmed, 0, 2) === '//') {
continue; continue;
} }
@@ -40,18 +40,18 @@ function vsCodeGetTranslationsFromFile($file, $path, $namespace)
} }
return [ return [
"k" => $key, 'k' => $key,
"la" => $lang, 'la' => $lang,
"vs" => collect(\Illuminate\Support\Arr::dot((\Illuminate\Support\Arr::wrap(__($key, [], $lang))))) 'vs' => collect(Illuminate\Support\Arr::dot((Illuminate\Support\Arr::wrap(__($key, [], $lang)))))
->map( ->map(
fn($value, $key) => vsCodeTranslationValue( fn ($value, $key) => vsCodeTranslationValue(
$key, $key,
$value, $value,
str_replace(base_path(DIRECTORY_SEPARATOR), "", $file), str_replace(base_path(DIRECTORY_SEPARATOR), '', $file),
$lines $lines
) )
) )
->filter() ->filter(),
]; ];
} }
@@ -62,7 +62,7 @@ function vsCodeTranslationValue($key, $value, $file, $lines): ?array
} }
$lineNumber = 1; $lineNumber = 1;
$keys = explode(".", $key); $keys = explode('.', $key);
$index = 0; $index = 0;
$currentKey = array_shift($keys); $currentKey = array_shift($keys);
@@ -81,12 +81,12 @@ function vsCodeTranslationValue($key, $value, $file, $lines): ?array
} }
return [ return [
"v" => $value, 'v' => $value,
"p" => $file, 'p' => $file,
"li" => $lineNumber, 'li' => $lineNumber,
"pa" => preg_match_all("/\:([A-Za-z0-9_]+)/", $value, $matches) 'pa' => preg_match_all("/\:([A-Za-z0-9_]+)/", $value, $matches)
? $matches[1] ? $matches[1]
: [] : [],
]; ];
} }
@@ -98,44 +98,44 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)
return collect(); return collect();
} }
return collect(\Illuminate\Support\Facades\File::allFiles($realPath))->map( return collect(Illuminate\Support\Facades\File::allFiles($realPath))->map(
fn($file) => vsCodeGetTranslationsFromFile($file, $path, $namespace) fn ($file) => vsCodeGetTranslationsFromFile($file, $path, $namespace)
); );
} }
$loader = app("translator")->getLoader(); $loader = app('translator')->getLoader();
$namespaces = $loader->namespaces(); $namespaces = $loader->namespaces();
$reflection = new ReflectionClass($loader); $reflection = new ReflectionClass($loader);
$property = $reflection->hasProperty("paths") $property = $reflection->hasProperty('paths')
? $reflection->getProperty("paths") ? $reflection->getProperty('paths')
: $reflection->getProperty("path"); : $reflection->getProperty('path');
$property->setAccessible(true); $property->setAccessible(true);
$paths = \Illuminate\Support\Arr::wrap($property->getValue($loader)); $paths = Illuminate\Support\Arr::wrap($property->getValue($loader));
$default = collect($paths)->flatMap( $default = collect($paths)->flatMap(
fn($path) => vscodeCollectTranslations($path) fn ($path) => vscodeCollectTranslations($path)
); );
$namespaced = collect($namespaces)->flatMap( $namespaced = collect($namespaces)->flatMap(
fn($path, $namespace) => vscodeCollectTranslations($path, $namespace) fn ($path, $namespace) => vscodeCollectTranslations($path, $namespace)
); );
$final = []; $final = [];
foreach ($default->merge($namespaced) as $value) { foreach ($default->merge($namespaced) as $value) {
foreach ($value["vs"] as $key => $v) { foreach ($value['vs'] as $key => $v) {
$dotKey = "{$value["k"]}.{$key}"; $dotKey = "{$value['k']}.{$key}";
if (!isset($final[$dotKey])) { if (!isset($final[$dotKey])) {
$final[$dotKey] = []; $final[$dotKey] = [];
} }
$final[$dotKey][$value["la"]] = $v; $final[$dotKey][$value['la']] = $v;
if ($value["la"] === \Illuminate\Support\Facades\App::currentLocale()) { if ($value['la'] === Illuminate\Support\Facades\App::currentLocale()) {
$final[$dotKey]["default"] = $v; $final[$dotKey]['default'] = $v;
} }
} }
} }