From ff08a5de94db609bf40e029d55468df82adf3f8a Mon Sep 17 00:00:00 2001
From: Qiang Xue
Date: Sat, 17 Aug 2013 21:08:14 -0400
Subject: [PATCH] gii wip
---
framework/yii/gii/Generator.php | 2 +-
framework/yii/gii/assets/main.css | 3 +-
framework/yii/gii/generators/form/Generator.php | 114 ++++++++++++++++++++-
.../yii/gii/generators/form/templates/action.php | 8 ++
.../yii/gii/generators/form/templates/form.php | 8 ++
framework/yii/gii/generators/form/views/form.php | 11 ++
framework/yii/gii/views/default/view/results.php | 2 +-
7 files changed, 144 insertions(+), 4 deletions(-)
create mode 100644 framework/yii/gii/generators/form/templates/action.php
create mode 100644 framework/yii/gii/generators/form/templates/form.php
create mode 100644 framework/yii/gii/generators/form/views/form.php
diff --git a/framework/yii/gii/Generator.php b/framework/yii/gii/Generator.php
index 5f4257d..aa4f014 100644
--- a/framework/yii/gii/Generator.php
+++ b/framework/yii/gii/Generator.php
@@ -231,7 +231,7 @@ abstract class Generator extends Model
$error = $file->save();
if (is_string($error)) {
$hasError = true;
- $lines[] = "generating $relativePath
$error";
+ $lines[] = "generating $relativePath\n$error";
} else {
$lines[] = $file->operation === CodeFile::OP_NEW ? " generated $relativePath" : " overwrote $relativePath";
}
diff --git a/framework/yii/gii/assets/main.css b/framework/yii/gii/assets/main.css
index 12d2b86..dc0c618 100644
--- a/framework/yii/gii/assets/main.css
+++ b/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;
diff --git a/framework/yii/gii/generators/form/Generator.php b/framework/yii/gii/generators/form/Generator.php
index 043fa0f..759978c 100644
--- a/framework/yii/gii/generators/form/Generator.php
+++ b/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
@@ -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 = <<The form has been generated successfully.
+You may add the following code in an appropriate controller class to invoke the view:
+EOD;
+ $code = "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();
}
}
diff --git a/framework/yii/gii/generators/form/templates/action.php b/framework/yii/gii/generators/form/templates/action.php
new file mode 100644
index 0000000..68168de
--- /dev/null
+++ b/framework/yii/gii/generators/form/templates/action.php
@@ -0,0 +1,8 @@
+
+field($generator, 'viewName'); ?>
+field($generator, 'modelClass'); ?>
+field($generator, 'scenarioName'); ?>
+field($generator, 'viewPath'); ?>
diff --git a/framework/yii/gii/views/default/view/results.php b/framework/yii/gii/views/default/view/results.php
index 0173882..caca404 100644
--- a/framework/yii/gii/views/default/view/results.php
+++ b/framework/yii/gii/views/default/view/results.php
@@ -18,5 +18,5 @@ use yii\gii\CodeFile;
echo '' . $generator->successMessage() . '
';
}
?>
-
+