Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
PSDI Army
yii2
Commits
ff08a5de
Commit
ff08a5de
authored
Aug 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gii wip
parent
18da3eaa
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
144 additions
and
4 deletions
+144
-4
Generator.php
framework/yii/gii/Generator.php
+1
-1
main.css
framework/yii/gii/assets/main.css
+2
-1
Generator.php
framework/yii/gii/generators/form/Generator.php
+113
-1
action.php
framework/yii/gii/generators/form/templates/action.php
+8
-0
form.php
framework/yii/gii/generators/form/templates/form.php
+8
-0
form.php
framework/yii/gii/generators/form/views/form.php
+11
-0
results.php
framework/yii/gii/views/default/view/results.php
+1
-1
No files found.
framework/yii/gii/Generator.php
View file @
ff08a5de
...
...
@@ -231,7 +231,7 @@ abstract class Generator extends Model
$error
=
$file
->
save
();
if
(
is_string
(
$error
))
{
$hasError
=
true
;
$lines
[]
=
"
<span class=
\"
error
\"
>generating
$relativePath
<br>
$error
</span>"
;
$lines
[]
=
"
generating
$relativePath
\n
<span class=
\"
error
\"
>
$error
</span>"
;
}
else
{
$lines
[]
=
$file
->
operation
===
CodeFile
::
OP_NEW
?
" generated
$relativePath
"
:
" overwrote
$relativePath
"
;
}
...
...
framework/yii/gii/assets/main.css
View file @
ff08a5de
...
...
@@ -90,9 +90,10 @@ body {
color
:
white
;
padding
:
10px
;
border-radius
:
0
;
white-space
:
nowrap
;
}
.default-view-results
pre
span
.error
{
.default-view-results
pre
.error
{
background
:
#FFE0E1
;
color
:
black
;
padding
:
1px
;
...
...
framework/yii/gii/generators/form/Generator.php
View file @
ff08a5de
...
...
@@ -7,6 +7,10 @@
namespace
yii\gii\generators\form
;
use
Yii
;
use
yii\base\Model
;
use
yii\gii\CodeFile
;
/**
*
* @author Qiang Xue <qiang.xue@gmail.com>
...
...
@@ -14,11 +18,23 @@ namespace yii\gii\generators\form;
*/
class
Generator
extends
\yii\gii\Generator
{
public
$modelClass
;
public
$viewPath
=
'@app/views'
;
public
$viewName
;
public
$scenarioName
=
'default'
;
/**
* @inheritdoc
*/
public
function
getName
()
{
return
'Form Generator'
;
}
/**
* @inheritdoc
*/
public
function
getDescription
()
{
return
'This generator generates a view script file that displays a form to collect input for the specified model class.'
;
...
...
@@ -29,6 +45,102 @@ class Generator extends \yii\gii\Generator
*/
public
function
generate
()
{
return
array
();
$files
=
array
();
$files
[]
=
new
CodeFile
(
Yii
::
getAlias
(
$this
->
viewPath
)
.
'/'
.
$this
->
viewName
.
'.php'
,
$this
->
render
(
$this
->
getTemplatePath
()
.
'/form.php'
)
);
return
$files
;
}
public
function
rules
()
{
return
array_merge
(
parent
::
rules
(),
array
(
array
(
'modelClass, viewName, scenarioName'
,
'filter'
,
'filter'
=>
'trim'
),
array
(
'modelClass, viewName, viewPath'
,
'required'
),
array
(
'modelClass, viewPath'
,
'match'
,
'pattern'
=>
'/^@?\w+[\\-\\/\w+]*$/'
,
'message'
=>
'Only word characters, dashes, slashes and @ are allowed.'
),
array
(
'viewName'
,
'match'
,
'pattern'
=>
'/^\w+[\\-\\/\w+]*$/'
,
'message'
=>
'Only word characters, dashes and slashes are allowed.'
),
array
(
'modelClass'
,
'validateModel'
),
array
(
'viewPath'
,
'validateViewPath'
),
array
(
'scenarioName'
,
'match'
,
'pattern'
=>
'/^\w+$/'
,
'message'
=>
'Only word characters are allowed.'
),
));
}
public
function
attributeLabels
()
{
return
array
(
'modelClass'
=>
'Model Class'
,
'viewName'
=>
'View Name'
,
'viewPath'
=>
'View Path'
,
'scenarioName'
=>
'Scenario'
,
);
}
public
function
requiredTemplates
()
{
return
array
(
'form.php'
,
'action.php'
,
);
}
/**
* @inheritdoc
*/
public
function
stickyAttributes
()
{
return
array
(
'viewPath'
,
'scenarioName'
);
}
/**
* @inheritdoc
*/
public
function
hints
()
{
return
array
(
);
}
public
function
successMessage
()
{
$output
=
<<<EOD
<p>The form has been generated successfully.</p>
<p>You may add the following code in an appropriate controller class to invoke the view:</p>
EOD;
$code
=
"<?php
\n
"
.
$this
->
render
(
$this
->
getTemplatePath
()
.
'/action.php'
);
return
$output
.
highlight_string
(
$code
,
true
);
}
public
function
validateModel
(
$attribute
,
$params
)
{
try
{
if
(
class_exists
(
$this
->
modelClass
))
{
if
(
!
is_subclass_of
(
$this
->
modelClass
,
Model
::
className
()))
{
$this
->
addError
(
'modelClass'
,
"'
{
$this
->
modelClass
}
' must extend from Model or its child class."
);
}
}
else
{
$this
->
addError
(
'modelClass'
,
"Class '
{
$this
->
modelClass
}
' does not exist or has syntax error."
);
}
}
catch
(
\Exception
$e
)
{
$this
->
addError
(
'modelClass'
,
"Class '
{
$this
->
modelClass
}
' does not exist or has syntax error."
);
return
;
}
}
public
function
validateViewPath
()
{
$path
=
Yii
::
getAlias
(
$this
->
viewPath
,
false
);
if
(
$path
===
false
||
!
is_dir
(
$path
))
{
$this
->
addError
(
'viewPath'
,
'View path does not exist.'
);
}
}
public
function
getModelAttributes
()
{
/** @var Model $model */
$model
=
new
$this
->
modelClass
;
$model
->
setScenario
(
$this
->
scenarioName
);
return
$model
->
safeAttributes
();
}
}
framework/yii/gii/generators/form/templates/action.php
0 → 100644
View file @
ff08a5de
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:48 PM
* To change this template use File | Settings | File Templates.
*/
framework/yii/gii/generators/form/templates/form.php
0 → 100644
View file @
ff08a5de
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:47 PM
* To change this template use File | Settings | File Templates.
*/
framework/yii/gii/generators/form/views/form.php
0 → 100644
View file @
ff08a5de
<?php
/**
* @var yii\base\View $this
* @var yii\widgets\ActiveForm $form
* @var yii\gii\generators\form\Generator $generator
*/
?>
<?php
echo
$form
->
field
(
$generator
,
'viewName'
);
?>
<?php
echo
$form
->
field
(
$generator
,
'modelClass'
);
?>
<?php
echo
$form
->
field
(
$generator
,
'scenarioName'
);
?>
<?php
echo
$form
->
field
(
$generator
,
'viewPath'
);
?>
framework/yii/gii/views/default/view/results.php
View file @
ff08a5de
...
...
@@ -18,5 +18,5 @@ use yii\gii\CodeFile;
echo
'<div class="alert alert-success">'
.
$generator
->
successMessage
()
.
'</div>'
;
}
?>
<pre>
<?php
echo
$results
;
?>
</pre>
<pre>
<?php
echo
nl2br
(
$results
)
;
?>
</pre>
</div>
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