19
This commit is contained in:
+132
-6
@@ -10,6 +10,9 @@ use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use support\think\Db;
|
||||
use app\model\User as UserModel;
|
||||
use \think\db\PDOConnection;
|
||||
use think\db\exception\PDOException;
|
||||
use think\db\exception\InvalidArgumentException;
|
||||
|
||||
|
||||
class Database extends Command
|
||||
@@ -22,10 +25,12 @@ class Database extends Command
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->addOption('action','a', InputArgument::OPTIONAL, '要做什么操作');
|
||||
$this->addOption('table','t', InputArgument::OPTIONAL, '表名');
|
||||
$this->addOption('domain','ym', InputArgument::OPTIONAL, 'domain');
|
||||
$this->addOption('robot_id','rid', InputArgument::OPTIONAL, 'robot_id');
|
||||
$this->addOption('action','a', InputOption::VALUE_OPTIONAL, '要做什么操作');
|
||||
$this->addOption('table','t', InputOption::VALUE_OPTIONAL, '表名');
|
||||
$this->addOption('domain','ym', InputOption::VALUE_OPTIONAL, 'domain');
|
||||
$this->addOption('connection','c', InputOption::VALUE_OPTIONAL, '数据库链接名,默认mysql','mysql');
|
||||
$this->addOption('dir','d', InputOption::VALUE_OPTIONAL, '缓存目录');
|
||||
$this->addOption('robot_id','rid', InputOption::VALUE_OPTIONAL, 'robot_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,12 +41,32 @@ class Database extends Command
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$action = $input->getOption('action');
|
||||
if($action == 'prototype'){
|
||||
return $this->prototype($input, $output);
|
||||
if(method_exists($this, $action)){
|
||||
return $this->$action($input, $output);
|
||||
}
|
||||
cp('操作不存在:'.$action);
|
||||
return 0;
|
||||
}
|
||||
function optimize_schema(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$table = $input->getOption('table');
|
||||
try {
|
||||
if ($table) {
|
||||
$this->cacheTable($table, $input->getOption('connection'));
|
||||
} else {
|
||||
$dirs = ((array) $input->getOption('dir')) ?: $this->getDefaultDirs();
|
||||
foreach ($dirs as $dir) {
|
||||
$this->cacheModel($dir);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$output->write($e->getMessage());
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$output->write('Succeed!');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
function prototype(InputInterface $input, OutputInterface $output){
|
||||
$table = $input->getOption('table');
|
||||
// 获取表前缀并构建完整表名
|
||||
@@ -132,4 +157,105 @@ class Database extends Command
|
||||
// 默认返回混合类型
|
||||
return 'mixed';
|
||||
}
|
||||
|
||||
|
||||
protected function buildModelSchema(string $class): void
|
||||
{
|
||||
$reflect = new \ReflectionClass($class);
|
||||
if ($reflect->isAbstract() || ! $reflect->isSubclassOf('\think\Model')) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
/** @var \think\Model $model */
|
||||
$model = new $class;
|
||||
$connection = $model->db()->getConnection();
|
||||
if ($connection instanceof PDOConnection) {
|
||||
$table = $model->getTable();
|
||||
//预读字段信息
|
||||
$connection->getSchemaInfo($table, true);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildDataBaseSchema(PDOConnection $connection, array $tables, string $dbName): void
|
||||
{
|
||||
foreach ($tables as $table) {
|
||||
//预读字段信息
|
||||
$connection->getSchemaInfo("{$dbName}.{$table}", true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存表
|
||||
*/
|
||||
private function cacheTable(string $table, ?string $connectionName = null): void
|
||||
{
|
||||
$connection = Db::connect($connectionName);
|
||||
if (! $connection instanceof PDOConnection) {
|
||||
throw new Exception('only PDO connection support schema cache!');
|
||||
}
|
||||
|
||||
if (str_contains($table, '.')) {
|
||||
[$dbName, $table] = explode('.', $table);
|
||||
} else {
|
||||
$dbName = $connection->getConfig('database');
|
||||
}
|
||||
|
||||
if ($table == '*') {
|
||||
$table = $connection->getTables($dbName);
|
||||
}
|
||||
|
||||
$this->buildDataBaseSchema($connection, (array) $table, $dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存模型
|
||||
*/
|
||||
private function cacheModel(?string $dir = null): void
|
||||
{
|
||||
if ($dir) {
|
||||
$modelDir = app_path('model') . $dir . DIRECTORY_SEPARATOR;
|
||||
$namespace = 'app\\' . $dir;
|
||||
} else {
|
||||
$modelDir = app_path('model').DIRECTORY_SEPARATOR;
|
||||
$namespace = 'app';
|
||||
}
|
||||
if (! is_dir($modelDir)) {
|
||||
throw new InvalidArgumentException("{$modelDir} directory does not exist");
|
||||
}
|
||||
|
||||
/** @var \SplFileInfo[] $iterator */
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($modelDir, \RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
|
||||
foreach ($iterator as $fileInfo) {
|
||||
$relativePath = substr($fileInfo->getRealPath(), strlen($modelDir));
|
||||
if (! str_ends_with($relativePath, '.php')) {
|
||||
continue;
|
||||
}
|
||||
// 去除 .php
|
||||
$relativePath = substr($relativePath, 0, -4);
|
||||
|
||||
$class = '\\' . $namespace . '\\model\\' . str_replace('/', '\\', $relativePath);
|
||||
if (! class_exists($class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->buildModelSchema($class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认目录名
|
||||
* @return array<int, ?string>
|
||||
*/
|
||||
private function getDefaultDirs(): array
|
||||
{
|
||||
// 包含默认的模型目录
|
||||
$dirs = [null];
|
||||
return $dirs;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user