mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
增加 optimize:schema 命令 用于生成 数据表字段信息缓存
This commit is contained in:
@@ -147,7 +147,7 @@ if (!function_exists('widget')) {
|
||||
/**
|
||||
* 渲染输出Widget
|
||||
* @param string $name Widget名称
|
||||
* @param array $data 传人的参数
|
||||
* @param array $data 传入的参数
|
||||
* @return mixed
|
||||
*/
|
||||
function widget($name, $data = [])
|
||||
|
||||
@@ -44,6 +44,7 @@ class Console
|
||||
"think\\console\\command\\optimize\\Autoload",
|
||||
"think\\console\\command\\optimize\\Config",
|
||||
"think\\console\\command\\optimize\\Route",
|
||||
"think\\console\\command\\optimize\\Schema",
|
||||
];
|
||||
|
||||
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
|
||||
@@ -130,7 +131,7 @@ class Console
|
||||
|
||||
$exitCode = $e->getCode();
|
||||
if (is_numeric($exitCode)) {
|
||||
$exitCode = (int)$exitCode;
|
||||
$exitCode = (int) $exitCode;
|
||||
if (0 === $exitCode) {
|
||||
$exitCode = 1;
|
||||
}
|
||||
@@ -221,7 +222,7 @@ class Console
|
||||
*/
|
||||
public function setCatchExceptions($boolean)
|
||||
{
|
||||
$this->catchExceptions = (bool)$boolean;
|
||||
$this->catchExceptions = (bool) $boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,7 +232,7 @@ class Console
|
||||
*/
|
||||
public function setAutoExit($boolean)
|
||||
{
|
||||
$this->autoExit = (bool)$boolean;
|
||||
$this->autoExit = (bool) $boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,7 +400,7 @@ class Console
|
||||
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
|
||||
return preg_quote($matches[1]) . '[^:]*';
|
||||
}, $namespace);
|
||||
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
|
||||
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
|
||||
|
||||
if (empty($namespaces)) {
|
||||
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
|
||||
@@ -437,7 +438,7 @@ class Console
|
||||
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
|
||||
return preg_quote($matches[1]) . '[^:]*';
|
||||
}, $name);
|
||||
$commands = preg_grep('{^' . $expr . '}', $allCommands);
|
||||
$commands = preg_grep('{^' . $expr . '}', $allCommands);
|
||||
|
||||
if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
|
||||
if (false !== $pos = strrpos($name, ':')) {
|
||||
|
||||
53
library/think/console/command/optimize/Schema.php
Normal file
53
library/think/console/command/optimize/Schema.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\console\command\optimize;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\Db;
|
||||
|
||||
class Schema extends Command
|
||||
{
|
||||
/** @var Output */
|
||||
protected $output;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('optimize:schema')
|
||||
->addOption('table', null, Option::VALUE_REQUIRED, 'Build table schema cache .')
|
||||
->setDescription('Build database schema cache.');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
if ($input->hasOption('table')) {
|
||||
$tables[] = $input->getOption('table');
|
||||
} else {
|
||||
$tables = Db::getTables();
|
||||
}
|
||||
|
||||
if (!is_dir(RUNTIME_PATH . 'schema')) {
|
||||
@mkdir(RUNTIME_PATH . 'schema', 0755, true);
|
||||
}
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$content = '<?php ' . PHP_EOL . 'return ';
|
||||
$info = Db::getFields($table);
|
||||
$content .= var_export($info, true) . ';';
|
||||
file_put_contents(RUNTIME_PATH . 'schema' . DS . $table . EXT, $content);
|
||||
}
|
||||
|
||||
$output->writeln('<info>Succeed!</info>');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1375,7 +1375,12 @@ class Query
|
||||
|
||||
list($guid) = explode(' ', $tableName);
|
||||
if (!isset(self::$info[$guid])) {
|
||||
$info = $this->connection->getFields($tableName);
|
||||
// 读取缓存
|
||||
if (is_file(RUNTIME_PATH . 'schema/' . $guid . '.php')) {
|
||||
$info = include RUNTIME_PATH . 'schema/' . $guid . '.php';
|
||||
} else {
|
||||
$info = $this->connection->getFields($guid);
|
||||
}
|
||||
$fields = array_keys($info);
|
||||
$bind = $type = [];
|
||||
foreach ($info as $key => $val) {
|
||||
|
||||
@@ -86,6 +86,7 @@ class Mysql extends Connection
|
||||
*/
|
||||
public function getTables($dbName = '')
|
||||
{
|
||||
$this->initConnect(true);
|
||||
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
|
||||
// 调试开始
|
||||
$this->debug(true);
|
||||
|
||||
Reference in New Issue
Block a user