1
0
Fork 0
mirror of https://github.com/laravel-ls/go-php synced 2026-06-17 20:40:03 +02:00

Initial commit

This commit is contained in:
Henrik Hautakoski 2025-11-02 07:55:45 +01:00
commit 4cbf7e62b7
19 changed files with 1295 additions and 0 deletions

152
cmd/configs.php Normal file
View file

@ -0,0 +1,152 @@
<?php
// generated by laravel-ls template compiler. Do not edit.
error_reporting(E_ERROR | E_PARSE);
require_once 'vendor/autoload.php';
$app = require_once 'bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
$local = collect(glob(config_path("/*.php")))
->merge(glob(config_path("**/*.php")))
->map(fn ($path) => [
(string) \Illuminate\Support\Str::of($path)
->replace([config_path('/'), ".php"], "")
->replace(DIRECTORY_SEPARATOR, "."),
$path
]);
$vendor = collect(glob(base_path("vendor/**/**/config/*.php")))->map(fn (
$path
) => [
(string) \Illuminate\Support\Str::of($path)
->afterLast(DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR)
->replace(".php", "")
->replace(DIRECTORY_SEPARATOR, "."),
$path
]);
$configPaths = $local
->merge($vendor)
->groupBy(0)
->map(fn ($items)=>$items->pluck(1));
$cachedContents = [];
$cachedParsed = [];
function vsCodeGetConfigValue($value, $key, $configPaths) {
$parts = explode(".", $key);
$toFind = $key;
$found = null;
while (count($parts) > 0) {
$toFind = implode(".", $parts);
if ($configPaths->has($toFind)) {
$found = $toFind;
break;
}
array_pop($parts);
}
if ($found === null) {
return null;
}
$file = null;
$line = null;
if ($found === $key) {
$file = $configPaths->get($found)[0];
} else {
foreach ($configPaths->get($found) as $path) {
$cachedContents[$path] ??= file_get_contents($path);
$cachedParsed[$path] ??= token_get_all($cachedContents[$path]);
$keysToFind = \Illuminate\Support\Str::of($key)
->replaceFirst($found, "")
->ltrim(".")
->explode(".");
if (is_numeric($keysToFind->last())) {
$index = $keysToFind->pop();
if ($index !== "0") {
return null;
}
$key = collect(explode(".", $key));
$key->pop();
$key = $key->implode(".");
$value = "array(...)";
}
$nextKey = $keysToFind->shift();
$expectedDepth = 1;
$depth = 0;
foreach ($cachedParsed[$path] as $token) {
if ($token === "[") {
$depth++;
}
if ($token === "]") {
$depth--;
}
if (!is_array($token)) {
continue;
}
$str = trim($token[1], '"\'');
if (
$str === $nextKey &&
$depth === $expectedDepth &&
$token[0] === T_CONSTANT_ENCAPSED_STRING
) {
$nextKey = $keysToFind->shift();
$expectedDepth++;
if ($nextKey === null) {
$file = $path;
$line = $token[2];
break;
}
}
}
if ($file) {
break;
}
}
}
return [
"value" => $value,
"file" => $file === null ? null : str_replace(base_path(DIRECTORY_SEPARATOR), '', $file),
"line" => $line
];
}
function vsCodeUnpackDottedKey($value, $key) {
$arr = [$key => $value];
$parts = explode('.', $key);
array_pop($parts);
while (count($parts)) {
$arr[implode('.', $parts)] = 'array(...)';
array_pop($parts);
}
return $arr;
}
echo collect(\Illuminate\Support\Arr::dot(config()->all()))
->mapWithKeys(fn($value, $key) => vsCodeUnpackDottedKey($value, $key))
->map(fn ($value, $key) => vsCodeGetConfigValue($value, $key, $configPaths))
->filter()
->toJson();

53
cmd/main.go Normal file
View file

@ -0,0 +1,53 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
_ "embed"
"github.com/laravel-ls/go-php"
)
//go:embed configs.php
var source []byte
type Entry struct {
Value string `json:"value"`
File string `json:"file"`
Line int `json:"line"`
}
// call with ./prog /path/to/laravel/project app.key for example.
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: %s <path> <key>\n", os.Args[0])
os.Exit(1)
}
php.Init()
defer php.Exit()
prog, err := php.Compile(source)
if err != nil {
panic(err)
}
defer prog.Free()
buffer := bytes.Buffer{}
php.Output(&buffer).SetPath(os.Args[1]).Exec(prog)
data := map[string]Entry{}
json.NewDecoder(&buffer).Decode(&data)
for name, ent := range data {
if strings.HasPrefix(name, os.Args[2]) {
fmt.Println("Name:", name)
fmt.Println("File:", ent.File)
fmt.Println("Value:", ent.Value)
fmt.Println("---")
}
}
}