From 8ea5858d0dcef776b7329cba3bb980606eed474a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 20 Aug 2013 14:36:43 -0400 Subject: [PATCH] refactored gii. --- framework/yii/gii/Generator.php | 11 +++-- .../yii/gii/generators/controller/Generator.php | 6 +-- framework/yii/gii/generators/controller/form.php | 10 ++++ .../yii/gii/generators/controller/views/form.php | 10 ---- framework/yii/gii/generators/form/Generator.php | 4 +- framework/yii/gii/generators/form/form.php | 10 ++++ framework/yii/gii/generators/form/views/form.php | 10 ---- framework/yii/gii/generators/model/Generator.php | 53 ++++++++-------------- framework/yii/gii/generators/model/form.php | 12 +++++ framework/yii/gii/generators/model/views/form.php | 12 ----- framework/yii/gii/generators/module/Generator.php | 7 ++- framework/yii/gii/generators/module/form.php | 13 ++++++ framework/yii/gii/generators/module/views/form.php | 13 ------ 13 files changed, 77 insertions(+), 94 deletions(-) create mode 100644 framework/yii/gii/generators/controller/form.php delete mode 100644 framework/yii/gii/generators/controller/views/form.php create mode 100644 framework/yii/gii/generators/form/form.php delete mode 100644 framework/yii/gii/generators/form/views/form.php create mode 100644 framework/yii/gii/generators/model/form.php delete mode 100644 framework/yii/gii/generators/model/views/form.php create mode 100644 framework/yii/gii/generators/module/form.php delete mode 100644 framework/yii/gii/generators/module/views/form.php diff --git a/framework/yii/gii/Generator.php b/framework/yii/gii/Generator.php index aa4f014..eb7b696 100644 --- a/framework/yii/gii/Generator.php +++ b/framework/yii/gii/Generator.php @@ -116,14 +116,14 @@ abstract class Generator extends Model /** * Returns the view file for the input form of the generator. - * The default implementation will return the "form.php" file under the "views" subdirectory of the - * directory containing the generator class file. + * The default implementation will return the "form.php" file under the directory + * that contains the generator class file. * @return string the view file for the input form of the generator. */ public function formView() { $class = new ReflectionClass($this); - return dirname($class->getFileName()) . '/views/form.php'; + return dirname($class->getFileName()) . '/form.php'; } /** @@ -261,7 +261,8 @@ abstract class Generator extends Model /** * Generates code using the specified code template and parameters. * Note that the code template will be used as a PHP file. - * @param string $template the code template file + * @param string $template the code template file. This must be specified as a file path + * relative to [[templatePath]]. * @param array $params list of parameters to be passed to the template file. * @return string the generated code */ @@ -269,7 +270,7 @@ abstract class Generator extends Model { $view = new View; $params['generator'] = $this; - return $view->renderFile($template, $params, $this); + return $view->renderFile($this->getTemplatePath() . '/' . $template, $params, $this); } /** diff --git a/framework/yii/gii/generators/controller/Generator.php b/framework/yii/gii/generators/controller/Generator.php index 1447e09..df7c129 100644 --- a/framework/yii/gii/generators/controller/Generator.php +++ b/framework/yii/gii/generators/controller/Generator.php @@ -147,17 +147,15 @@ class Generator extends \yii\gii\Generator { $files = array(); - $templatePath = $this->getTemplatePath(); - $files[] = new CodeFile( $this->getControllerFile(), - $this->render($templatePath . '/controller.php') + $this->render('controller.php') ); foreach ($this->getActionIDs() as $action) { $files[] = new CodeFile( $this->getViewFile($action), - $this->render($templatePath . '/view.php', array('action' => $action)) + $this->render('view.php', array('action' => $action)) ); } diff --git a/framework/yii/gii/generators/controller/form.php b/framework/yii/gii/generators/controller/form.php new file mode 100644 index 0000000..e4d2947 --- /dev/null +++ b/framework/yii/gii/generators/controller/form.php @@ -0,0 +1,10 @@ +field($generator, 'controller'); +echo $form->field($generator, 'actions'); +echo $form->field($generator, 'ns'); +echo $form->field($generator, 'baseClass'); diff --git a/framework/yii/gii/generators/controller/views/form.php b/framework/yii/gii/generators/controller/views/form.php deleted file mode 100644 index e4d2947..0000000 --- a/framework/yii/gii/generators/controller/views/form.php +++ /dev/null @@ -1,10 +0,0 @@ -field($generator, 'controller'); -echo $form->field($generator, 'actions'); -echo $form->field($generator, 'ns'); -echo $form->field($generator, 'baseClass'); diff --git a/framework/yii/gii/generators/form/Generator.php b/framework/yii/gii/generators/form/Generator.php index 70420f6..4ae96c9 100644 --- a/framework/yii/gii/generators/form/Generator.php +++ b/framework/yii/gii/generators/form/Generator.php @@ -49,7 +49,7 @@ class Generator extends \yii\gii\Generator $files = array(); $files[] = new CodeFile( Yii::getAlias($this->viewPath) . '/' . $this->viewName . '.php', - $this->render($this->getTemplatePath() . '/form.php') + $this->render('form.php') ); return $files; } @@ -121,7 +121,7 @@ class Generator extends \yii\gii\Generator */ public function successMessage() { - $code = highlight_string($this->render($this->getTemplatePath() . '/action.php'), true); + $code = highlight_string($this->render('action.php'), true); return <<The form has been generated successfully.

You may add the following code in an appropriate controller class to invoke the view:

diff --git a/framework/yii/gii/generators/form/form.php b/framework/yii/gii/generators/form/form.php new file mode 100644 index 0000000..c04a26e --- /dev/null +++ b/framework/yii/gii/generators/form/form.php @@ -0,0 +1,10 @@ +field($generator, 'viewName'); +echo $form->field($generator, 'modelClass'); +echo $form->field($generator, 'scenarioName'); +echo $form->field($generator, 'viewPath'); diff --git a/framework/yii/gii/generators/form/views/form.php b/framework/yii/gii/generators/form/views/form.php deleted file mode 100644 index c04a26e..0000000 --- a/framework/yii/gii/generators/form/views/form.php +++ /dev/null @@ -1,10 +0,0 @@ -field($generator, 'viewName'); -echo $form->field($generator, 'modelClass'); -echo $form->field($generator, 'scenarioName'); -echo $form->field($generator, 'viewPath'); diff --git a/framework/yii/gii/generators/model/Generator.php b/framework/yii/gii/generators/model/Generator.php index f6cda34..e60dac7 100644 --- a/framework/yii/gii/generators/model/Generator.php +++ b/framework/yii/gii/generators/model/Generator.php @@ -8,6 +8,8 @@ namespace yii\gii\generators\model; use Yii; +use yii\base\InvalidConfigException; +use yii\gii\CodeFile; /** * @@ -36,16 +38,15 @@ class Generator extends \yii\gii\Generator public function getDescription() { - return 'This generator generates a model class for the specified database table.'; + return 'This generator generates an ActiveRecord class for the specified database table.'; } public function rules() { return array_merge(parent::rules(), array( array('tablePrefix, baseClass, tableName, modelClass, modelPath, connectionId', 'filter', 'filter' => 'trim'), - array('connectionId, tableName, modelPath, baseClass', 'required'), + array('tableName, modelPath, baseClass', 'required'), array('tablePrefix, tableName, modelPath', 'match', 'pattern' => '/^(\w+[\w\.]*|\*?|\w+\.\*)$/', 'message' => '{attribute} should only contain word characters, dots, and an optional ending asterisk.'), - array('connectionId', 'validateConnectionId'), array('tableName', 'validateTableName'), array('tablePrefix, modelClass', 'match', 'pattern' => '/^[a-zA-Z_]\w*$/', 'message' => '{attribute} should only contain word characters.'), array('baseClass', 'match', 'pattern' => '/^[a-zA-Z_][\w\\\\]*$/', 'message' => '{attribute} should only contain word characters and backslashes.'), @@ -57,7 +58,7 @@ class Generator extends \yii\gii\Generator public function attributeLabels() { - return array_merge(parent::attributeLabels(), array( + return array( 'tablePrefix' => 'Table Prefix', 'tableName' => 'Table Name', 'modelPath' => 'Model Path', @@ -66,7 +67,7 @@ class Generator extends \yii\gii\Generator 'buildRelations' => 'Build Relations', 'commentsAsLabels' => 'Use Column Comments as Attribute Labels', 'connectionId' => 'Database Connection', - )); + ); } public function requiredTemplates() @@ -78,15 +79,14 @@ class Generator extends \yii\gii\Generator public function stickyAttributes() { - return array('connectionId', 'tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels'); + return array('tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels'); } public function generate() { - if (Yii::$app->{$this->connectionId} === null) { - throw new CHttpException(500, 'A valid database connection is required to run this generator.'); + if (($db = Yii::$app->{$this->db}) === null) { + throw new InvalidConfigException('The "db" property must refer to a valid DB connection.'); } - $this->tablePrefix = Yii::$app->{$this->connectionId}->tablePrefix; if (($pos = strrpos($this->tableName, '.')) !== false) { $schema = substr($this->tableName, 0, $pos); @@ -95,25 +95,16 @@ class Generator extends \yii\gii\Generator $schema = ''; $tableName = $this->tableName; } - if ($tableName[strlen($tableName) - 1] === '*') { - $tables = Yii::$app->{$this->connectionId}->schema->getTables($schema); - if ($this->tablePrefix != '') { - foreach ($tables as $i => $table) { - if (strpos($table->name, $this->tablePrefix) !== 0) { - unset($tables[$i]); - } - } - } + if (strpos($tableName, '*') !== false) { + $tables = $db->getSchema()->getTableSchemas($schema); } else { - $tables = array($this->getTableSchema($this->tableName)); + $tables = array($db->getTableSchema($this->tableName, true)); } - $this->files = array(); - $templatePath = $this->templatePath; - $this->relations = $this->generateRelations(); + $files = array(); + $relations = $this->generateRelations(); foreach ($tables as $table) { - $tableName = $this->removePrefix($table->name); $className = $this->generateClassName($table->name); $params = array( 'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName, @@ -122,13 +113,14 @@ class Generator extends \yii\gii\Generator 'labels' => $this->generateLabels($table), 'rules' => $this->generateRules($table), 'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(), - 'connectionId' => $this->connectionId, ); - $this->files[] = new CCodeFile( - Yii::getPathOfAlias($this->modelPath) . '/' . $className . '.php', - $this->render($templatePath . '/model.php', $params) + $files[] = new CodeFile( + Yii::getAlias($this->modelPath) . '/' . $className . '.php', + $this->render('model.php', $params) ); } + + return $files; } public function validateTableName($attribute, $params) @@ -195,13 +187,6 @@ class Generator extends \yii\gii\Generator } } - public function validateModelPath($attribute, $params) - { - if (Yii::getPathOfAlias($this->modelPath) === false) { - $this->addError('modelPath', 'Model Path must be a valid path alias.'); - } - } - public function validateBaseClass($attribute, $params) { $class = @Yii::import($this->baseClass, true); diff --git a/framework/yii/gii/generators/model/form.php b/framework/yii/gii/generators/model/form.php new file mode 100644 index 0000000..d5ffd0b --- /dev/null +++ b/framework/yii/gii/generators/model/form.php @@ -0,0 +1,12 @@ +field($generator, 'tableName'); +echo $form->field($generator, 'modelClass'); +echo $form->field($generator, 'baseClass'); +echo $form->field($generator, 'buildRelations')->checkbox(); +echo $form->field($generator, 'commentsAsLabels')->checkbox(); diff --git a/framework/yii/gii/generators/model/views/form.php b/framework/yii/gii/generators/model/views/form.php deleted file mode 100644 index d5ffd0b..0000000 --- a/framework/yii/gii/generators/model/views/form.php +++ /dev/null @@ -1,12 +0,0 @@ -field($generator, 'tableName'); -echo $form->field($generator, 'modelClass'); -echo $form->field($generator, 'baseClass'); -echo $form->field($generator, 'buildRelations')->checkbox(); -echo $form->field($generator, 'commentsAsLabels')->checkbox(); diff --git a/framework/yii/gii/generators/module/Generator.php b/framework/yii/gii/generators/module/Generator.php index 1955ec5..52fbfe4 100644 --- a/framework/yii/gii/generators/module/Generator.php +++ b/framework/yii/gii/generators/module/Generator.php @@ -123,18 +123,17 @@ EOD; { $files = array(); $modulePath = $this->getModulePath(); - $templatePath = $this->getTemplatePath(); $files[] = new CodeFile( $modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php', - $this->render("$templatePath/module.php") + $this->render("module.php") ); $files[] = new CodeFile( $modulePath . '/controllers/DefaultController.php', - $this->render("$templatePath/controller.php") + $this->render("controller.php") ); $files[] = new CodeFile( $modulePath . '/views/default/index.php', - $this->render("$templatePath/view.php") + $this->render("view.php") ); return $files; diff --git a/framework/yii/gii/generators/module/form.php b/framework/yii/gii/generators/module/form.php new file mode 100644 index 0000000..8a0cc88 --- /dev/null +++ b/framework/yii/gii/generators/module/form.php @@ -0,0 +1,13 @@ + +
+field($generator, 'moduleClass'); + echo $form->field($generator, 'moduleID'); +?> +
diff --git a/framework/yii/gii/generators/module/views/form.php b/framework/yii/gii/generators/module/views/form.php deleted file mode 100644 index 8a0cc88..0000000 --- a/framework/yii/gii/generators/module/views/form.php +++ /dev/null @@ -1,13 +0,0 @@ - -
-field($generator, 'moduleClass'); - echo $form->field($generator, 'moduleID'); -?> -