From f99a12b079c89723bb39946736b154f644d2faaa Mon Sep 17 00:00:00 2001 From: blobt <380255922@qq.com> Date: Sun, 6 Oct 2019 22:23:47 +0800 Subject: [PATCH] =?UTF-8?q?generator=E5=BC=80=E5=A7=8B=E7=BC=96=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vendor/blobt/generators/crud/Generator.php | 186 ++++++++++++++++++ .../generators/crud/default/views/_search.php | 50 +++-- .../generators/crud/default/views/index.php | 100 +++------- 3 files changed, 237 insertions(+), 99 deletions(-) create mode 100644 vendor/blobt/generators/crud/Generator.php diff --git a/vendor/blobt/generators/crud/Generator.php b/vendor/blobt/generators/crud/Generator.php new file mode 100644 index 0000000..e306536 --- /dev/null +++ b/vendor/blobt/generators/crud/Generator.php @@ -0,0 +1,186 @@ +controllerClass, '\\')) . '.php'); + + $files = [ + new CodeFile($controllerFile, $this->render('controller.php')), + ]; + + if (!empty($this->searchModelClass)) { + $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php')); + $files[] = new CodeFile($searchModel, $this->render('search.php')); + } + + $viewPath = $this->getViewPath(); + $templatePath = $this->getTemplatePath() . '/views'; + foreach (scandir($templatePath) as $file) { + if (empty($this->searchModelClass) && $file === '_search.php') { + continue; + } + if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { + $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file")); + } + } + + return $files; + } + + /** + * @return string the controller ID (without the module ID prefix) + */ + public function getControllerID() { + $pos = strrpos($this->controllerClass, '\\'); + $class = substr(substr($this->controllerClass, $pos + 1), 0, -10); + + return Inflector::camel2id($class, '-', $this->strictInflector); + } + + /** + * @return string the controller view path + */ + public function getViewPath() { + if (empty($this->viewPath)) { + return Yii::getAlias('@app/views/' . $this->getControllerID()); + } + + return Yii::getAlias(str_replace('\\', '/', $this->viewPath)); + } + + /** + * @return string + */ + public function getNameAttribute() { + foreach ($this->getColumnNames() as $name) { + if (!strcasecmp($name, 'name') || !strcasecmp($name, 'title')) { + return $name; + } + } + /* @var $class \yii\db\ActiveRecord */ + $class = $this->modelClass; + $pk = $class::primaryKey(); + + return $pk[0]; + } + + /** + * Generates code for active field + * @param string $attribute + * @return string + */ + public function generateActiveField($attribute) { + $tableSchema = $this->getTableSchema(); + if ($tableSchema === false || !isset($tableSchema->columns[$attribute])) { + if (preg_match('/^(password|pass|passwd|passcode)$/i', $attribute)) { + return "\$form->field(\$model, '$attribute')->passwordInput()"; + } + + return "\$form->field(\$model, '$attribute')"; + } + $column = $tableSchema->columns[$attribute]; + if ($column->phpType === 'boolean') { + return "\$form->field(\$model, '$attribute')->checkbox()"; + } + + if ($column->type === 'text') { + return "\$form->field(\$model, '$attribute')->textarea(['rows' => 6])"; + } + + if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) { + $input = 'passwordInput'; + } else { + $input = 'textInput'; + } + + if (is_array($column->enumValues) && count($column->enumValues) > 0) { + $dropDownOptions = []; + foreach ($column->enumValues as $enumValue) { + $dropDownOptions[$enumValue] = Inflector::humanize($enumValue); + } + return "\$form->field(\$model, '$attribute')->dropDownList(" + . preg_replace("/\n\s*/", ' ', VarDumper::export($dropDownOptions)) . ", ['prompt' => ''])"; + } + + if ($column->phpType !== 'string' || $column->size === null) { + return "\$form->field(\$model, '$attribute')->$input()"; + } + + return "\$form->field(\$model, '$attribute')->$input(['maxlength' => true])"; + } + +} diff --git a/vendor/blobt/generators/crud/default/views/_search.php b/vendor/blobt/generators/crud/default/views/_search.php index b65231f..e7efb17 100644 --- a/vendor/blobt/generators/crud/default/views/_search.php +++ b/vendor/blobt/generators/crud/default/views/_search.php @@ -17,33 +17,29 @@ use yii\widgets\ActiveForm; /* @var $form yii\widgets\ActiveForm */ ?> -
-= "= " ?>Html::a(= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'], ['class' => 'btn btn-success']) ?> -
- - = $generator->enablePjax ? " \n" : '' ?> - searchModelClass)): ?> - = " indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?> - - - indexWidgetType === 'grid'): ?> - = "= " ?>GridView::widget([ - 'dataProvider' => $dataProvider, - = !empty($generator->searchModelClass) ? "'filterModel' => \$searchModel,\n 'columns' => [\n" : "'columns' => [\n"; ?> - ['class' => 'yii\grid\SerialColumn'], - - getTableSchema()) === false) { - foreach ($generator->getColumnNames() as $name) { - if (++$count < 6) { - echo " '" . $name . "',\n"; - } else { - echo " //'" . $name . "',\n"; - } - } +getColumnNames() as $name) { + if (++$count < 6) { + echo " '" . $name . "',\n"; } else { - foreach ($tableSchema->columns as $column) { - $format = $generator->generateColumnFormat($column); - if (++$count < 6) { - echo " '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; - } else { - echo " //'" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; - } - } + echo " //'" . $name . "',\n"; } - ?> - - ['class' => 'yii\grid\ActionColumn'], - ], - ]); ?> - - = "= " ?>ListView::widget([ - 'dataProvider' => $dataProvider, - 'itemOptions' => ['class' => 'item'], - 'itemView' => function ($model, $key, $index, $widget) { - return Html::a(Html::encode($model->= $nameAttribute ?>), ['view', = $urlParams ?>]); - }, - ]) ?> - - -= $generator->enablePjax ? " \n" : '' ?> - -