MakeServiceCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Traits\ServiceAppAop;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Str;
  6. class MakeServiceCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'make:service
  14. {--noProvider : Register in the provider}
  15. {name : fileName}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Create Service file';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. if (!file_exists(base_path('app\\Services'))){
  39. mkdir('app\\Services',666,false);
  40. }
  41. preg_match('/(.*?)Service/',$this->argument('name'),$resultNames);
  42. if(count($resultNames)==0) {
  43. $fileName = $this->argument('name');
  44. $modelName = $this->argument('name');
  45. }else{
  46. $fileName=$resultNames[0];
  47. $modelName=Str::studly($resultNames[1]);
  48. }
  49. if (!file_exists(base_path('app\\Services\\'.$fileName.'.php'))){
  50. file_put_contents(base_path('app\\Services\\'.$fileName.'.php'),
  51. '<?php '
  52. .PHP_EOL
  53. .PHP_EOL
  54. .'namespace App\Services;'
  55. .PHP_EOL
  56. .PHP_EOL
  57. .'use App\Traits\ServiceAppAop;'
  58. .PHP_EOL
  59. .'use App\\'.$modelName.';'
  60. .PHP_EOL
  61. .PHP_EOL
  62. .'class '.$fileName
  63. .PHP_EOL
  64. .'{'
  65. .PHP_EOL
  66. .' use ServiceAppAop;'
  67. .PHP_EOL
  68. .' protected $modelClass='.$modelName.'::class;'
  69. .PHP_EOL
  70. .PHP_EOL.
  71. '}');
  72. }
  73. else $this->error("ERROR: file exists;");
  74. if (!$this->option("noProvider")){
  75. $path = base_path("app\\Providers\\AppServiceProvider.php");
  76. $i = 0;
  77. $fop = fopen($path,"r+");
  78. $endLine = 0;
  79. $startLine = 0;
  80. $sign = 0;
  81. while (!feof($fop)){
  82. $i += 1;
  83. $lineStr = fgets($fop);
  84. if (strpos($lineStr,"loadingService(){"))$sign = $i;
  85. if (strpos($lineStr,"app()->singleton"))$endLine = $i;
  86. if (strpos($lineStr,"App\Services"))$startLine = $i;
  87. };
  88. fclose($fop);
  89. $fop = fopen($path,"r+");
  90. $i = 0;
  91. $header = [];
  92. $content = [];
  93. $footer = [];
  94. while (!feof($fop)){
  95. $i += 1;
  96. $lineStr = fgets($fop);
  97. if ($i == $startLine){
  98. $lineStr = $lineStr."use App\Services\\$fileName;".PHP_EOL;
  99. }
  100. if ($i<=$sign)$header[] = $lineStr;
  101. if ($i>$sign && $i<= $endLine){
  102. $content[] = $lineStr;
  103. }
  104. if ($i>$endLine)$footer[] = $lineStr;
  105. };
  106. $content[] = " app()->singleton('{$fileName}',{$fileName}::class);".PHP_EOL;
  107. array_multisort($content);
  108. fclose($fop);
  109. unlink($path);
  110. foreach (array_merge($header,$content,$footer) as $value){
  111. file_put_contents($path,$value,FILE_APPEND);
  112. }
  113. }
  114. $this->info("File create success!");
  115. }
  116. }