Generator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2018-10-08 22:17
  7. */
  8. namespace backend\components\gii\crud;
  9. use Yii;
  10. use ReflectionClass;
  11. use yii\gii\CodeFile;
  12. use yii\helpers\StringHelper;
  13. class Generator extends \yii\gii\generators\crud\Generator
  14. {
  15. /**
  16. * @param string $attribute
  17. * @return string
  18. */
  19. public function generateActiveSearchField($attribute)
  20. {
  21. $tableSchema = $this->getTableSchema();
  22. if ($tableSchema === false) {
  23. return "\$form->field(\$model, '$attribute', ['labelOptions'=>['class'=>'col-sm-4 control-label'], 'size'=>8, 'options'=>['class'=>'col-sm-3']])";
  24. }
  25. $column = $tableSchema->columns[$attribute];
  26. if ($column->phpType === 'boolean') {
  27. return "\$form->field(\$model, '$attribute')->checkbox()";
  28. }
  29. return "\$form->field(\$model, '$attribute', ['labelOptions'=>['class'=>'col-sm-4 control-label'], 'size'=>8, 'options'=>['class'=>'col-sm-3']])";
  30. }
  31. public function formView()
  32. {
  33. $class = new ReflectionClass(\yii\gii\generators\crud\Generator::className());
  34. return dirname($class->getFileName()) . '/form.php';
  35. }
  36. public function generate()
  37. {
  38. $controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
  39. $files = [
  40. new CodeFile($controllerFile, $this->render('controller.php')),
  41. ];
  42. if (!empty($this->searchModelClass)) {
  43. $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php'));
  44. $files[] = new CodeFile($searchModel, $this->render('search.php'));
  45. }
  46. $modelClass = StringHelper::basename($this->modelClass);
  47. $files[] = new CodeFile(Yii::getAlias("@common/services/") . $modelClass . 'ServiceInterface.php', $this->render("serviceInterface.php"));
  48. $files[] = new CodeFile(Yii::getAlias("@common/services/") . $modelClass . 'Service.php', $this->render("service.php"));
  49. $viewPath = $this->getViewPath();
  50. $templatePath = $this->getTemplatePath() . '/views';
  51. foreach (scandir($templatePath) as $file) {
  52. if (empty($this->searchModelClass) && $file === '_search.php') {
  53. continue;
  54. }
  55. if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
  56. $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file"));
  57. }
  58. }
  59. $type = Yii::$app->getRequest()->post("generate");
  60. if( $type !== null ){
  61. $services = require Yii::getAlias("@common/config/") . 'services.php';
  62. $key = $modelClass . "Service";
  63. if( !isset($services[$key]) ) {
  64. $str = file_get_contents(Yii::getAlias("@common/config/") . 'services.php');
  65. $lines = explode("\n", $str);
  66. foreach ($lines as $key => $line) {
  67. $line = trim($line);
  68. if (empty($line)) {
  69. unset($lines[$key]);
  70. }
  71. }
  72. $temp[] = " \\common\services\\" . $modelClass . "ServiceInterface::ServiceName=>[";
  73. $temp[] = " 'class' => \\common\services\\" . $modelClass . "Service::className(),";
  74. $temp[] = " ],";
  75. array_splice($lines, count($lines) - 1, 0, $temp);
  76. file_put_contents(Yii::getAlias("@common/config/") . 'services.php', implode("\n", $lines));
  77. }
  78. }
  79. return $files;
  80. }
  81. }