Commit 507e7280 by 杨树贤

fix

parent d29646e4
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
echo collect(app("Illuminate\Contracts\Http\Kernel")->getMiddlewareGroups())
->merge(app("Illuminate\Contracts\Http\Kernel")->getRouteMiddleware())
->map(function ($middleware, $key) {
$result = [
"class" => null,
"path" => null,
"line" => null,
"parameters" => null,
"groups" => [],
];
if (is_array($middleware)) {
$result["groups"] = collect($middleware)->map(function ($m) {
if (!class_exists($m)) {
return [
"class" => $m,
"path" => null,
"line" => null
];
}
$reflected = new ReflectionClass($m);
$reflectedMethod = $reflected->getMethod("handle");
return [
"class" => $m,
"path" => LaravelVsCode::relativePath($reflected->getFileName()),
"line" =>
$reflectedMethod->getFileName() === $reflected->getFileName()
? $reflectedMethod->getStartLine()
: null
];
})->all();
return $result;
}
$reflected = new ReflectionClass($middleware);
$reflectedMethod = $reflected->getMethod("handle");
$result = array_merge($result, [
"class" => $middleware,
"path" => LaravelVsCode::relativePath($reflected->getFileName()),
"line" => $reflectedMethod->getStartLine(),
]);
$parameters = collect($reflectedMethod->getParameters())
->filter(function ($rc) {
return $rc->getName() !== "request" && $rc->getName() !== "next";
})
->map(function ($rc) {
return $rc->getName() . ($rc->isVariadic() ? "..." : "");
});
if ($parameters->isEmpty()) {
return $result;
}
return array_merge($result, [
"parameters" => $parameters->implode(",")
]);
})
->toJson();
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
$routes = new class {
public function all()
{
return collect(app('router')->getRoutes()->getRoutes())
->map(fn(\Illuminate\Routing\Route $route) => $this->getRoute($route))
->merge($this->getFolioRoutes());
}
protected function getFolioRoutes()
{
try {
$output = new \Symfony\Component\Console\Output\BufferedOutput();
\Illuminate\Support\Facades\Artisan::call("folio:list", ["--json" => true], $output);
$mountPaths = collect(app(\Laravel\Folio\FolioManager::class)->mountPaths());
return collect(json_decode($output->fetch(), true))->map(fn($route) => $this->getFolioRoute($route, $mountPaths));
} catch (\Exception | \Throwable $e) {
return [];
}
}
protected function getFolioRoute($route, $mountPaths)
{
if ($mountPaths->count() === 1) {
$mountPath = $mountPaths[0];
} else {
$mountPath = $mountPaths->first(fn($mp) => file_exists($mp->path . DIRECTORY_SEPARATOR . $route['view']));
}
$path = $route['view'];
if ($mountPath) {
$path = $mountPath->path . DIRECTORY_SEPARATOR . $path;
}
return [
'method' => $route['method'],
'uri' => $route['uri'],
'name' => $route['name'],
'action' => null,
'parameters' => [],
'filename' => $path,
'line' => 0,
];
}
protected function getRoute(\Illuminate\Routing\Route $route)
{
try {
$reflection = $this->getRouteReflection($route);
} catch (\Throwable $e) {
$reflection = null;
}
return [
'method' => collect($route->methods())
->filter(fn($method) => $method !== 'HEAD')
->implode('|'),
'uri' => $route->uri(),
'name' => $route->getName(),
'action' => $route->getActionName(),
'parameters' => $route->parameterNames(),
'filename' => $reflection ? $reflection->getFileName() : null,
'line' => $reflection ? $reflection->getStartLine() : null,
];
}
protected function getRouteReflection(\Illuminate\Routing\Route $route)
{
if ($route->getActionName() === 'Closure') {
return new \ReflectionFunction($route->getAction()['uses']);
}
if (!str_contains($route->getActionName(), '@')) {
return new \ReflectionClass($route->getActionName());
}
try {
return new \ReflectionMethod($route->getControllerClass(), $route->getActionMethod());
} catch (\Throwable $e) {
$namespace = app(\Illuminate\Routing\UrlGenerator::class)->getRootControllerNamespace()
?? (app()->getNamespace() . 'Http\Controllers');
return new \ReflectionMethod(
$namespace . '\\' . ltrim($route->getControllerClass(), '\\'),
$route->getActionMethod(),
);
}
}
};
echo $routes->all()->toJson();
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
echo collect(app()->getBindings())
->filter(fn ($binding) => ($binding['concrete'] ?? null) !== null)
->flatMap(function ($binding, $key) {
$boundTo = new ReflectionFunction($binding['concrete']);
$closureClass = $boundTo->getClosureScopeClass();
if ($closureClass === null) {
return [];
}
return [
$key => [
'path' => LaravelVsCode::relativePath($closureClass->getFileName()),
'class' => $closureClass->getName(),
'line' => $boundTo->getStartLine(),
],
];
})->toJson();
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
echo json_encode([
...config('inertia.testing', []),
'page_paths' => collect(config('inertia.testing.page_paths', []))->flatMap(function($path) {
$relativePath = LaravelVsCode::relativePath($path);
return [$relativePath, mb_strtolower($relativePath)];
})->unique()->values(),
]);
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
echo json_encode([
'php_version' => phpversion(),
'laravel_version' => app()->version(),
]);
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
echo json_encode([
[
'key' => 'base_path',
'path' => base_path(),
],
[
'key' => 'resource_path',
'path' => resource_path(),
],
[
'key' => 'config_path',
'path' => config_path(),
],
[
'key' => 'app_path',
'path' => app_path(),
],
[
'key' => 'database_path',
'path' => database_path(),
],
[
'key' => 'lang_path',
'path' => lang_path(),
],
[
'key' => 'public_path',
'path' => public_path(),
],
[
'key' => 'storage_path',
'path' => storage_path(),
],
]);
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
$blade = new class {
public function getAllViews()
{
$finder = app("view")->getFinder();
$paths = collect($finder->getPaths())->flatMap(fn($path) => $this->findViews($path));
$hints = collect($finder->getHints())->flatMap(
fn($paths, $key) => collect($paths)->flatMap(
fn($path) => collect($this->findViews($path))->map(
fn($value) => array_merge($value, ["key" => "{$key}::{$value["key"]}"])
)
)
);
[$local, $vendor] = $paths
->merge($hints)
->values()
->partition(fn($v) => !$v["isVendor"]);
return $local
->sortBy("key", SORT_NATURAL)
->merge($vendor->sortBy("key", SORT_NATURAL));
}
public function getAllComponents()
{
$namespaced = \Illuminate\Support\Facades\Blade::getClassComponentNamespaces();
$autoloaded = require base_path("vendor/composer/autoload_psr4.php");
$components = [];
foreach ($namespaced as $key => $ns) {
$path = null;
foreach ($autoloaded as $namespace => $paths) {
if (str_starts_with($ns, $namespace)) {
foreach ($paths as $p) {
$test = str($ns)->replace($namespace, '')->replace('\\', '/')->prepend($p . DIRECTORY_SEPARATOR)->toString();
if (is_dir($test)) {
$path = $test;
break;
}
}
break;
}
}
if (!$path) {
continue;
}
$files = \Symfony\Component\Finder\Finder::create()
->files()
->name("*.php")
->in($path);
foreach ($files as $file) {
$realPath = $file->getRealPath();
$components[] = [
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $realPath),
"isVendor" => str_contains($realPath, base_path("vendor")),
"key" => str($realPath)
->replace(realpath($path), "")
->replace(".php", "")
->ltrim(DIRECTORY_SEPARATOR)
->replace(DIRECTORY_SEPARATOR, ".")
->kebab()
->prepend($key . "::"),
];
}
}
return $components;
}
protected function findViews($path)
{
$paths = [];
if (!is_dir($path)) {
return $paths;
}
$files = \Symfony\Component\Finder\Finder::create()
->files()
->name("*.blade.php")
->in($path);
foreach ($files as $file) {
$paths[] = [
"path" => str_replace(base_path(DIRECTORY_SEPARATOR), '', $file->getRealPath()),
"isVendor" => str_contains($file->getRealPath(), base_path("vendor")),
"key" => str($file->getRealPath())
->replace(realpath($path), "")
->replace(".blade.php", "")
->ltrim(DIRECTORY_SEPARATOR)
->replace(DIRECTORY_SEPARATOR, ".")
];
}
return $paths;
}
};
echo json_encode($blade->getAllViews()->merge($blade->getAllComponents()));
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
$local = collect(glob(config_path("/*.php")))
->merge(glob(config_path("**/*.php")))
->map(fn ($path) => [
(string) str($path)
->replace([config_path('/'), ".php"], "")
->replace(DIRECTORY_SEPARATOR, "."),
$path
]);
$vendor = collect(glob(base_path("vendor/**/**/config/*.php")))->map(fn (
$path
) => [
(string) str($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 = str($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 [
"name" => $key,
"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()
->values()
->toJson();
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
echo collect(app(\Illuminate\View\Compilers\BladeCompiler::class)->getCustomDirectives())
->map(function ($customDirective, $name) {
if ($customDirective instanceof \Closure) {
return [
'name' => $name,
'hasParams' => (new ReflectionFunction($customDirective))->getNumberOfParameters() >= 1,
];
}
if (is_array($customDirective)) {
return [
'name' => $name,
'hasParams' => (new ReflectionMethod($customDirective[0], $customDirective[1]))->getNumberOfParameters() >= 1,
];
}
return null;
})
->filter()
->values()
->toJson();
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
if (class_exists('\phpDocumentor\Reflection\DocBlockFactory')) {
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
} else {
$factory = null;
}
$docblocks = new class($factory) {
public function __construct(protected $factory) {}
public function forMethod($method)
{
if ($this->factory !== null) {
$docblock = $this->factory->create($method->getDocComment());
$params = collect($docblock->getTagsByName("param"))->map(fn($p) => (string) $p)->all();
$return = (string) $docblock->getTagsByName("return")[0] ?? null;
return [$params, $return];
}
$params = collect($method->getParameters())
->map(function (\ReflectionParameter $param) {
$types = match ($param?->getType()) {
null => [],
default => method_exists($param->getType(), "getTypes")
? $param->getType()->getTypes()
: [$param->getType()]
};
$types = collect($types)
->filter()
->values()
->map(fn($t) => $t->getName());
return trim($types->join("|") . " $" . $param->getName());
})
->all();
$return = $method->getReturnType()?->getName();
return [$params, $return];
}
};
$models = new class($factory) {
protected $output;
public function __construct(protected $factory)
{
$this->output = new \Symfony\Component\Console\Output\BufferedOutput();
}
public function all()
{
collect(glob(base_path('**/Models/*.php')))->each(fn($file) => include_once($file));
return collect(get_declared_classes())
->filter(fn($class) => is_subclass_of($class, \Illuminate\Database\Eloquent\Model::class))
->filter(fn($class) => !in_array($class, [\Illuminate\Database\Eloquent\Relations\Pivot::class, \Illuminate\Foundation\Auth\User::class]))
->values()
->flatMap(fn(string $className) => $this->getInfo($className))
->filter();
}
protected function getCastReturnType($className)
{
if ($className === null) {
return null;
}
try {
$method = (new \ReflectionClass($className))->getMethod('get');
if ($method->hasReturnType()) {
return $method->getReturnType()->getName();
}
return $className;
} catch (\Exception | \Throwable $e) {
return $className;
}
}
protected function fromArtisan($className)
{
try {
\Illuminate\Support\Facades\Artisan::call(
"model:show",
[
"model" => $className,
"--json" => true,
],
$this->output
);
} catch (\Exception | \Throwable $e) {
return null;
}
return json_decode($this->output->fetch(), true);
}
protected function collectExistingProperties($reflection)
{
if ($this->factory === null) {
return collect();
}
if ($comment = $reflection->getDocComment()) {
$docblock = $this->factory->create($comment);
$existingProperties = collect($docblock->getTagsByName("property"))->map(fn($p) => $p->getVariableName());
$existingReadProperties = collect($docblock->getTagsByName("property-read"))->map(fn($p) => $p->getVariableName());
return $existingProperties->merge($existingReadProperties);
}
return collect();
}
protected function getParentClass(\ReflectionClass $reflection)
{
if (!$reflection->getParentClass()) {
return null;
}
$parent = $reflection->getParentClass()->getName();
if ($parent === \Illuminate\Database\Eloquent\Model::class) {
return null;
}
return \Illuminate\Support\Str::start($parent, '\\');
}
protected function getInfo($className)
{
if (($data = $this->fromArtisan($className)) === null) {
return null;
}
$reflection = new \ReflectionClass($className);
$data["extends"] = $this->getParentClass($reflection);
$existingProperties = $this->collectExistingProperties($reflection);
$data['attributes'] = collect($data['attributes'])
->map(fn($attrs) => array_merge($attrs, [
'title_case' => str($attrs['name'])->title()->replace('_', '')->toString(),
'documented' => $existingProperties->contains($attrs['name']),
'cast' => $this->getCastReturnType($attrs['cast'])
]))
->toArray();
$data['scopes'] = collect($reflection->getMethods())
->filter(fn($method) => $method->isPublic() && !$method->isStatic() && str_starts_with($method->name, 'scope'))
->map(fn($method) => str($method->name)->replace('scope', '')->lcfirst()->toString())
->values()
->toArray();
$data['uri'] = $reflection->getFileName();
return [
$className => $data,
];
}
};
$builder = new class($docblocks) {
public function __construct(protected $docblocks) {}
public function methods()
{
$reflection = new \ReflectionClass(\Illuminate\Database\Query\Builder::class);
return collect($reflection->getMethods(\ReflectionMethod::IS_PUBLIC))
->filter(fn(ReflectionMethod $method) => !str_starts_with($method->getName(), "__"))
->map(fn(\ReflectionMethod $method) => $this->getMethodInfo($method))
->filter()
->values();
}
protected function getMethodInfo($method)
{
[$params, $return] = $this->docblocks->forMethod($method);
return [
"name" => $method->getName(),
"parameters" => $params,
"return" => $return,
];
}
};
echo json_encode([
'builderMethods' => $builder->methods(),
'models' => $models->all(),
]);
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
<?php
error_reporting(E_ERROR | E_PARSE);
define('LARAVEL_START', microtime(true));
require_once __DIR__ . '/../autoload.php';
class LaravelVsCode
{
public static function relativePath($path)
{
if (!str_contains($path, base_path())) {
return (string) $path;
}
return ltrim(str_replace(base_path(), '', realpath($path) ?: $path), DIRECTORY_SEPARATOR);
}
public static function isVendor($path)
{
return str_contains($path, base_path("vendor"));
}
public static function outputMarker($key)
{
return '__VSCODE_LARAVEL_' . $key . '__';
}
public static function startupError(\Throwable $e)
{
throw new Error(self::outputMarker('STARTUP_ERROR') . ': ' . $e->getMessage());
}
}
try {
$app = require_once __DIR__ . '/../../bootstrap/app.php';
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
$app->register(new class($app) extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
config([
'logging.channels.null' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class,
],
'logging.default' => 'null',
]);
}
});
try {
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (\Throwable $e) {
LaravelVsCode::startupError($e);
exit(1);
}
echo LaravelVsCode::outputMarker('START_OUTPUT');
collect(glob(base_path('**/Models/*.php')))->each(fn($file) => include_once($file));
$modelPolicies = collect(get_declared_classes())
->filter(fn($class) => is_subclass_of($class, \Illuminate\Database\Eloquent\Model::class))
->filter(fn($class) => !in_array($class, [
\Illuminate\Database\Eloquent\Relations\Pivot::class,
\Illuminate\Foundation\Auth\User::class,
]))
->flatMap(fn($class) => [
$class => \Illuminate\Support\Facades\Gate::getPolicyFor($class),
])
->filter(fn($policy) => $policy !== null);
function vsCodeGetPolicyInfo($policy, $model)
{
$methods = (new ReflectionClass($policy))->getMethods();
return collect($methods)->map(fn(ReflectionMethod $method) => [
'key' => $method->getName(),
'uri' => $method->getFileName(),
'policy' => is_string($policy) ? $policy : get_class($policy),
'model' => $model,
'line' => $method->getStartLine(),
])->filter(fn($ability) => !in_array($ability['key'], ['allow', 'deny']));
}
echo collect(\Illuminate\Support\Facades\Gate::abilities())
->map(function ($policy, $key) {
$reflection = new \ReflectionFunction($policy);
$policyClass = null;
$closureThis = $reflection->getClosureThis();
if ($closureThis !== null) {
if (get_class($closureThis) === \Illuminate\Auth\Access\Gate::class) {
$vars = $reflection->getClosureUsedVariables();
if (isset($vars['callback'])) {
[$policyClass, $method] = explode('@', $vars['callback']);
$reflection = new \ReflectionMethod($policyClass, $method);
}
}
}
return [
'key' => $key,
'uri' => $reflection->getFileName(),
'policy' => $policyClass,
'line' => $reflection->getStartLine(),
];
})
->merge(
collect(\Illuminate\Support\Facades\Gate::policies())->flatMap(fn($policy, $model) => vsCodeGetPolicyInfo($policy, $model)),
)
->merge(
$modelPolicies->flatMap(fn($policy, $model) => vsCodeGetPolicyInfo($policy, $model)),
)
->values()
->groupBy('key')
->map(fn($item) => $item->map(fn($i) => \Illuminate\Support\Arr::except($i, 'key')))
->toJson();
echo LaravelVsCode::outputMarker('END_OUTPUT');
exit(0);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment