Browse Source

refactored gii.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
8ea5858d0d
  1. 11
      framework/yii/gii/Generator.php
  2. 6
      framework/yii/gii/generators/controller/Generator.php
  3. 0
      framework/yii/gii/generators/controller/form.php
  4. 4
      framework/yii/gii/generators/form/Generator.php
  5. 0
      framework/yii/gii/generators/form/form.php
  6. 53
      framework/yii/gii/generators/model/Generator.php
  7. 0
      framework/yii/gii/generators/model/form.php
  8. 7
      framework/yii/gii/generators/module/Generator.php
  9. 0
      framework/yii/gii/generators/module/form.php

11
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. * 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 * The default implementation will return the "form.php" file under the directory
* directory containing the generator class file. * that contains the generator class file.
* @return string the view file for the input form of the generator. * @return string the view file for the input form of the generator.
*/ */
public function formView() public function formView()
{ {
$class = new ReflectionClass($this); $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. * Generates code using the specified code template and parameters.
* Note that the code template will be used as a PHP file. * 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. * @param array $params list of parameters to be passed to the template file.
* @return string the generated code * @return string the generated code
*/ */
@ -269,7 +270,7 @@ abstract class Generator extends Model
{ {
$view = new View; $view = new View;
$params['generator'] = $this; $params['generator'] = $this;
return $view->renderFile($template, $params, $this); return $view->renderFile($this->getTemplatePath() . '/' . $template, $params, $this);
} }
/** /**

6
framework/yii/gii/generators/controller/Generator.php

@ -147,17 +147,15 @@ class Generator extends \yii\gii\Generator
{ {
$files = array(); $files = array();
$templatePath = $this->getTemplatePath();
$files[] = new CodeFile( $files[] = new CodeFile(
$this->getControllerFile(), $this->getControllerFile(),
$this->render($templatePath . '/controller.php') $this->render('controller.php')
); );
foreach ($this->getActionIDs() as $action) { foreach ($this->getActionIDs() as $action) {
$files[] = new CodeFile( $files[] = new CodeFile(
$this->getViewFile($action), $this->getViewFile($action),
$this->render($templatePath . '/view.php', array('action' => $action)) $this->render('view.php', array('action' => $action))
); );
} }

0
framework/yii/gii/generators/controller/views/form.php → framework/yii/gii/generators/controller/form.php

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

@ -49,7 +49,7 @@ class Generator extends \yii\gii\Generator
$files = array(); $files = array();
$files[] = new CodeFile( $files[] = new CodeFile(
Yii::getAlias($this->viewPath) . '/' . $this->viewName . '.php', Yii::getAlias($this->viewPath) . '/' . $this->viewName . '.php',
$this->render($this->getTemplatePath() . '/form.php') $this->render('form.php')
); );
return $files; return $files;
} }
@ -121,7 +121,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function successMessage() public function successMessage()
{ {
$code = highlight_string($this->render($this->getTemplatePath() . '/action.php'), true); $code = highlight_string($this->render('action.php'), true);
return <<<EOD return <<<EOD
<p>The form has been generated successfully.</p> <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> <p>You may add the following code in an appropriate controller class to invoke the view:</p>

0
framework/yii/gii/generators/form/views/form.php → framework/yii/gii/generators/form/form.php

53
framework/yii/gii/generators/model/Generator.php

@ -8,6 +8,8 @@
namespace yii\gii\generators\model; namespace yii\gii\generators\model;
use Yii; use Yii;
use yii\base\InvalidConfigException;
use yii\gii\CodeFile;
/** /**
* *
@ -36,16 +38,15 @@ class Generator extends \yii\gii\Generator
public function getDescription() 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() public function rules()
{ {
return array_merge(parent::rules(), array( return array_merge(parent::rules(), array(
array('tablePrefix, baseClass, tableName, modelClass, modelPath, connectionId', 'filter', 'filter' => 'trim'), 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('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('tableName', 'validateTableName'),
array('tablePrefix, modelClass', 'match', 'pattern' => '/^[a-zA-Z_]\w*$/', 'message' => '{attribute} should only contain word characters.'), 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.'), 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() public function attributeLabels()
{ {
return array_merge(parent::attributeLabels(), array( return array(
'tablePrefix' => 'Table Prefix', 'tablePrefix' => 'Table Prefix',
'tableName' => 'Table Name', 'tableName' => 'Table Name',
'modelPath' => 'Model Path', 'modelPath' => 'Model Path',
@ -66,7 +67,7 @@ class Generator extends \yii\gii\Generator
'buildRelations' => 'Build Relations', 'buildRelations' => 'Build Relations',
'commentsAsLabels' => 'Use Column Comments as Attribute Labels', 'commentsAsLabels' => 'Use Column Comments as Attribute Labels',
'connectionId' => 'Database Connection', 'connectionId' => 'Database Connection',
)); );
} }
public function requiredTemplates() public function requiredTemplates()
@ -78,15 +79,14 @@ class Generator extends \yii\gii\Generator
public function stickyAttributes() public function stickyAttributes()
{ {
return array('connectionId', 'tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels'); return array('tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels');
} }
public function generate() public function generate()
{ {
if (Yii::$app->{$this->connectionId} === null) { if (($db = Yii::$app->{$this->db}) === null) {
throw new CHttpException(500, 'A valid database connection is required to run this generator.'); 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) { if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos); $schema = substr($this->tableName, 0, $pos);
@ -95,25 +95,16 @@ class Generator extends \yii\gii\Generator
$schema = ''; $schema = '';
$tableName = $this->tableName; $tableName = $this->tableName;
} }
if ($tableName[strlen($tableName) - 1] === '*') { if (strpos($tableName, '*') !== false) {
$tables = Yii::$app->{$this->connectionId}->schema->getTables($schema); $tables = $db->getSchema()->getTableSchemas($schema);
if ($this->tablePrefix != '') {
foreach ($tables as $i => $table) {
if (strpos($table->name, $this->tablePrefix) !== 0) {
unset($tables[$i]);
}
}
}
} else { } else {
$tables = array($this->getTableSchema($this->tableName)); $tables = array($db->getTableSchema($this->tableName, true));
} }
$this->files = array(); $files = array();
$templatePath = $this->templatePath; $relations = $this->generateRelations();
$this->relations = $this->generateRelations();
foreach ($tables as $table) { foreach ($tables as $table) {
$tableName = $this->removePrefix($table->name);
$className = $this->generateClassName($table->name); $className = $this->generateClassName($table->name);
$params = array( $params = array(
'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName, 'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName,
@ -122,13 +113,14 @@ class Generator extends \yii\gii\Generator
'labels' => $this->generateLabels($table), 'labels' => $this->generateLabels($table),
'rules' => $this->generateRules($table), 'rules' => $this->generateRules($table),
'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(), 'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(),
'connectionId' => $this->connectionId,
); );
$this->files[] = new CCodeFile( $files[] = new CodeFile(
Yii::getPathOfAlias($this->modelPath) . '/' . $className . '.php', Yii::getAlias($this->modelPath) . '/' . $className . '.php',
$this->render($templatePath . '/model.php', $params) $this->render('model.php', $params)
); );
} }
return $files;
} }
public function validateTableName($attribute, $params) 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) public function validateBaseClass($attribute, $params)
{ {
$class = @Yii::import($this->baseClass, true); $class = @Yii::import($this->baseClass, true);

0
framework/yii/gii/generators/model/views/form.php → framework/yii/gii/generators/model/form.php

7
framework/yii/gii/generators/module/Generator.php

@ -123,18 +123,17 @@ EOD;
{ {
$files = array(); $files = array();
$modulePath = $this->getModulePath(); $modulePath = $this->getModulePath();
$templatePath = $this->getTemplatePath();
$files[] = new CodeFile( $files[] = new CodeFile(
$modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php', $modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php',
$this->render("$templatePath/module.php") $this->render("module.php")
); );
$files[] = new CodeFile( $files[] = new CodeFile(
$modulePath . '/controllers/DefaultController.php', $modulePath . '/controllers/DefaultController.php',
$this->render("$templatePath/controller.php") $this->render("controller.php")
); );
$files[] = new CodeFile( $files[] = new CodeFile(
$modulePath . '/views/default/index.php', $modulePath . '/views/default/index.php',
$this->render("$templatePath/view.php") $this->render("view.php")
); );
return $files; return $files;

0
framework/yii/gii/generators/module/views/form.php → framework/yii/gii/generators/module/form.php

Loading…
Cancel
Save