Browse Source

gii wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
ff08a5de94
  1. 2
      framework/yii/gii/Generator.php
  2. 3
      framework/yii/gii/assets/main.css
  3. 114
      framework/yii/gii/generators/form/Generator.php
  4. 8
      framework/yii/gii/generators/form/templates/action.php
  5. 8
      framework/yii/gii/generators/form/templates/form.php
  6. 11
      framework/yii/gii/generators/form/views/form.php
  7. 2
      framework/yii/gii/views/default/view/results.php

2
framework/yii/gii/Generator.php

@ -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";
}

3
framework/yii/gii/assets/main.css

@ -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;

114
framework/yii/gii/generators/form/Generator.php

@ -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();
}
}

8
framework/yii/gii/generators/form/templates/action.php

@ -0,0 +1,8 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:48 PM
* To change this template use File | Settings | File Templates.
*/

8
framework/yii/gii/generators/form/templates/form.php

@ -0,0 +1,8 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: qiang
* Date: 8/17/13
* Time: 3:47 PM
* To change this template use File | Settings | File Templates.
*/

11
framework/yii/gii/generators/form/views/form.php

@ -0,0 +1,11 @@
<?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'); ?>

2
framework/yii/gii/views/default/view/results.php

@ -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>

Loading…
Cancel
Save