feat: 同步下新环境
This commit is contained in:
@@ -10,18 +10,22 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\console\command\optimize;
|
||||
|
||||
use think\App;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\Db;
|
||||
use think\facade\App;
|
||||
|
||||
class Schema extends Command
|
||||
{
|
||||
/** @var Output */
|
||||
protected $output;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('optimize:schema')
|
||||
->addOption('config', null, Option::VALUE_REQUIRED, 'db config .')
|
||||
->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
|
||||
->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
|
||||
->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
|
||||
@@ -30,58 +34,56 @@ class Schema extends Command
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
if (!is_dir(App::getRuntimePath() . 'schema')) {
|
||||
@mkdir(App::getRuntimePath() . 'schema', 0755, true);
|
||||
if (!is_dir(RUNTIME_PATH . 'schema')) {
|
||||
@mkdir(RUNTIME_PATH . 'schema', 0755, true);
|
||||
}
|
||||
$config = [];
|
||||
if ($input->hasOption('config')) {
|
||||
$config = $input->getOption('config');
|
||||
}
|
||||
|
||||
if ($input->hasOption('module')) {
|
||||
$module = $input->getOption('module');
|
||||
// 读取模型
|
||||
$path = App::getAppPath() . $module . DIRECTORY_SEPARATOR . 'model';
|
||||
$list = is_dir($path) ? scandir($path) : [];
|
||||
$namespace = App::getNamespace();
|
||||
|
||||
$path = APP_PATH . $module . DS . 'model';
|
||||
$list = is_dir($path) ? scandir($path) : [];
|
||||
$app = App::$namespace;
|
||||
foreach ($list as $file) {
|
||||
if (0 === strpos($file, '.')) {
|
||||
continue;
|
||||
}
|
||||
$class = '\\' . $namespace . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
|
||||
$class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
|
||||
$this->buildModelSchema($class);
|
||||
}
|
||||
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
return;
|
||||
} elseif ($input->hasOption('table')) {
|
||||
$table = $input->getOption('table');
|
||||
if (false === strpos($table, '.')) {
|
||||
$dbName = Db::getConfig('database');
|
||||
if (!strpos($table, '.')) {
|
||||
$dbName = Db::connect($config)->getConfig('database');
|
||||
}
|
||||
|
||||
$tables[] = $table;
|
||||
} elseif ($input->hasOption('db')) {
|
||||
$dbName = $input->getOption('db');
|
||||
$tables = Db::getConnection()->getTables($dbName);
|
||||
} elseif (!\think\facade\Config::get('app_multi_module')) {
|
||||
$namespace = App::getNamespace();
|
||||
$path = App::getAppPath() . 'model';
|
||||
$list = is_dir($path) ? scandir($path) : [];
|
||||
|
||||
$tables = Db::connect($config)->getTables($dbName);
|
||||
} elseif (!\think\Config::get('app_multi_module')) {
|
||||
$app = App::$namespace;
|
||||
$path = APP_PATH . 'model';
|
||||
$list = is_dir($path) ? scandir($path) : [];
|
||||
foreach ($list as $file) {
|
||||
if (0 === strpos($file, '.')) {
|
||||
continue;
|
||||
}
|
||||
$class = '\\' . $namespace . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
|
||||
$class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
|
||||
$this->buildModelSchema($class);
|
||||
}
|
||||
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
return;
|
||||
} else {
|
||||
$tables = Db::getConnection()->getTables();
|
||||
$tables = Db::connect($config)->getTables();
|
||||
}
|
||||
|
||||
$db = isset($dbName) ? $dbName . '.' : '';
|
||||
$this->buildDataBaseSchema($tables, $db);
|
||||
$this->buildDataBaseSchema($tables, $db, $config);
|
||||
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
}
|
||||
@@ -95,24 +97,22 @@ class Schema extends Command
|
||||
$content = '<?php ' . PHP_EOL . 'return ';
|
||||
$info = $class::getConnection()->getFields($table);
|
||||
$content .= var_export($info, true) . ';';
|
||||
|
||||
file_put_contents(App::getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR . $dbName . '.' . $table . '.php', $content);
|
||||
file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $content);
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildDataBaseSchema($tables, $db)
|
||||
protected function buildDataBaseSchema($tables, $db, $config)
|
||||
{
|
||||
if ('' == $db) {
|
||||
$dbName = Db::getConfig('database') . '.';
|
||||
$dbName = Db::connect($config)->getConfig('database') . '.';
|
||||
} else {
|
||||
$dbName = $db;
|
||||
}
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$content = '<?php ' . PHP_EOL . 'return ';
|
||||
$info = Db::getConnection()->getFields($db . $table);
|
||||
$info = Db::connect($config)->getFields($db . $table);
|
||||
$content .= var_export($info, true) . ';';
|
||||
file_put_contents(App::getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR . $dbName . $table . '.php', $content);
|
||||
file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user