Commit ff08a5de by Qiang Xue

gii wip

parent 18da3eaa
...@@ -231,7 +231,7 @@ abstract class Generator extends Model ...@@ -231,7 +231,7 @@ abstract class Generator extends Model
$error = $file->save(); $error = $file->save();
if (is_string($error)) { if (is_string($error)) {
$hasError = true; $hasError = true;
$lines[] = "<span class=\"error\">generating $relativePath<br> $error</span>"; $lines[] = "generating $relativePath\n<span class=\"error\">$error</span>";
} else { } else {
$lines[] = $file->operation === CodeFile::OP_NEW ? " generated $relativePath" : " overwrote $relativePath"; $lines[] = $file->operation === CodeFile::OP_NEW ? " generated $relativePath" : " overwrote $relativePath";
} }
......
...@@ -90,9 +90,10 @@ body { ...@@ -90,9 +90,10 @@ body {
color: white; color: white;
padding: 10px; padding: 10px;
border-radius: 0; border-radius: 0;
white-space: nowrap;
} }
.default-view-results pre span.error { .default-view-results pre .error {
background: #FFE0E1; background: #FFE0E1;
color: black; color: black;
padding: 1px; padding: 1px;
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
namespace yii\gii\generators\form; namespace yii\gii\generators\form;
use Yii;
use yii\base\Model;
use yii\gii\CodeFile;
/** /**
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -14,11 +18,23 @@ namespace yii\gii\generators\form; ...@@ -14,11 +18,23 @@ namespace yii\gii\generators\form;
*/ */
class Generator extends \yii\gii\Generator class Generator extends \yii\gii\Generator
{ {
public $modelClass;
public $viewPath = '@app/views';
public $viewName;
public $scenarioName = 'default';
/**
* @inheritdoc
*/
public function getName() public function getName()
{ {
return 'Form Generator'; return 'Form Generator';
} }
/**
* @inheritdoc
*/
public function getDescription() public function getDescription()
{ {
return 'This generator generates a view script file that displays a form to collect input for the specified model class.'; 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 ...@@ -29,6 +45,102 @@ class Generator extends \yii\gii\Generator
*/ */
public function generate() 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();
} }
} }
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:48 PM
* To change this template use File | Settings | File Templates.
*/
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:47 PM
* To change this template use File | Settings | File Templates.
*/
<?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'); ?>
...@@ -18,5 +18,5 @@ use yii\gii\CodeFile; ...@@ -18,5 +18,5 @@ use yii\gii\CodeFile;
echo '<div class="alert alert-success">' . $generator->successMessage() . '</div>'; echo '<div class="alert alert-success">' . $generator->successMessage() . '</div>';
} }
?> ?>
<pre><?php echo $results; ?></pre> <pre><?php echo nl2br($results); ?></pre>
</div> </div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment