diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index a5d2c1d..0d0dd89 100644 --- a/framework/yii/base/Application.php +++ b/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) { diff --git a/framework/yii/gii/assets/gii.js b/framework/yii/gii/assets/gii.js index d91be6d..ac701e0 100644 --- a/framework/yii/gii/assets/gii.js +++ b/framework/yii/gii/assets/gii.js @@ -28,7 +28,7 @@ yii.gii = (function ($) { } $(this).before('
' + value + '
').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); diff --git a/framework/yii/gii/assets/main.css b/framework/yii/gii/assets/main.css index dc0c618..f69d57f 100644 --- a/framework/yii/gii/assets/main.css +++ b/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; diff --git a/framework/yii/gii/generators/controller/templates/controller.php b/framework/yii/gii/generators/controller/templates/controller.php index fba6566..3829d54 100644 --- a/framework/yii/gii/generators/controller/templates/controller.php +++ b/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 " - ns)): ?> namespace ns; ?>; diff --git a/framework/yii/gii/generators/controller/templates/view.php b/framework/yii/gii/generators/controller/templates/view.php index d939f1f..4b75d7a 100644 --- a/framework/yii/gii/generators/controller/templates/view.php +++ b/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 " - /** * @var yii\base\View $this */ diff --git a/framework/yii/gii/generators/controller/views/form.php b/framework/yii/gii/generators/controller/views/form.php index 845d7e7..e4d2947 100644 --- a/framework/yii/gii/generators/controller/views/form.php +++ b/framework/yii/gii/generators/controller/views/form.php @@ -4,8 +4,7 @@ * @var yii\widgets\ActiveForm $form * @var yii\gii\generators\controller\Generator $generator */ -?> -field($generator, 'controller'); ?> -field($generator, 'actions'); ?> -field($generator, 'ns'); ?> -field($generator, 'baseClass'); ?> +echo $form->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 e02a98e..70420f6 100644 --- a/framework/yii/gii/generators/form/Generator.php +++ b/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 * @since 2.0 @@ -124,9 +125,7 @@ class Generator extends \yii\gii\Generator return <<The form has been generated successfully.

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

-
-$code
-
+
$code
EOD; } diff --git a/framework/yii/gii/generators/form/templates/action.php b/framework/yii/gii/generators/form/templates/action.php index 3884b02..fc5830e 100644 --- a/framework/yii/gii/generators/form/templates/action.php +++ b/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 " - + public function actionviewName), '_')); ?>() { $model = new modelClass; ?>scenarioName) ? '' : "(array('scenario' => '{$generator->scenarioName}'))"; ?>; diff --git a/framework/yii/gii/generators/form/templates/form.php b/framework/yii/gii/generators/form/templates/form.php index 53cf5e6..0635746 100644 --- a/framework/yii/gii/generators/form/templates/form.php +++ b/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 " - use yii\helpers\Html; use yii\widgets\ActiveForm; diff --git a/framework/yii/gii/generators/form/views/form.php b/framework/yii/gii/generators/form/views/form.php index f276688..c04a26e 100644 --- a/framework/yii/gii/generators/form/views/form.php +++ b/framework/yii/gii/generators/form/views/form.php @@ -4,8 +4,7 @@ * @var yii\widgets\ActiveForm $form * @var yii\gii\generators\form\Generator $generator */ -?> -field($generator, 'viewName'); ?> -field($generator, 'modelClass'); ?> -field($generator, 'scenarioName'); ?> -field($generator, 'viewPath'); ?> +echo $form->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/module/Generator.php b/framework/yii/gii/generators/module/Generator.php index bf47b2b..1955ec5 100644 --- a/framework/yii/gii/generators/module/Generator.php +++ b/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 * @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., admin.', + 'moduleClass' => 'This is the fully qualified class name of the module, e.g., app\modules\admin\Module.', + ); + } + + /** + * @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 = <<The module has been generated successfully.

+

To access the module, you need to modify the application configuration as follows:

+EOD; + $code = <<array( + '{$this->moduleID}' => array( + 'class' => '{$this->moduleClass}', + ), + ), + ...... +); +EOD; + + return $output . '
' . highlight_string($code, true) . '
'; + } + + /** + * @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'; } } diff --git a/framework/yii/gii/generators/module/templates/controller.php b/framework/yii/gii/generators/module/templates/controller.php new file mode 100644 index 0000000..34dd961 --- /dev/null +++ b/framework/yii/gii/generators/module/templates/controller.php @@ -0,0 +1,21 @@ + + +namespace getControllerNamespace(); ?>; + +use yii\web\Controller; + +class DefaultController extends Controller +{ + public function actionIndex() + { + $this->render('index'); + } +} diff --git a/framework/yii/gii/generators/module/templates/module.php b/framework/yii/gii/generators/module/templates/module.php new file mode 100644 index 0000000..8841473 --- /dev/null +++ b/framework/yii/gii/generators/module/templates/module.php @@ -0,0 +1,29 @@ +moduleClass; +$pos = strrpos($className, '\\'); +$ns = ltrim(substr($className, 0, $pos), '\\'); +$className = substr($className, $pos + 1); + +echo " + +namespace + + +class extends \yii\web\Module +{ + public $controllerNamespace = 'getControllerNamespace(); ?>'; + + public function init() + { + parent::init(); + + // custom initialization code goes here + } +} diff --git a/framework/yii/gii/generators/module/templates/view.php b/framework/yii/gii/generators/module/templates/view.php new file mode 100644 index 0000000..d0e1ce6 --- /dev/null +++ b/framework/yii/gii/generators/module/templates/view.php @@ -0,0 +1,18 @@ + +
+

echo $this->context->action->uniqueId; ?>

+

+ This is the view content for action " echo $this->context->action->id; ?>". + The action belongs to the controller " echo get_class($this->context); ?>" + in the " echo $this->context->module->id; ?>" module. +

+

+ You may customize this page by editing the following file:
+ echo __FILE__; ?> +

+
diff --git a/framework/yii/gii/generators/module/views/form.php b/framework/yii/gii/generators/module/views/form.php new file mode 100644 index 0000000..8a0cc88 --- /dev/null +++ b/framework/yii/gii/generators/module/views/form.php @@ -0,0 +1,13 @@ + +
+field($generator, 'moduleClass'); + echo $form->field($generator, 'moduleID'); +?> +
diff --git a/framework/yii/gii/views/default/view.php b/framework/yii/gii/views/default/view.php index 50a87fc..821f6fc 100644 --- a/framework/yii/gii/views/default/view.php +++ b/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