Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-rpc
Commits
732baa64
Commit
732baa64
authored
May 08, 2020
by
梁俊杰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提供单独生成验证器的参数
parent
d84666cc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
125 additions
and
122 deletions
+125
-122
MakeModelCommand.php
src/Command/MakeModelCommand.php
+125
-122
No files found.
src/Command/MakeModelCommand.php
View file @
732baa64
...
...
@@ -122,11 +122,13 @@ class MakeModelCommand extends HyperfCommand
$this
->
makeModel
();
}
if
(
!
Str
::
contains
(
$v
,
"_to_"
)
&&
(
$this
->
input
->
getOption
(
'controller'
)
||
$this
->
input
->
getOption
(
'all'
)))
{
$this
->
makeValidator
();
$this
->
makeRepositoryInterface
();
$this
->
makeRepository
();
$this
->
makeController
();
}
if
(
!
Str
::
contains
(
$v
,
"_to_"
)
&&
(
$this
->
input
->
getOption
(
'validator'
)
||
$this
->
input
->
getOption
(
'all'
)))
{
$this
->
makeValidator
();
}
if
(
$this
->
input
->
getOption
(
'migrate'
)
||
$this
->
input
->
getOption
(
'all'
))
{
$this
->
makeMigrate
();
}
...
...
@@ -451,127 +453,6 @@ class MakeModelCommand extends HyperfCommand
return
true
;
}
/**
* 创建验证文件
*/
private
function
makeValidator
()
{
$stubFile
=
$this
->
path
.
'validator.stub'
;
$folder
=
$this
->
appPath
.
'/Validators'
;
$this
->
makeFolder
(
$folder
);
$table
=
$this
->
table
;
$modelClass
=
Str
::
studly
(
Str
::
singular
(
$table
));
$className
=
$modelClass
.
"Validator"
;
$file
=
$folder
.
"/"
.
$className
.
".php"
;
$content
=
file_get_contents
(
$stubFile
);
$info
=
$this
->
currentTableStructure
;
$filterFields
=
[
"id"
,
"created_at"
,
"updated_at"
,
"deleted_at"
];
$rules
=
''
;
$attributes
=
''
;
$messages
=
[];
$list
=
$info
[
'fields'
];
foreach
(
$list
as
$v
)
{
$name
=
$v
[
'column_name'
];
$default
=
$v
[
'column_default'
];
$type
=
$v
[
'data_type'
];
$key
=
$v
[
'column_key'
];
$null
=
$v
[
'is_nullable'
];
// $extra = $v['extra'];
$comment
=
$v
[
'column_comment'
];
$length
=
$v
[
'length'
];
$msgName
=
(
$comment
?
$comment
:
$name
);
if
(
in_array
(
$name
,
$filterFields
))
{
continue
;
}
$rs
=
[];
$required
=
"nullable"
;
if
(
$null
!==
'YES'
)
{
if
(
!
$default
)
{
$required
=
"required"
;
$messages
[]
=
"
\t\t
'
{
$name
}
.
{
$required
}
' => '
{
$msgName
}
不能为空!'"
;
}
}
$rs
[]
=
$required
;
switch
(
$type
)
{
case
"bigint"
:
case
"smallint"
:
case
"tinyint"
:
case
"mediumint"
:
case
"int"
:
case
"integer"
:
$rs
[]
=
'integer'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.integer' => '
{
$msgName
}
只能是整数!'"
;
break
;
case
"decimal"
:
case
"double"
:
case
"float"
:
case
"numeric"
:
case
"real"
:
$rs
[]
=
'numeric'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.numeric' => '
{
$msgName
}
只能是数字支持小数!'"
;
break
;
case
"char"
:
case
"varchar"
:
case
"tinytext"
:
case
"mediumtext"
:
case
"longtext"
:
case
"text"
:
$rs
[]
=
'string'
;
if
(
$length
)
{
$rs
[]
=
'max:'
.
$length
;
$messages
[]
=
"
\t\t
'
{
$name
}
.max' => '
{
$msgName
}
字符长度不能超过
{
$length
}
!'"
;
}
break
;
case
"date"
:
case
"datetime"
:
case
"time"
:
case
"timestamp"
:
case
"year"
:
$rs
[]
=
'date'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.date' => '
{
$msgName
}
不符合日期时间格式!'"
;
break
;
case
"enum"
:
case
"set"
:
$rs
[]
=
'in:['
.
$length
.
"]"
;
$messages
[]
=
"
\t\t
'
{
$name
}
.in' => '
{
$msgName
}
的值只能在[
{
$length
}
]列表中!'"
;
break
;
default
:
if
(
Str
::
contains
(
$name
,
"email"
)
||
Str
::
contains
(
$name
,
"e-mail"
)
||
Str
::
contains
(
$name
,
"e_mail"
))
{
$rs
[]
=
'email'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.email' => '
{
$msgName
}
只支持邮箱格式!'"
;
}
elseif
(
$name
==
'url'
||
Str
::
contains
(
$name
,
"_url"
)
||
Str
::
contains
(
$name
,
"url_"
))
{
$rs
[]
=
'url'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.email' => '
{
$msgName
}
只支持url格式!'"
;
}
elseif
(
$name
==
'date'
||
Str
::
contains
(
$name
,
"_date"
)
||
Str
::
contains
(
$name
,
"date_"
))
{
$rs
[]
=
'date'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.email' => '
{
$msgName
}
不符合日期时间格式!'"
;
}
break
;
}
if
(
$key
==
'uni'
)
{
$rs
[]
=
"unique:
$table
,"
.
$name
;
$messages
[]
=
"
\t\t
'
{
$name
}
.unique' => '
{
$msgName
}
的值在数据库中已经存在!'"
;
}
if
(
$comment
)
{
$attributes
.=
"
\t\t
'"
.
$name
.
"' => '"
.
$comment
.
"',"
.
"
\n
"
;
}
$rules
.=
"
\t\t\t
'"
.
$name
.
"' => '"
.
implode
(
"|"
,
$rs
)
.
"',"
.
(
$comment
?
"// "
.
$comment
.
"-"
.
$type
:
"//"
.
$type
)
.
"
\n
"
;
}
$messages
=
join
(
",
\n
"
,
$messages
);
$patterns
=
[
"%ModelClass%"
,
'%createRules%'
,
'%updateRules%'
,
'%attributes%'
,
'%messages%'
];
$createRules
=
$rules
;
$updateRules
=
str_replace
(
"nullable"
,
"sometimes|nullable"
,
$rules
);
$updateRules
=
str_replace
(
"required"
,
"sometimes|required"
,
$updateRules
);
$replacements
=
[
$modelClass
,
$createRules
,
$updateRules
,
$attributes
,
$messages
];
$content
=
$this
->
buildField
(
$patterns
,
$replacements
,
$content
);
$this
->
writeToFile
(
$file
,
$content
);
}
private
function
makeRepositoryInterface
()
{
$stubFile
=
$this
->
path
.
'repositoryInterface.stub'
;
...
...
@@ -796,6 +677,127 @@ class MakeModelCommand extends HyperfCommand
}
}
/**
* 创建验证文件
*/
private
function
makeValidator
()
{
$stubFile
=
$this
->
path
.
'validator.stub'
;
$folder
=
$this
->
appPath
.
'/Validators'
;
$this
->
makeFolder
(
$folder
);
$table
=
$this
->
table
;
$modelClass
=
Str
::
studly
(
Str
::
singular
(
$table
));
$className
=
$modelClass
.
"Validator"
;
$file
=
$folder
.
"/"
.
$className
.
".php"
;
$content
=
file_get_contents
(
$stubFile
);
$info
=
$this
->
currentTableStructure
;
$filterFields
=
[
"id"
,
"created_at"
,
"updated_at"
,
"deleted_at"
];
$rules
=
''
;
$attributes
=
''
;
$messages
=
[];
$list
=
$info
[
'fields'
];
foreach
(
$list
as
$v
)
{
$name
=
$v
[
'column_name'
];
$default
=
$v
[
'column_default'
];
$type
=
$v
[
'data_type'
];
$key
=
$v
[
'column_key'
];
$null
=
$v
[
'is_nullable'
];
// $extra = $v['extra'];
$comment
=
$v
[
'column_comment'
];
$length
=
$v
[
'length'
];
$msgName
=
(
$comment
?
$comment
:
$name
);
if
(
in_array
(
$name
,
$filterFields
))
{
continue
;
}
$rs
=
[];
$required
=
"nullable"
;
if
(
$null
!==
'YES'
)
{
if
(
!
$default
)
{
$required
=
"required"
;
$messages
[]
=
"
\t\t
'
{
$name
}
.
{
$required
}
' => '
{
$msgName
}
不能为空!'"
;
}
}
$rs
[]
=
$required
;
switch
(
$type
)
{
case
"bigint"
:
case
"smallint"
:
case
"tinyint"
:
case
"mediumint"
:
case
"int"
:
case
"integer"
:
$rs
[]
=
'integer'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.integer' => '
{
$msgName
}
只能是整数!'"
;
break
;
case
"decimal"
:
case
"double"
:
case
"float"
:
case
"numeric"
:
case
"real"
:
$rs
[]
=
'numeric'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.numeric' => '
{
$msgName
}
只能是数字支持小数!'"
;
break
;
case
"char"
:
case
"varchar"
:
case
"tinytext"
:
case
"mediumtext"
:
case
"longtext"
:
case
"text"
:
$rs
[]
=
'string'
;
if
(
$length
)
{
$rs
[]
=
'max:'
.
$length
;
$messages
[]
=
"
\t\t
'
{
$name
}
.max' => '
{
$msgName
}
字符长度不能超过
{
$length
}
!'"
;
}
break
;
case
"date"
:
case
"datetime"
:
case
"time"
:
case
"timestamp"
:
case
"year"
:
$rs
[]
=
'date'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.date' => '
{
$msgName
}
不符合日期时间格式!'"
;
break
;
case
"enum"
:
case
"set"
:
$rs
[]
=
'in:['
.
$length
.
"]"
;
$messages
[]
=
"
\t\t
'
{
$name
}
.in' => '
{
$msgName
}
的值只能在[
{
$length
}
]列表中!'"
;
break
;
default
:
if
(
Str
::
contains
(
$name
,
"email"
)
||
Str
::
contains
(
$name
,
"e-mail"
)
||
Str
::
contains
(
$name
,
"e_mail"
))
{
$rs
[]
=
'email'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.email' => '
{
$msgName
}
只支持邮箱格式!'"
;
}
elseif
(
$name
==
'url'
||
Str
::
contains
(
$name
,
"_url"
)
||
Str
::
contains
(
$name
,
"url_"
))
{
$rs
[]
=
'url'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.email' => '
{
$msgName
}
只支持url格式!'"
;
}
elseif
(
$name
==
'date'
||
Str
::
contains
(
$name
,
"_date"
)
||
Str
::
contains
(
$name
,
"date_"
))
{
$rs
[]
=
'date'
;
$messages
[]
=
"
\t\t
'
{
$name
}
.email' => '
{
$msgName
}
不符合日期时间格式!'"
;
}
break
;
}
if
(
$key
==
'uni'
)
{
$rs
[]
=
"unique:
$table
,"
.
$name
;
$messages
[]
=
"
\t\t
'
{
$name
}
.unique' => '
{
$msgName
}
的值在数据库中已经存在!'"
;
}
if
(
$comment
)
{
$attributes
.=
"
\t\t
'"
.
$name
.
"' => '"
.
$comment
.
"',"
.
"
\n
"
;
}
$rules
.=
"
\t\t\t
'"
.
$name
.
"' => '"
.
implode
(
"|"
,
$rs
)
.
"',"
.
(
$comment
?
"// "
.
$comment
.
"-"
.
$type
:
"//"
.
$type
)
.
"
\n
"
;
}
$messages
=
join
(
",
\n
"
,
$messages
);
$patterns
=
[
"%ModelClass%"
,
'%createRules%'
,
'%updateRules%'
,
'%attributes%'
,
'%messages%'
];
$createRules
=
$rules
;
$updateRules
=
str_replace
(
"nullable"
,
"sometimes|nullable"
,
$rules
);
$updateRules
=
str_replace
(
"required"
,
"sometimes|required"
,
$updateRules
);
$replacements
=
[
$modelClass
,
$createRules
,
$updateRules
,
$attributes
,
$messages
];
$content
=
$this
->
buildField
(
$patterns
,
$replacements
,
$content
);
$this
->
writeToFile
(
$file
,
$content
);
}
private
function
makeMigrate
()
{
$stubFile
=
$this
->
path
.
'migration.stub'
;
...
...
@@ -1032,6 +1034,7 @@ class MakeModelCommand extends HyperfCommand
$this
->
addOption
(
'model'
,
'm'
,
InputOption
::
VALUE_NONE
,
'生成model文件'
);
$this
->
addOption
(
'controller'
,
'c'
,
InputOption
::
VALUE_NONE
,
'生成controller文件'
);
$this
->
addOption
(
'migrate'
,
'i'
,
InputOption
::
VALUE_NONE
,
'生成迁移文件'
);
$this
->
addOption
(
'validator'
,
'l'
,
InputOption
::
VALUE_NONE
,
'生成验证文件'
);
$this
->
addOption
(
'author'
,
'r'
,
InputOption
::
VALUE_OPTIONAL
,
'生成文件的作者'
);
$this
->
addOption
(
'force'
,
'f'
,
InputOption
::
VALUE_NONE
,
'文件存在是否覆盖'
);
$this
->
addOption
(
'database'
,
'd'
,
InputOption
::
VALUE_NONE
,
'全数据库索引自动生成全站文件'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment