setName('scheme:make')
->addArgument('table', Argument::OPTIONAL, "The table name (without prefix)")
->setDescription('Generate Scheme class from Database table');
}
protected function execute(Input $input, Output $output)
{
$table = $input->getArgument('table');
$service = new DbToSchemeService();
$tables = [];
if ($table) {
$tables[] = $table;
} else {
// 获取所有表
$allTables = Db::getTables();
// 过滤掉忽略的表
$config = Config::get('scheme.ignore_tables', []);
$prefix = Config::get('database.connections.mysql.prefix');
foreach ($allTables as $t) {
// 如果有前缀,去除前缀后再判断
$shortName = $t;
if ($prefix && str_starts_with($t, $prefix)) {
$shortName = substr($t, strlen($prefix));
}
if (!in_array($shortName, $config) && !in_array($t, $config)) {
$tables[] = $shortName;
}
}
}
foreach ($tables as $t) {
$output->writeln("Processing table: $t");
try {
$code = $service->generate($t);
// 提取类名以确定文件名
if (preg_match('/class\s+(\w+)/', $code, $matches)) {
$className = $matches[1];
$path = app()->getAppPath() . 'admin/scheme/' . $className . '.php';
// 确保目录存在
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0755, true);
}
file_put_contents($path, $code);
$output->writeln("Generated: $path");
}
} catch (\Exception $e) {
$output->writeln("Error processing $t: " . $e->getMessage() . "");
}
}
}
}