55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
// SPDX-License-Identifier: EUPL-1.2
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Core\Mcp\Resources;
|
||
|
|
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Facades\Schema;
|
||
|
|
use Laravel\Mcp\Request;
|
||
|
|
use Laravel\Mcp\Response;
|
||
|
|
use Laravel\Mcp\Server\Resource;
|
||
|
|
|
||
|
|
final class DatabaseSchema extends Resource
|
||
|
|
{
|
||
|
|
protected string $description = 'Database schema information for Host Hub';
|
||
|
|
|
||
|
|
public function handle(Request $request): Response
|
||
|
|
{
|
||
|
|
$schema = [];
|
||
|
|
|
||
|
|
foreach ($this->tables() as $tableName) {
|
||
|
|
$schema[$tableName] = $this->describeTable($tableName);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Response::text((string) json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function tables(): array
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
return collect(DB::select('SHOW TABLES'))
|
||
|
|
->map(static fn (object $row): string => (string) array_values((array) $row)[0])
|
||
|
|
->all();
|
||
|
|
} catch (\Throwable) {
|
||
|
|
return Schema::getTableListing();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function describeTable(string $tableName): array
|
||
|
|
{
|
||
|
|
$driver = DB::getDriverName();
|
||
|
|
|
||
|
|
try {
|
||
|
|
return array_map(static fn (object $column): array => (array) $column, DB::select(sprintf(
|
||
|
|
$driver === 'sqlite' ? 'PRAGMA table_info("%s")' : 'DESCRIBE `%s`',
|
||
|
|
$tableName,
|
||
|
|
)));
|
||
|
|
} catch (\Throwable) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|