Browse Source

finished module code generator.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
3852e0b7cb
  1. 3
      framework/yii/base/Application.php
  2. 13
      framework/yii/gii/assets/gii.js
  3. 5
      framework/yii/gii/assets/main.css
  4. 3
      framework/yii/gii/generators/controller/templates/controller.php
  5. 3
      framework/yii/gii/generators/controller/templates/view.php
  6. 9
      framework/yii/gii/generators/controller/views/form.php
  7. 5
      framework/yii/gii/generators/form/Generator.php
  8. 4
      framework/yii/gii/generators/form/templates/action.php
  9. 3
      framework/yii/gii/generators/form/templates/form.php
  10. 9
      framework/yii/gii/generators/form/views/form.php
  11. 136
      framework/yii/gii/generators/module/Generator.php
  12. 21
      framework/yii/gii/generators/module/templates/controller.php
  13. 29
      framework/yii/gii/generators/module/templates/module.php
  14. 18
      framework/yii/gii/generators/module/templates/view.php
  15. 13
      framework/yii/gii/generators/module/views/form.php
  16. 1
      framework/yii/gii/views/default/view.php

3
framework/yii/base/Application.php

@ -169,7 +169,7 @@ abstract class Application extends Module
public function registerErrorHandlers()
{
if (YII_ENABLE_ERROR_HANDLER) {
//ini_set('display_errors', 0);
ini_set('display_errors', 0);
set_exception_handler(array($this, 'handleException'));
set_error_handler(array($this, 'handleError'), error_reporting());
if ($this->memoryReserveSize > 0) {
@ -419,7 +419,6 @@ abstract class Application extends Module
// disable error capturing to avoid recursive errors while handling exceptions
restore_error_handler();
restore_exception_handler();
try {
$this->logException($exception);
if (($handler = $this->getErrorHandler()) !== null) {

13
framework/yii/gii/assets/gii.js

@ -28,7 +28,7 @@ yii.gii = (function ($) {
}
$(this).before('<div class="sticky-value">' + value + '</div>').hide();
});
$('.sticky-value').on('click', function() {
$('.sticky-value').on('click', function () {
$(this).hide();
$(this).next().show().get(0).focus();
});
@ -76,10 +76,19 @@ yii.gii = (function ($) {
initPreviewDiffLinks();
initConfirmationCheckboxes();
$('.default-view .form-group input,select,textarea').change(function(){
// hide Generate button if any input is changed
$('.default-view .form-group input,select,textarea').change(function () {
$('.default-view-results,.default-view-files').hide();
$('.default-view button[name="generate"]').hide();
});
$('.module-form #generator-moduleclass').change(function () {
var value = $(this).val().match(/(\w+)\\\w+$/);
var $idInput = $('#generator-moduleid');
if (value && value[1] && $idInput.val() == '') {
$idInput.val(value[1]);
}
});
}
};
})(jQuery);

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

@ -45,6 +45,7 @@ body {
.default-view .sticky-value {
padding: 6px 12px;
background: lightyellow;
overflow: auto;
}
.default-view .modal-dialog {
@ -99,6 +100,10 @@ body {
padding: 1px;
}
.default-view-results .alert pre {
background: white;
}
.default-diff pre {
padding: 0;
margin: 0;

3
framework/yii/gii/generators/controller/templates/controller.php

@ -8,8 +8,9 @@ use yii\helpers\Inflector;
* @var yii\base\View $this
* @var yii\gii\generators\controller\Generator $generator
*/
echo "<?php\n";
?>
<?php echo "<?php\n"; ?>
<?php if (!empty($generator->ns)): ?>
namespace <?php echo $generator->ns; ?>;

3
framework/yii/gii/generators/controller/templates/view.php

@ -6,8 +6,9 @@
* @var yii\gii\generators\controller\Generator $generator
* @var string $action the action ID
*/
echo "<?php\n";
?>
<?php echo "<?php\n"; ?>
/**
* @var yii\base\View $this
*/

9
framework/yii/gii/generators/controller/views/form.php

@ -4,8 +4,7 @@
* @var yii\widgets\ActiveForm $form
* @var yii\gii\generators\controller\Generator $generator
*/
?>
<?php echo $form->field($generator, 'controller'); ?>
<?php echo $form->field($generator, 'actions'); ?>
<?php echo $form->field($generator, 'ns'); ?>
<?php echo $form->field($generator, 'baseClass'); ?>
echo $form->field($generator, 'controller');
echo $form->field($generator, 'actions');
echo $form->field($generator, 'ns');
echo $form->field($generator, 'baseClass');

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

@ -12,6 +12,7 @@ use yii\base\Model;
use yii\gii\CodeFile;
/**
* This generator will generate an action view file based on the specified model class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
@ -124,9 +125,7 @@ class Generator extends \yii\gii\Generator
return <<<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>
<pre style="background:white">
$code
</pre>
<pre>$code</pre>
EOD;
}

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

@ -8,8 +8,10 @@ use yii\helpers\Inflector;
* @var yii\base\View $this
* @var yii\gii\generators\form\Generator $generator
*/
echo "<?php\n";
?>
<?php echo "<?php\n"; ?>
public function action<?php echo Inflector::id2camel(trim(basename($generator->viewName), '_')); ?>()
{
$model = new <?php echo $generator->modelClass; ?><?php echo empty($generator->scenarioName) ? '' : "(array('scenario' => '{$generator->scenarioName}'))"; ?>;

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

@ -5,8 +5,9 @@
* @var yii\base\View $this
* @var yii\gii\generators\form\Generator $generator
*/
echo "<?php\n";
?>
<?php echo "<?php\n"; ?>
use yii\helpers\Html;
use yii\widgets\ActiveForm;

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

@ -4,8 +4,7 @@
* @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'); ?>
echo $form->field($generator, 'viewName');
echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'scenarioName');
echo $form->field($generator, 'viewPath');

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

@ -7,18 +7,33 @@
namespace yii\gii\generators\module;
use Yii;
use yii\gii\CodeFile;
use yii\helpers\Html;
use yii\helpers\StringHelper;
/**
* This generator will generate the skeleton code needed by a module.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Generator extends \yii\gii\Generator
{
public $moduleClass;
public $moduleID;
/**
* @inheritdoc
*/
public function getName()
{
return 'Module Generator';
}
/**
* @inheritdoc
*/
public function getDescription()
{
return 'This generator helps you to generate the skeleton code needed by a Yii module.';
@ -27,8 +42,127 @@ class Generator extends \yii\gii\Generator
/**
* @inheritdoc
*/
public function rules()
{
return array_merge(parent::rules(), array(
array('moduleID, moduleClass', 'filter', 'filter' => 'trim'),
array('moduleID, moduleClass', 'required'),
array('moduleID', 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'),
array('moduleClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('moduleClass', 'validateModuleClass'),
));
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return array(
'moduleID' => 'Module ID',
'moduleClass' => 'Module Class',
);
}
/**
* @inheritdoc
*/
public function hints()
{
return array(
'moduleID' => 'This refers to the ID of the module, e.g., <code>admin</code>.',
'moduleClass' => 'This is the fully qualified class name of the module, e.g., <code>app\modules\admin\Module</code>.',
);
}
/**
* @inheritdoc
*/
public function successMessage()
{
if (Yii::$app->hasModule($this->moduleID)) {
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), array('target' => '_blank'));
return "The module has been generated successfully. You may $link.";
}
$output = <<<EOD
<p>The module has been generated successfully.</p>
<p>To access the module, you need to modify the application configuration as follows:</p>
EOD;
$code = <<<EOD
<?php
return array(
'modules'=>array(
'{$this->moduleID}' => array(
'class' => '{$this->moduleClass}',
),
),
......
);
EOD;
return $output . '<pre>' . highlight_string($code, true) . '</pre>';
}
/**
* @inheritdoc
*/
public function requiredTemplates()
{
return array(
'module.php',
'controller.php',
'view.php',
);
}
/**
* @inheritdoc
*/
public function generate()
{
return array();
$files = array();
$modulePath = $this->getModulePath();
$templatePath = $this->getTemplatePath();
$files[] = new CodeFile(
$modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php',
$this->render("$templatePath/module.php")
);
$files[] = new CodeFile(
$modulePath . '/controllers/DefaultController.php',
$this->render("$templatePath/controller.php")
);
$files[] = new CodeFile(
$modulePath . '/views/default/index.php',
$this->render("$templatePath/view.php")
);
return $files;
}
/**
* Validates [[moduleClass]] to make sure it is a fully qualified class name.
*/
public function validateModuleClass()
{
if (strpos($this->moduleClass, '\\') === false || Yii::getAlias('@' . str_replace('\\', '/', $this->moduleClass)) === false) {
$this->addError('moduleClass', 'Module class must be properly namespaced.');
}
}
/**
* @return boolean the directory that contains the module class
*/
public function getModulePath()
{
return Yii::getAlias('@' . str_replace('\\', '/', substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\'))));
}
/**
* @return string the controller namespace of the module.
*/
public function getControllerNamespace()
{
return substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\')) . '\controllers';
}
}

21
framework/yii/gii/generators/module/templates/controller.php

@ -0,0 +1,21 @@
<?php
/**
* This is the template for generating a controller class within a module.
*
* @var yii\base\View $this
* @var yii\gii\generators\module\Generator $generator
*/
echo "<?php\n";
?>
namespace <?php echo $generator->getControllerNamespace(); ?>;
use yii\web\Controller;
class DefaultController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
}

29
framework/yii/gii/generators/module/templates/module.php

@ -0,0 +1,29 @@
<?php
/**
* This is the template for generating a module class file.
*
* @var yii\base\View $this
* @var yii\gii\generators\module\Generator $generator
*/
$className = $generator->moduleClass;
$pos = strrpos($className, '\\');
$ns = ltrim(substr($className, 0, $pos), '\\');
$className = substr($className, $pos + 1);
echo "<?php\n";
?>
namespace <?php echo $ns; ?>
class <?php echo $className; ?> extends \yii\web\Module
{
public $controllerNamespace = '<?php echo $generator->getControllerNamespace(); ?>';
public function init()
{
parent::init();
// custom initialization code goes here
}
}

18
framework/yii/gii/generators/module/templates/view.php

@ -0,0 +1,18 @@
<?php
/**
* @var yii\base\View $this
* @var yii\gii\generators\module\Generator $generator
*/
?>
<div class="<?php echo $generator->moduleID . '-default-index'; ?>">
<h1><?php echo "<?php"; ?> echo $this->context->action->uniqueId; ?></h1>
<p>
This is the view content for action "<?php echo "<?php"; ?> echo $this->context->action->id; ?>".
The action belongs to the controller "<?php echo "<?php"; ?> echo get_class($this->context); ?>"
in the "<?php echo "<?php"; ?> echo $this->context->module->id; ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?php echo "<?php"; ?> echo __FILE__; ?></code>
</p>
</div>

13
framework/yii/gii/generators/module/views/form.php

@ -0,0 +1,13 @@
<?php
/**
* @var yii\base\View $this
* @var yii\widgets\ActiveForm $form
* @var yii\gii\generators\module\Generator $generator
*/
?>
<div class="module-form">
<?php
echo $form->field($generator, 'moduleClass');
echo $form->field($generator, 'moduleID');
?>
</div>

1
framework/yii/gii/views/default/view.php

@ -9,6 +9,7 @@ use yii\gii\CodeFile;
/**
* @var yii\base\View $this
* @var yii\gii\Generator $generator
* @var string $id
* @var yii\widgets\ActiveForm $form
* @var string $results
* @var boolean $hasError

Loading…
Cancel
Save