This commit is contained in:
2020-12-17 14:09:14 +08:00
parent 672a719dbd
commit 36c1d0502a
151 changed files with 1937 additions and 2848 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ class Command extends \Console\Command
* @return array
* @throws Exception
*/
public function onHandler(Input $dtl)
public function onHandler(Input $dtl): array
{
/** @var Gii $gii */
$gii = Snowflake::app()->get('gii');
+18 -20
View File
@@ -12,6 +12,8 @@ namespace Gii;
use Database\Connection;
use Database\Db;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Input;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
@@ -52,7 +54,7 @@ class Gii
* @throws ConfigException
* @throws Exception
*/
public function run(?Connection $db, $input)
public function run(?Connection $db, $input): array
{
$this->input = $input;
if (!empty($db)) $this->db = $db;
@@ -90,8 +92,9 @@ class Gii
* @return array
* @throws ComponentException
* @throws ConfigException
* @throws Exception
*/
private function getModel($make, $input)
private function getModel($make, $input): array
{
if (!$this->db) {
$db = $this->input->get('databases', 'db');
@@ -120,7 +123,7 @@ class Gii
*
* @throws Exception
*/
private function getTable($controller, $model)
private function getTable($controller, $model): array
{
$tables = $this->getFields($this->getTables());
if (empty($tables)) {
@@ -145,7 +148,7 @@ class Gii
* @return string
* @throws Exception
*/
private function generateModel(array $data)
private function generateModel(array $data): string
{
$controller = new GiiModel($data['classFileName'], $data['tableName'], $data['visible'], $data['res'], $data['fields']);
$controller->setConnection($this->db);
@@ -163,7 +166,7 @@ class Gii
* @return string
* @throws Exception
*/
private function generateController(array $data)
private function generateController(array $data): string
{
$controller = new GiiController($data['classFileName'], $data['fields']);
$controller->setConnection($this->db);
@@ -177,10 +180,10 @@ class Gii
}
/**
* @return array|null
* @return array|string|null
* @throws Exception
*/
private function getTables()
private function getTables(): array|string|null
{
if (empty($this->tableName)) {
return $this->showAll();
@@ -199,7 +202,7 @@ class Gii
* @return array
* @throws Exception
*/
private function showAll()
private function showAll(): array
{
$res = [];
$_tables = Db::findAllBySql('show tables', [], $this->db);
@@ -214,10 +217,10 @@ class Gii
/**
* @param $table
* @return bool|int
* @return bool|int|null
* @throws Exception
*/
private function getIndex($table)
private function getIndex($table): bool|int|null
{
$data = Db::findAllBySql('SHOW INDEX FROM ' . $table, [], $this->db);
@@ -230,7 +233,7 @@ class Gii
* @return array
* @throws
*/
private function getFields($tables)
private function getFields($tables): array
{
$res = [];
if (!is_array($tables)) {
@@ -254,7 +257,7 @@ class Gii
* @return array
* @throws Exception
*/
public function createModelFile($tableName, $tables)
public function createModelFile($tableName, $tables): array
{
$res = $visible = $fields = $keys = [];
foreach ($tables as $_key => $_val) {
@@ -289,7 +292,7 @@ class Gii
* @return string
* 创建变量注释
*/
private function createVisible($field)
private function createVisible($field): string
{
return '
* @property $' . $field;
@@ -301,7 +304,7 @@ class Gii
* @return string
* 暂时不知道干嘛用的
*/
private function createSetFunc($field, $comment)
#[Pure] private function createSetFunc($field, $comment): string
{
return '
' . str_pad('\'' . $field . '\'', 20, ' ', STR_PAD_RIGHT) . '=> \'' . (empty($comment) ? ucfirst($field) : $comment) . '\',';
@@ -312,18 +315,13 @@ class Gii
* @return string
* 构建类名称
*/
private function getClassName($tableName)
private function getClassName($tableName): string
{
$res = [];
foreach (explode('_', $tableName) as $n => $val) {
$res[] = ucfirst($val);
}
$name = ucfirst(rtrim($this->db->tablePrefix, '_'));
return implode('', $res);
return str_replace($name, '', implode('', $res)) . 'Comply';
}
}
+24 -22
View File
@@ -6,6 +6,9 @@ namespace Gii;
use Database\Connection;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use ReflectionClass;
use Snowflake\Abstracts\Input;
/**
@@ -15,22 +18,22 @@ use Snowflake\Abstracts\Input;
abstract class GiiBase
{
public $fileList = [];
public array $fileList = [];
/** @var Input */
protected $input;
protected Input $input;
public $modelPath = APP_PATH . '/app/Models/';
public $modelNamespace = 'App\Models\\';
public string $modelPath = APP_PATH . '/app/Models/';
public string $modelNamespace = 'App\Models\\';
public $controllerPath = APP_PATH . '/app/Http/Controllers/';
public $controllerNamespace = 'App\\Http\\Controllers\\';
public string $controllerPath = APP_PATH . '/app/Http/Controllers/';
public string $controllerNamespace = 'App\\Http\\Controllers\\';
public $module = null;
public ?string $module = null;
public $rules = [];
public $type = [
public array $rules = [];
public array $type = [
'int' => ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'],
'string' => ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext', 'enum'],
'date' => ['date'],
@@ -40,10 +43,9 @@ abstract class GiiBase
'timestamp' => ['timestamp'],
'float' => ['float', 'double', 'decimal',],
];
public $tableName = NULL;
public ?string $tableName = NULL;
/** @var Connection */
public $db;
public ?Connection $db = null;
/**
* @param string $modelPath
@@ -96,12 +98,12 @@ abstract class GiiBase
/**
* @param \ReflectionClass $object
* @param ReflectionClass $object
* @param $className
*
* @return string
*/
public function getUseContent($object, $className)
public function getUseContent(ReflectionClass $object, $className): string
{
if (empty($object)) {
return '';
@@ -126,10 +128,10 @@ abstract class GiiBase
/**
* @param $fields
* @return mixed|null
* @return mixed 返回表主键
* 返回表主键
*/
public function getPrimaryKey($fields)
public function getPrimaryKey($fields): mixed
{
$condition = ['PRI', 'UNI'];
foreach ($fields as $field) {
@@ -147,7 +149,7 @@ abstract class GiiBase
* @param $className
* @return string
*/
private function getFilePath($className)
private function getFilePath($className): string
{
if (strpos($className, '\\')) {
$className = str_replace('\\', '/', $className);
@@ -160,13 +162,13 @@ abstract class GiiBase
}
/**
* @param \ReflectionClass $object
* @param ReflectionClass $object
* @param $className
* @param $method
* @return string
* @throws \Exception
*/
public function getFuncLineContent($object, $className, $method)
public function getFuncLineContent(ReflectionClass $object, $className, $method): string
{
$fun = $object->getMethod($method);
@@ -180,7 +182,7 @@ abstract class GiiBase
/**
* @return array
*/
protected function getModelPath()
protected function getModelPath(): array
{
$dbName = $this->db->id;
if (empty($dbName) || $dbName == 'db') {
@@ -217,7 +219,7 @@ abstract class GiiBase
* @param $val
* @return string
*/
protected function checkIsRequired($val)
#[Pure] protected function checkIsRequired($val): string
{
return strtolower($val['Null']) == 'no' && $val['Default'] === NULL ? 'true' : 'false';
}
@@ -225,7 +227,7 @@ abstract class GiiBase
/**
* @return array
*/
public function getFileLists()
public function getFileLists(): array
{
return $this->fileList;
}
+26 -16
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Gii;
use Exception;
use Snowflake\Snowflake;
/**
@@ -13,9 +14,9 @@ use Snowflake\Snowflake;
class GiiController extends GiiBase
{
public $className;
public string $className = '';
public $fields;
public array $fields = [];
public function __construct($className, $fields)
{
@@ -26,9 +27,9 @@ class GiiController extends GiiBase
/**
* @return string
* @throws \Exception
* @throws Exception
*/
public function generate()
public function generate(): string
{
$path = $this->getControllerPath();
$modelPath = $this->getModelPath();
@@ -70,7 +71,7 @@ use {$model_namespace}\\{$managerName};
}
$historyModel = "use {$model_namespace}\\{$managerName};";
if (strpos($html, $historyModel) === false) {
if (!str_contains($html, $historyModel)) {
$html .= $historyModel;
}
@@ -125,7 +126,7 @@ class {$controllerName}Controller extends Controller
/**
* @return array
*/
private function getControllerPath()
private function getControllerPath(): array
{
$dbName = $this->db->id;
if (empty($dbName) || $dbName == 'db') {
@@ -159,7 +160,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 新增
*/
public function controllerMethodAdd($fields, $className, $object = NULL)
public function controllerMethodAdd($fields, $className, $object = NULL): string
{
return '
/**
@@ -184,7 +185,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 通用
*/
public function controllerMethodLoadParam($fields, $className, $object = NULL)
public function controllerMethodLoadParam($fields, $className, $object = NULL): string
{
return '
/**
@@ -205,7 +206,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 构建更新
*/
public function controllerMethodUpdate($fields, $className, $object = NULL)
public function controllerMethodUpdate($fields, $className, $object = NULL): string
{
return '
/**
@@ -234,7 +235,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 构建更新
*/
public function controllerMethodBatchDelete($fields, $className, $object = NULL)
public function controllerMethodBatchDelete($fields, $className, $object = NULL): string
{
return '
/**
@@ -269,7 +270,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 构建详情
*/
public function controllerMethodDetail($fields, $className, $managerName)
public function controllerMethodDetail($fields, $className, $managerName): string
{
return '
/**
@@ -293,7 +294,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 构建删除操作
*/
public function controllerMethodDelete($fields, $className, $managerName)
public function controllerMethodDelete($fields, $className, $managerName): string
{
return '
/**
@@ -329,7 +330,7 @@ class {$controllerName}Controller extends Controller
* @return string
* 构建查询列表
*/
public function controllerMethodList($fields, $className, $managerName, $object = NULL)
public function controllerMethodList($fields, $className, $managerName, $object = NULL): string
{
return '
/**
@@ -366,7 +367,7 @@ class {$controllerName}Controller extends Controller
';
}
private function getData($fields)
private function getData($fields): string
{
$html = '';
@@ -439,7 +440,12 @@ class {$controllerName}Controller extends Controller
return $html;
}
private function getMaxLength($fields)
/**
* @param $fields
* @return int
*/
private function getMaxLength($fields): int
{
$length = 0;
foreach ($fields as $key => $val) {
@@ -448,7 +454,11 @@ class {$controllerName}Controller extends Controller
return $length;
}
private function getWhere($fields)
/**
* @param $fields
* @return string
*/
private function getWhere($fields): string
{
$html = '';
+2 -5
View File
@@ -15,17 +15,14 @@ use Snowflake\Snowflake;
class GiiInterceptor extends GiiBase
{
public $tableName;
public $visible;
public $res;
public $fields;
public ?string $tableName = null;
/**
* @return string[]
* @throws Exception
*/
public function generate()
public function generate(): array
{
$managerName = $this->input->get('name', null);
+2 -5
View File
@@ -15,17 +15,14 @@ use Snowflake\Snowflake;
class GiiLimits extends GiiBase
{
public $tableName;
public $visible;
public $res;
public $fields;
public ?string $tableName = '';
/**
* @return string[]
* @throws Exception
*/
public function generate()
public function generate(): array
{
$managerName = $this->input->get('name', null);
+2 -7
View File
@@ -15,17 +15,12 @@ use Snowflake\Snowflake;
class GiiMiddleware extends GiiBase
{
public $tableName;
public $visible;
public $res;
public $fields;
/**
* @return string[]
* @return array
* @throws Exception
*/
public function generate()
public function generate(): array
{
$managerName = $this->input->get('name', null);
+30 -24
View File
@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Gii;
use Database\Db;
use Exception;
use ReflectionException;
use Snowflake\Snowflake;
/**
@@ -14,11 +16,10 @@ use Snowflake\Snowflake;
class GiiModel extends GiiBase
{
public $classFileName;
public $tableName;
public $visible;
public $res;
public $fields;
public ?string $classFileName;
public ?array $visible;
public ?array $res;
public ?array $fields;
/**
* ModelFile constructor.
@@ -28,7 +29,7 @@ class GiiModel extends GiiBase
* @param $res
* @param $fields
*/
public function __construct($classFileName, $tableName, $visible, $res, $fields)
public function __construct(string $classFileName, string $tableName, array $visible, array $res, array $fields)
{
$this->classFileName = $classFileName;
$this->tableName = $tableName;
@@ -38,10 +39,10 @@ class GiiModel extends GiiBase
}
/**
* @throws \ReflectionException
* @throws \Exception
* @throws ReflectionException
* @throws Exception
*/
public function generate()
public function generate(): string
{
$class = '';
$modelPath = $this->getModelPath();
@@ -75,7 +76,7 @@ use Database\ActiveRecord;';
$createSql = $this->setCreateSql($this->tableName);
if (strpos($html, $createSql) === false) {
if (!str_contains($html, $createSql)) {
$html .= '
' . $this->setCreateSql($this->tableName);
}
@@ -193,7 +194,12 @@ class ' . $managerName . ' extends ActiveRecord
}
private function generate_json_function($html, $fields)
/**
* @param $html
* @param $fields
* @return array
*/
private function generate_json_function($html, $fields): array
{
$strings = [];
foreach ($fields as $field) {
@@ -228,11 +234,11 @@ class ' . $managerName . ' extends ActiveRecord
}
';
if (strpos($html, 'set' . ucfirst($field['Field']) . 'Attribute') === false) {
if (!str_contains($html, 'set' . ucfirst($field['Field']) . 'Attribute')) {
$strings[] = $function;
}
if (strpos($html, 'get' . ucfirst($field['Field']) . 'Attribute') === false) {
if (!str_contains($html, 'get' . ucfirst($field['Field']) . 'Attribute')) {
$strings[] = $get_function;
}
}
@@ -247,7 +253,7 @@ class ' . $managerName . ' extends ActiveRecord
* @return string
* 创建表名称
*/
private function createTableName($field)
private function createTableName($field): string
{
$prefixed = $this->db->tablePrefix;
@@ -271,7 +277,7 @@ class ' . $managerName . ' extends ActiveRecord
* @return string
* 创建效验规则
*/
private function createRules($fields)
private function createRules($fields): string
{
$data = [];
foreach ($fields as $key => $val) {
@@ -325,7 +331,7 @@ class ' . $managerName . ' extends ActiveRecord
* @param $val
* @return string
*/
public function getLength($val)
public function getLength($val): string
{
$data = [];
foreach ($val as $key => $_val) {
@@ -339,7 +345,7 @@ class ' . $managerName . ' extends ActiveRecord
if (empty($data)) return '';
$string = [];
foreach ($data as $key => $_val) {
if (is_string($key) && strpos($key, ',') !== false) {
if (is_string($key) && str_contains($key, ',')) {
$key = '[' . $key . ']';
}
if (count($_val) == 1) {
@@ -359,12 +365,12 @@ class ' . $managerName . ' extends ActiveRecord
* @param $fields
* @return string
*/
public function getUnique($fields)
public function getUnique($fields): string
{
$data = [];
foreach ($fields as $_key => $_val) {
if ($_val['Extra'] == 'auto_increment') continue;
if (strpos($_val['Type'], 'unique') !== FALSE) {
if (str_contains($_val['Type'], 'unique')) {
$data[] = $_val['Field'];
}
}
@@ -379,7 +385,7 @@ class ' . $managerName . ' extends ActiveRecord
* @param $val
* @return string
*/
public function getRequired($val)
public function getRequired($val): string
{
$data = [];
foreach ($val as $_key => $_val) {
@@ -409,7 +415,7 @@ class ' . $managerName . ' extends ActiveRecord
* 'field' ,'字段類型' ,'是否必填' ,'字段长度' , '字段解释',
* )
*/
private function createPrimary($fields)
private function createPrimary($fields): ?string
{
$field = $this->getPrimaryKey($fields);
if (empty($field)) {
@@ -422,7 +428,7 @@ class ' . $managerName . ' extends ActiveRecord
/**
* @return string
*/
private function createDatabaseSource()
private function createDatabaseSource(): string
{
return '
/**
@@ -439,9 +445,9 @@ class ' . $managerName . ' extends ActiveRecord
/**
* @param $table
* @return string
* @throws \Exception
* @throws Exception
*/
private function setCreateSql($table)
private function setCreateSql($table): string
{
$text = Db::showCreateSql($table, $this->db)['Create Table'] ?? '';
+1 -7
View File
@@ -14,18 +14,12 @@ use Snowflake\Snowflake;
class GiiTask extends GiiBase
{
public $classFileName;
public $tableName;
public $visible;
public $res;
public $fields;
/**
* @return string[]
* @throws Exception
*/
public function generate()
public function generate(): array
{
$managerName = $this->input->get('name', null);