mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-02 05:02:48 +08:00
改进数据表字段缓存生成 支持读取模块的模型生成
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\console\command\optimize;
|
||||
|
||||
use think\App;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
@@ -26,35 +27,78 @@ class Schema extends Command
|
||||
$this->setName('optimize:schema')
|
||||
->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
|
||||
->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
|
||||
->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
|
||||
->setDescription('Build database schema cache.');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
if ($input->hasOption('table')) {
|
||||
$tables[] = $input->getOption('table');
|
||||
} else {
|
||||
if ($input->hasOption('db')) {
|
||||
$dbName = $input->getOption('db');
|
||||
$tables = Db::getTables($dbName);
|
||||
} else {
|
||||
$tables = Db::getTables();
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_dir(RUNTIME_PATH . 'schema')) {
|
||||
@mkdir(RUNTIME_PATH . 'schema', 0755, true);
|
||||
}
|
||||
if ($input->hasOption('module')) {
|
||||
$module = $input->getOption('module');
|
||||
// 读取模型
|
||||
$list = scandir(APP_PATH . $module . DS . 'model');
|
||||
$app = App::$namespace;
|
||||
foreach ($list as $file) {
|
||||
if ('.' == $file || '..' == $file) {
|
||||
continue;
|
||||
}
|
||||
$class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
|
||||
$this->buildModelSchema($class);
|
||||
}
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
return;
|
||||
} else if ($input->hasOption('table')) {
|
||||
$table = $input->getOption('table');
|
||||
if (!strpos($table, '.')) {
|
||||
$dbName = Db::getConfig('database');
|
||||
}
|
||||
$tables[] = $table;
|
||||
} elseif ($input->hasOption('db')) {
|
||||
$dbName = $input->getOption('db');
|
||||
$tables = Db::getTables($dbName);
|
||||
} elseif (!\think\Config::get('app_multi_module')) {
|
||||
$app = App::$namespace;
|
||||
$list = scandir(APP_PATH . 'model');
|
||||
foreach ($list as $file) {
|
||||
if ('.' == $file || '..' == $file) {
|
||||
continue;
|
||||
}
|
||||
$class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
|
||||
$this->buildModelSchema($class);
|
||||
}
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
return;
|
||||
} else {
|
||||
$dbName = Db::getConfig('database');
|
||||
$tables = Db::getTables();
|
||||
}
|
||||
|
||||
$db = isset($dbName) ? $dbName . '.' : '';
|
||||
$this->buildDataBaseSchema($tables, $db);
|
||||
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
}
|
||||
|
||||
protected function buildModelSchema($class)
|
||||
{
|
||||
$table = $class::getTable();
|
||||
$dbName = $class::getConfig('database');
|
||||
$content = '<?php ' . PHP_EOL . 'return ';
|
||||
$info = $class::getConnection()->getFields($table);
|
||||
$content .= var_export($info, true) . ';';
|
||||
file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $content);
|
||||
}
|
||||
|
||||
protected function buildDataBaseSchema($tables, $db)
|
||||
{
|
||||
foreach ($tables as $table) {
|
||||
$content = '<?php ' . PHP_EOL . 'return ';
|
||||
$info = Db::getFields($db . $table);
|
||||
$content .= var_export($info, true) . ';';
|
||||
file_put_contents(RUNTIME_PATH . 'schema' . DS . $db . $table . EXT, $content);
|
||||
}
|
||||
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1341,6 +1341,9 @@ class Query
|
||||
}
|
||||
|
||||
list($guid) = explode(' ', $tableName);
|
||||
if (!strpos($guid, '.')) {
|
||||
$guid = $this->getConfig('database') . '.' . $guid;
|
||||
}
|
||||
if (!isset(self::$info[$guid])) {
|
||||
// 读取缓存
|
||||
if (is_file(RUNTIME_PATH . 'schema/' . $guid . '.php')) {
|
||||
|
||||
Reference in New Issue
Block a user