From 62b4a645f1c5fa52c29dad57f1baf84d9f67f708 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 27 Aug 2013 08:08:57 -0400 Subject: [PATCH] crud generator WIP --- framework/yii/gii/generators/crud/Generator.php | 83 +++++++++++++++++++++- framework/yii/gii/generators/crud/form.php | 10 +++ .../gii/generators/crud/templates/controller.php | 8 +++ .../yii/gii/generators/crud/templates/model.php | 8 +++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 framework/yii/gii/generators/crud/form.php create mode 100644 framework/yii/gii/generators/crud/templates/controller.php create mode 100644 framework/yii/gii/generators/crud/templates/model.php diff --git a/framework/yii/gii/generators/crud/Generator.php b/framework/yii/gii/generators/crud/Generator.php index 15da2c2..2b6697d 100644 --- a/framework/yii/gii/generators/crud/Generator.php +++ b/framework/yii/gii/generators/crud/Generator.php @@ -7,6 +7,10 @@ namespace yii\gii\generators\crud; +use yii\db\ActiveRecord; +use yii\gii\CodeFile; +use yii\web\Controller; + /** * * @author Qiang Xue @@ -14,6 +18,10 @@ namespace yii\gii\generators\crud; */ class Generator extends \yii\gii\Generator { + public $modelClass; + public $controllerID; + public $baseControllerClass = 'yii\web\Controller'; + public function getName() { return 'CRUD Generator'; @@ -25,11 +33,84 @@ class Generator extends \yii\gii\Generator operations for the specified data model.'; } + public function rules() + { + return array_merge(parent::rules(), array( + array('modelClass, controllerID, baseControllerClass', 'filter', 'filter' => 'trim'), + array('modelClass, controllerID, baseControllerClass', 'required'), + array('modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), + array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())), + array('controllerID', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'), + array('baseControllerClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), + array('baseControllerClass', 'validateClass', 'params' => array('extends' => Controller::className())), + )); + } + + public function attributeLabels() + { + return array_merge(parent::attributeLabels(), array( + 'modelClass' => 'Model Class', + 'controllerID' => 'Controller ID', + 'baseControllerClass' => 'Base Controller Class', + )); + } + + /** + * @inheritdoc + */ + public function hints() + { + return array( + 'modelClass' => 'This is the ActiveRecord class associated with the table that CRUD will be built upon. + You should provide a fully qualified class name, e.g., app\models\Post.', + 'controllerID' => 'CRUD controllers are often named after the model class name that they are dealing with. + Controller ID should be in lower case and may contain module ID(s) separated by slashes. For example: + ', + 'baseControllerClass' => 'This is the class that the new CRUD controller class will extend from. + You should provide a fully qualified class name, e.g., yii\web\Controller.', + ); + } + + public function requiredTemplates() + { + return array( + 'controller.php', + ); + } + + /** + * @inheritdoc + */ + public function stickyAttributes() + { + return array('baseControllerClass'); + } + /** * @inheritdoc */ public function generate() { - return array(); + $files = array(); + $files[] = new CodeFile( + $this->controllerFile, + $this->render('controller.php') + ); + + $files = scandir($this->getTemplatePath()); + foreach ($files as $file) { + if (is_file($templatePath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && $file !== 'controller.php') { + $files[] = new CodeFile( + $this->viewPath . DIRECTORY_SEPARATOR . $file, + $this->render($templatePath . '/' . $file) + ); + } + } + + return $files; } } diff --git a/framework/yii/gii/generators/crud/form.php b/framework/yii/gii/generators/crud/form.php new file mode 100644 index 0000000..0951695 --- /dev/null +++ b/framework/yii/gii/generators/crud/form.php @@ -0,0 +1,10 @@ +field($generator, 'modelClass'); +echo $form->field($generator, 'controllerID'); +echo $form->field($generator, 'baseControllerClass'); diff --git a/framework/yii/gii/generators/crud/templates/controller.php b/framework/yii/gii/generators/crud/templates/controller.php new file mode 100644 index 0000000..f372629 --- /dev/null +++ b/framework/yii/gii/generators/crud/templates/controller.php @@ -0,0 +1,8 @@ +