createModule.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\File;
  5. define("ROOT_PATH",dirname(dirname(dirname(__DIR__))));
  6. class createModule extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'module:create {name} {module?} {app?}';
  14. protected $root=ROOT_PATH;
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'create module or model or controller or lib ';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $proj=$this->argument('name');
  38. switch($proj){
  39. case "module":
  40. $this->cdir();
  41. break;
  42. case "controller":
  43. $this->cController();
  44. break;
  45. case "model":
  46. $this->cModel();
  47. break;
  48. case "lib":
  49. $this->cLib();
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. protected function cLib(){
  56. $app=$this->argument('module');
  57. $app=ucfirst($app);
  58. $module=$this->root."/module/";
  59. $path=$module."/Lib"."/";
  60. if(!File::exists($path)){
  61. File::makeDirectory($path);
  62. }
  63. $path=$module."/Lib"."/".ucfirst($app).".php";
  64. $content="<?php ".chr(9).chr(13).
  65. "namespace Module\\Lib;".chr(9).chr(13).
  66. "class ".ucfirst($app)." {".chr(9).chr(13).
  67. "}";
  68. File::put($path,$content);
  69. }
  70. protected function cController()
  71. {
  72. $proj=$this->argument('module');
  73. $app=$this->argument('app');
  74. $proj=ucfirst($proj);
  75. $module=$this->root."/module/".$proj."/";
  76. $path=$module."/Controller"."/".ucfirst($app).".php";
  77. $content="<?php ".chr(9).chr(13).
  78. "namespace Module\\$proj\Controller;".chr(9).chr(13).
  79. "class ".ucfirst($app)." extends BaseController{".chr(9).chr(13).
  80. " function _init(){".chr(9).chr(13).
  81. " }".chr(9).chr(13).
  82. "}";
  83. File::put($path,$content);
  84. }
  85. protected function cBaseController()
  86. {
  87. $proj=$this->argument('module');
  88. $proj=ucfirst($proj);
  89. $module=$this->root."/module/".$proj."/";
  90. $path=$module."/Controller"."/BaseController.php";
  91. $content="<?php".chr(9).chr(13).
  92. "namespace Module\\$proj\Controller;".chr(9).chr(13).
  93. "class BaseController extends \\Module\\Controller{".chr(9).chr(13).
  94. " function _init(){".chr(9).chr(13).
  95. " }".chr(9).chr(13).
  96. " function __construct(){".chr(9).chr(13).
  97. " \$this->_init();".chr(9).chr(13).
  98. " }".chr(9).chr(13).
  99. "}";
  100. File::put($path,$content);
  101. }
  102. protected function cBaseModel()
  103. {
  104. $proj=$this->argument('module');
  105. $proj=ucfirst($proj);
  106. $module=$this->root."/module/".$proj."/";
  107. $path=$module."/Model"."/BaseModel.php";
  108. $content="<?php".chr(9).chr(13).
  109. "namespace Module\\$proj\Model;".chr(9).chr(13).
  110. "class BaseModel extends \Illuminate\Database\Eloquent\Model{".chr(9).chr(13).
  111. "}";
  112. File::put($path,$content);
  113. }
  114. protected function cModel()
  115. {
  116. $proj=$this->argument('module');
  117. $model=$this->argument('app');
  118. $proj=ucfirst($proj);
  119. $model=ucfirst($model);
  120. $module=$this->root."/module/".$proj."/";
  121. $path=$module."/Model"."/$model.php";
  122. $content="<?php".chr(9).chr(13).
  123. "namespace Module\\$proj\Model;".chr(9).chr(13).
  124. "class $model extends BaseModel{".chr(9).chr(13).
  125. "}";
  126. File::put($path,$content);
  127. }
  128. protected function cdir()
  129. {
  130. $proj=$this->argument('module');
  131. $proj=ucfirst($proj);
  132. $module=$this->root."/module/".$proj."/";
  133. File::makeDirectory($module);
  134. $path=$module."/Model"."/";
  135. File::makeDirectory($path);
  136. $path=$module."/Controller"."/";
  137. File::makeDirectory($path);
  138. $path=$this->root."/resources/views/".lcfirst($proj)."/";
  139. File::makeDirectory($path);
  140. $this->cBaseController();
  141. $this->cBaseModel();
  142. }
  143. }