qqq
This commit is contained in:
@@ -253,7 +253,7 @@ class Gii
|
|||||||
}
|
}
|
||||||
foreach ($tables as $key => $val) {
|
foreach ($tables as $key => $val) {
|
||||||
if (empty($val)) continue;
|
if (empty($val)) continue;
|
||||||
$_tmp = Db::findAllBySql('SHOW FULL FIELDS FROM `' . $this->db->database . '`.' . $val, [], $this->db);
|
$_tmp = Db::connect($this->db)->query('SHOW FULL FIELDS FROM `' . $this->db->database . '`.' . $val, []);
|
||||||
if (empty($_tmp)) {
|
if (empty($_tmp)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
+268
-268
@@ -17,62 +17,62 @@ use function logger;
|
|||||||
class GiiModel extends GiiBase
|
class GiiModel extends GiiBase
|
||||||
{
|
{
|
||||||
|
|
||||||
public ?string $classFileName;
|
public ?string $classFileName;
|
||||||
public ?array $visible;
|
public ?array $visible;
|
||||||
public ?array $res;
|
public ?array $res;
|
||||||
public ?array $fields;
|
public ?array $fields;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GiiModel constructor.
|
* GiiModel constructor.
|
||||||
* @param string $classFileName
|
* @param string $classFileName
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @param array $visible
|
* @param array $visible
|
||||||
* @param array $res
|
* @param array $res
|
||||||
* @param array $fields
|
* @param array $fields
|
||||||
*/
|
*/
|
||||||
public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields)
|
public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields)
|
||||||
{
|
{
|
||||||
$this->classFileName = $classFileName;
|
$this->classFileName = $classFileName;
|
||||||
$this->tableName = $tableName;
|
$this->tableName = $tableName;
|
||||||
$this->visible = $visible;
|
$this->visible = $visible;
|
||||||
$this->res = $res;
|
$this->res = $res;
|
||||||
$this->fields = $fields;
|
$this->fields = $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function generate(): string
|
public function generate(): string
|
||||||
{
|
{
|
||||||
$class = '';
|
$class = '';
|
||||||
$modelPath = $this->getModelPath();
|
$modelPath = $this->getModelPath();
|
||||||
$managerName = $this->classFileName;
|
$managerName = $this->classFileName;
|
||||||
|
|
||||||
$namespace = rtrim($modelPath['namespace'], '\\');
|
$namespace = rtrim($modelPath['namespace'], '\\');
|
||||||
|
|
||||||
if (file_exists($modelPath['path'] . '/' . $managerName . '.php')) {
|
if (file_exists($modelPath['path'] . '/' . $managerName . '.php')) {
|
||||||
try {
|
try {
|
||||||
$className = str_replace('\\\\', '\\', "{$modelPath['namespace']}\\{$managerName}");
|
$className = str_replace('\\\\', '\\', "{$modelPath['namespace']}\\{$managerName}");
|
||||||
|
|
||||||
$class = Kiri::getDi()->getReflectionClass($className);
|
$class = Kiri::getDi()->getReflectionClass($className);
|
||||||
|
|
||||||
$html = '<?php
|
$html = '<?php
|
||||||
namespace ' . $namespace . ';
|
namespace ' . $namespace . ';
|
||||||
|
|
||||||
';
|
';
|
||||||
$imports = $this->getImports($modelPath['path'] . '/' . $managerName . '.php', $class);
|
$imports = $this->getImports($modelPath['path'] . '/' . $managerName . '.php', $class);
|
||||||
if (!empty($imports)) {
|
if (!empty($imports)) {
|
||||||
$html .= $imports . PHP_EOL;
|
$html .= $imports . PHP_EOL;
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
error($e);
|
error($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (empty($html)) {
|
if (empty($html)) {
|
||||||
$html = '<?php
|
$html = '<?php
|
||||||
namespace ' . $namespace . ';
|
namespace ' . $namespace . ';
|
||||||
|
|
||||||
|
|
||||||
@@ -81,16 +81,16 @@ use Kiri\Core\Json;
|
|||||||
use Database\Connection;
|
use Database\Connection;
|
||||||
use Database\Model;
|
use Database\Model;
|
||||||
' . PHP_EOL;
|
' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
$createSql = $this->setCreateSql($this->tableName);
|
$createSql = $this->setCreateSql($this->tableName);
|
||||||
|
|
||||||
if (!str_contains($html, $createSql)) {
|
if (!str_contains($html, $createSql)) {
|
||||||
$html .= '
|
$html .= '
|
||||||
' . $this->setCreateSql($this->tableName);
|
' . $this->setCreateSql($this->tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
$html .= '
|
$html .= '
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ' . $managerName . '
|
* Class ' . $managerName . '
|
||||||
@@ -102,54 +102,54 @@ class ' . $managerName . ' extends Model
|
|||||||
|
|
||||||
';
|
';
|
||||||
|
|
||||||
if (!empty($class)) {
|
if (!empty($class)) {
|
||||||
$html .= $this->getClassProperty($class);
|
$html .= $this->getClassProperty($class);
|
||||||
}
|
}
|
||||||
|
|
||||||
$primary = $this->createPrimary($this->fields);
|
$primary = $this->createPrimary($this->fields);
|
||||||
if (!empty($primary)) {
|
if (!empty($primary)) {
|
||||||
$html .= $primary . "\n";
|
$html .= $primary . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$html .= $this->createTableName($this->tableName) . "\n";
|
$html .= $this->createTableName($this->tableName) . "\n";
|
||||||
|
|
||||||
$html .= $this->createDatabaseSource();
|
$html .= $this->createDatabaseSource();
|
||||||
|
|
||||||
$html .= $this->createRules($this->fields);
|
$html .= $this->createRules($this->fields);
|
||||||
|
|
||||||
if (is_object($class)) {
|
if (is_object($class)) {
|
||||||
$html .= $this->getClassMethods($class, ['rules', 'tableName', 'attributes', 'getDb']);
|
$html .= $this->getClassMethods($class, ['rules', 'tableName', 'attributes', 'getDb']);
|
||||||
} else {
|
} else {
|
||||||
$other = $this->generate_json_function($html, $this->fields);
|
$other = $this->generate_json_function($html, $this->fields);
|
||||||
if (!empty($other)) {
|
if (!empty($other)) {
|
||||||
$html .= implode($other);
|
$html .= implode($other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$html .= '
|
$html .= '
|
||||||
}';
|
}';
|
||||||
|
|
||||||
$file = rtrim($modelPath['path'], '/') . '/' . $managerName . '.php';
|
$file = rtrim($modelPath['path'], '/') . '/' . $managerName . '.php';
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
unlink($file);
|
unlink($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
Kiri::writeFile($file, $html);
|
Kiri::writeFile($file, $html);
|
||||||
return $managerName . '.php';
|
return $managerName . '.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $html
|
* @param $html
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function generate_json_function($html, $fields): array
|
private function generate_json_function($html, $fields): array
|
||||||
{
|
{
|
||||||
$strings = [];
|
$strings = [];
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
if ($field['Type'] === 'json') {
|
if ($field['Type'] === 'json') {
|
||||||
$function = '
|
$function = '
|
||||||
/**
|
/**
|
||||||
* @param array|null $value
|
* @param array|null $value
|
||||||
* @return int|bool|string
|
* @return int|bool|string
|
||||||
@@ -164,7 +164,7 @@ class ' . $managerName . ' extends Model
|
|||||||
}
|
}
|
||||||
';
|
';
|
||||||
|
|
||||||
$get_function = '
|
$get_function = '
|
||||||
/**
|
/**
|
||||||
* @param string|null $value
|
* @param string|null $value
|
||||||
* @return array|null|bool
|
* @return array|null|bool
|
||||||
@@ -178,87 +178,87 @@ class ' . $managerName . ' extends Model
|
|||||||
}
|
}
|
||||||
';
|
';
|
||||||
|
|
||||||
if (!str_contains($html, 'set' . ucfirst($field['Field']) . 'Attribute')) {
|
if (!str_contains($html, 'set' . ucfirst($field['Field']) . 'Attribute')) {
|
||||||
$strings[] = $function;
|
$strings[] = $function;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!str_contains($html, 'get' . ucfirst($field['Field']) . 'Attribute')) {
|
if (!str_contains($html, 'get' . ucfirst($field['Field']) . 'Attribute')) {
|
||||||
$strings[] = $get_function;
|
$strings[] = $get_function;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $strings;
|
return $strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $field
|
* @param $field
|
||||||
* @return string
|
* @return string
|
||||||
* 创建表名称
|
* 创建表名称
|
||||||
*/
|
*/
|
||||||
private function createTableName($field): string
|
private function createTableName($field): string
|
||||||
{
|
{
|
||||||
|
|
||||||
$prefixed = $this->db->tablePrefix;
|
$prefixed = $this->db->tablePrefix;
|
||||||
if (!empty($prefixed)) {
|
if (!empty($prefixed)) {
|
||||||
if (str_starts_with($field, $prefixed)) {
|
if (str_starts_with($field, $prefixed)) {
|
||||||
$field = str_replace($prefixed, '', $field);
|
$field = str_replace($prefixed, '', $field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return '
|
return '
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
protected string $table = \'' . $field . '\';
|
protected string $table = \'' . $field . '\';
|
||||||
';
|
';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @return string
|
* @return string
|
||||||
* 创建效验规则
|
* 创建效验规则
|
||||||
*/
|
*/
|
||||||
private function createRules($fields): string
|
private function createRules($fields): string
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($fields as $key => $val) {
|
foreach ($fields as $key => $val) {
|
||||||
if ($val['Extra'] == 'auto_increment') continue;
|
if ($val['Extra'] == 'auto_increment') continue;
|
||||||
$type = preg_replace('/\(.*?\)|\s+\w+/', '', $val['Type']);
|
$type = preg_replace('/\(.*?\)|\s+\w+/', '', $val['Type']);
|
||||||
foreach ($this->type as $_key => $_val) {
|
foreach ($this->type as $_key => $_val) {
|
||||||
if (in_array($type, $_val)) {
|
if (in_array($type, $_val)) {
|
||||||
$type = lcfirst(str_replace('get', '', $_key));
|
$type = lcfirst(str_replace('get', '', $_key));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$data[$type][] = $val;
|
$data[$type][] = $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
$_field_one = '';
|
$_field_one = '';
|
||||||
$required = $this->getRequired($fields);
|
$required = $this->getRequired($fields);
|
||||||
if (!empty($required)) {
|
if (!empty($required)) {
|
||||||
$_field_one .= $required;
|
$_field_one .= $required;
|
||||||
}
|
}
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
$field = '[\'' . implode('\', \'', array_column($val, 'Field')) . '\']';
|
$field = '[\'' . implode('\', \'', array_column($val, 'Field')) . '\']';
|
||||||
if (count($val) == 1) {
|
if (count($val) == 1) {
|
||||||
$field = '[\'' . current($val)['Field'] . '\']';
|
$field = '[\'' . current($val)['Field'] . '\']';
|
||||||
}
|
}
|
||||||
$_field_one .= '
|
$_field_one .= '
|
||||||
[' . $field . ', \'' . $key . '\'],';
|
[' . $field . ', \'' . $key . '\'],';
|
||||||
}
|
}
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
$length = $this->getLength($val);
|
$length = $this->getLength($val);
|
||||||
if (!empty($length)) {
|
if (!empty($length)) {
|
||||||
$_field_one .= $length . ',';
|
$_field_one .= $length . ',';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$required = $this->getUnique($fields);
|
$required = $this->getUnique($fields);
|
||||||
if (!empty($required)) {
|
if (!empty($required)) {
|
||||||
$_field_one .= $required;
|
$_field_one .= $required;
|
||||||
}
|
}
|
||||||
return '
|
return '
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -268,139 +268,139 @@ class ' . $managerName . ' extends Model
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
';
|
';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $val
|
* @param $val
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getLength($val): string
|
public function getLength($val): string
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($val as $key => $_val) {
|
foreach ($val as $key => $_val) {
|
||||||
$preg = preg_match('/(\w+)\((.*?)\)/', $_val['Type'], $results);
|
$preg = preg_match('/(\w+)\((.*?)\)/', $_val['Type'], $results);
|
||||||
if ($preg && isset($results[2])) {
|
if ($preg && isset($results[2])) {
|
||||||
$results[] = $_val['Field'];
|
$results[] = $_val['Field'];
|
||||||
|
|
||||||
$data[$results[2]][] = $results;
|
$data[$results[2]][] = $results;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty($data)) return '';
|
if (empty($data)) return '';
|
||||||
$string = [];
|
$string = [];
|
||||||
foreach ($data as $key => $_val) {
|
foreach ($data as $key => $_val) {
|
||||||
if (in_array($_val[0][1], $this->type['float'])) {
|
if (in_array($_val[0][1], $this->type['float'])) {
|
||||||
$e_x = explode(',', $key);
|
$e_x = explode(',', $key);
|
||||||
$key = '\'round\' => ' . $e_x[1] . ', \'maxLength\' => ' . ((int)$e_x[0] + 1);
|
$key = '\'round\' => ' . $e_x[1] . ', \'maxLength\' => ' . ((int)$e_x[0] + 1);
|
||||||
} else if (is_string($key) && str_contains($key, ',')) {
|
} else if (is_string($key) && str_contains($key, ',')) {
|
||||||
} else {
|
} else {
|
||||||
$key = '\'maxLength\' => ' . $key;
|
$key = '\'maxLength\' => ' . $key;
|
||||||
}
|
}
|
||||||
$string[] = '
|
$string[] = '
|
||||||
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . ($_val[0][1] == 'enum' ? '\'enum\' => [' . $key .']' : $key) . ']';
|
[[\'' . implode('\', \'', array_column($_val, 3)) . '\'], ' . ($_val[0][1] == 'enum' ? '\'enum\' => [' . $key . ']' : $key) . ']';
|
||||||
}
|
}
|
||||||
return implode(',', $string);
|
return implode(',', $string);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUnique($fields): string
|
public function getUnique($fields): string
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($fields as $_val) {
|
foreach ($fields as $_val) {
|
||||||
if ($_val['Extra'] == 'auto_increment') continue;
|
if ($_val['Extra'] == 'auto_increment') continue;
|
||||||
if (str_contains($_val['Type'], 'unique')) {
|
if (str_contains($_val['Type'], 'unique')) {
|
||||||
$data[] = $_val['Field'];
|
$data[] = $_val['Field'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return '
|
return '
|
||||||
[[\'' . implode('\', \'', $data) . '\'], \'unique\'],';
|
[[\'' . implode('\', \'', $data) . '\'], \'unique\'],';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $val
|
* @param $val
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getRequired($val): string
|
public function getRequired($val): string
|
||||||
{
|
{
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($val as $_key => $_val) {
|
foreach ($val as $_key => $_val) {
|
||||||
if ($_val['Extra'] == 'auto_increment') continue;
|
if ($_val['Extra'] == 'auto_increment') continue;
|
||||||
if ($_val['Key'] == 'PRI' || $_val['Key'] == 'UNI' || $this->checkIsRequired($_val) === 'true') {
|
if ($_val['Key'] == 'PRI' || $_val['Key'] == 'UNI' || $this->checkIsRequired($_val) === 'true') {
|
||||||
$data[] = $_val['Field'];
|
$data[] = $_val['Field'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return '
|
return '
|
||||||
[[\'' . implode('\', \'', $data) . '\'], \'required\'],';
|
[[\'' . implode('\', \'', $data) . '\'], \'required\'],';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用来生成文档的
|
* 用来生成文档的
|
||||||
* 格式
|
* 格式
|
||||||
* @param $fields
|
* @param $fields
|
||||||
* @return null|string
|
* @return null|string
|
||||||
* array(
|
* array(
|
||||||
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
||||||
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
||||||
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
||||||
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
||||||
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
||||||
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
private function createPrimary($fields): ?string
|
private function createPrimary($fields): ?string
|
||||||
{
|
{
|
||||||
$field = $this->getPrimaryKey($fields);
|
$field = $this->getPrimaryKey($fields);
|
||||||
if (empty($field)) {
|
if (empty($field)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return '
|
return '
|
||||||
/**
|
/**
|
||||||
* @var string|null
|
* @var string|null
|
||||||
*/
|
*/
|
||||||
protected ?string $primary = \'' . $field . '\';';
|
protected ?string $primary = \'' . $field . '\';';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function createDatabaseSource(): string
|
private function createDatabaseSource(): string
|
||||||
{
|
{
|
||||||
return '
|
return '
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected string $connection = \'' . $this->db->id . '\';
|
protected string $connection = \'' . $this->db->id . '\';
|
||||||
';
|
';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function setCreateSql($table): string
|
private function setCreateSql($table): string
|
||||||
{
|
{
|
||||||
if (isset(Gii::$createSqls[$table])) {
|
if (isset(Gii::$createSqls[$table])) {
|
||||||
return Gii::$createSqls[$table];
|
return Gii::$createSqls[$table];
|
||||||
}
|
}
|
||||||
|
|
||||||
$text = Db::showCreateSql($table, $this->db)['Create Table'] ?? '';
|
$text = Db::connect($this->db)->one('SHOW CREATE TABLE `'.$this->db->database. '`.`' . $table.'`')['Create Table'] ?? '';
|
||||||
|
|
||||||
$_tmp = [];
|
$_tmp = [];
|
||||||
foreach (explode(PHP_EOL, $text) as $val) {
|
foreach (explode(PHP_EOL, $text) as $val) {
|
||||||
$_tmp[] = '// ' . $val;
|
$_tmp[] = '// ' . $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Gii::$createSqls[$table] = implode(PHP_EOL, $_tmp);
|
return Gii::$createSqls[$table] = implode(PHP_EOL, $_tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user