|
| 1 | +<?php |
| 2 | + |
| 3 | +$local = collect(glob(config_path("/*.php"))) |
| 4 | + ->merge(glob(config_path("**/*.php"))) |
| 5 | + ->map(fn ($path) => [ |
| 6 | + (string) \Illuminate\Support\Str::of($path) |
| 7 | + ->replace([config_path("/"), ".php"], "") |
| 8 | + ->replace("/", "."), |
| 9 | + $path |
| 10 | + ]); |
| 11 | + |
| 12 | +$vendor = collect(glob(base_path("vendor/**/**/config/*.php")))->map(fn ( |
| 13 | + $path |
| 14 | +) => [ |
| 15 | + (string) \Illuminate\Support\Str::of($path) |
| 16 | + ->afterLast("/config/") |
| 17 | + ->replace(".php", "") |
| 18 | + ->replace("/", "."), |
| 19 | + $path |
| 20 | +]); |
| 21 | + |
| 22 | +$configPaths = $local |
| 23 | + ->merge($vendor) |
| 24 | + ->groupBy(0) |
| 25 | + ->map(fn ($items)=>$items->pluck(1)); |
| 26 | + |
| 27 | +$cachedContents = []; |
| 28 | +$cachedParsed = []; |
| 29 | + |
| 30 | +function vsCodeGetConfigValue($value, $key, $configPaths) { |
| 31 | + $parts = explode(".", $key); |
| 32 | + $toFind = $key; |
| 33 | + $found = null; |
| 34 | + |
| 35 | + while (count($parts) > 0) { |
| 36 | + array_pop($parts); |
| 37 | + $toFind = implode(".", $parts); |
| 38 | + |
| 39 | + if ($configPaths->has($toFind)) { |
| 40 | + $found = $toFind; |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if ($found === null) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + $file = null; |
| 50 | + $line = null; |
| 51 | + |
| 52 | + if ($found === $key) { |
| 53 | + $file = $configPaths->get($found)[0]; |
| 54 | + } else { |
| 55 | + foreach ($configPaths->get($found) as $path) { |
| 56 | + $cachedContents[$path] ??= file_get_contents($path); |
| 57 | + $cachedParsed[$path] ??= token_get_all($cachedContents[$path]); |
| 58 | + |
| 59 | + $keysToFind = \Illuminate\Support\Str::of($key) |
| 60 | + ->replaceFirst($found, "") |
| 61 | + ->ltrim(".") |
| 62 | + ->explode("."); |
| 63 | + |
| 64 | + if (is_numeric($keysToFind->last())) { |
| 65 | + $index = $keysToFind->pop(); |
| 66 | + |
| 67 | + if ($index !== "0") { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + $key = collect(explode(".", $key)); |
| 72 | + $key->pop(); |
| 73 | + $key = $key->implode("."); |
| 74 | + $value = "array(...)"; |
| 75 | + } |
| 76 | + |
| 77 | + $nextKey = $keysToFind->shift(); |
| 78 | + $expectedDepth = 1; |
| 79 | + |
| 80 | + $depth = 0; |
| 81 | + |
| 82 | + foreach ($cachedParsed[$path] as $token) { |
| 83 | + if ($token === "[") { |
| 84 | + $depth++; |
| 85 | + } |
| 86 | + |
| 87 | + if ($token === "]") { |
| 88 | + $depth--; |
| 89 | + } |
| 90 | + |
| 91 | + if (!is_array($token)) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $str = trim($token[1], '"\''); |
| 96 | + |
| 97 | + if ( |
| 98 | + $str === $nextKey && |
| 99 | + $depth === $expectedDepth && |
| 100 | + $token[0] === T_CONSTANT_ENCAPSED_STRING |
| 101 | + ) { |
| 102 | + $nextKey = $keysToFind->shift(); |
| 103 | + $expectedDepth++; |
| 104 | + |
| 105 | + if ($nextKey === null) { |
| 106 | + $file = $path; |
| 107 | + $line = $token[2]; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if ($file) { |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return [ |
| 120 | + "name" => $key, |
| 121 | + "value" => $value, |
| 122 | + "file" => $file === null ? null : str_replace(base_path('/'), '', $file), |
| 123 | + "line" => $line |
| 124 | + ]; |
| 125 | +} |
| 126 | + |
| 127 | +return collect(\Illuminate\Support\Arr::dot(config()->all())) |
| 128 | + ->map(fn ($value, $key) => vsCodeGetConfigValue($value, $key, $configPaths)) |
| 129 | + ->filter() |
| 130 | + ->values(); |
0 commit comments