MakeServiceCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. $fileName=$resultNames[0];
  43. $modelName=Str::studly($resultNames[1]);
  44. if (!file_exists(base_path('app\\Services\\'.$fileName.'.php'))){
  45. file_put_contents(base_path('app\\Services\\'.$fileName.'.php'),
  46. '<?php '
  47. .PHP_EOL
  48. .PHP_EOL
  49. .'namespace App\Services;'
  50. .PHP_EOL
  51. .PHP_EOL
  52. .'use App\Traits\ServiceAppAop;'
  53. .'use App\\'.$modelName.';'
  54. .PHP_EOL
  55. .PHP_EOL
  56. .'class '.$fileName
  57. .PHP_EOL
  58. .'{'
  59. .PHP_EOL
  60. .' use ServiceAppAop;'
  61. .' protected $modelClass='.$modelName.'::class;'
  62. .PHP_EOL
  63. .PHP_EOL.
  64. '}');
  65. }
  66. else $this->error("ERROR: file exists;");
  67. if (!$this->option("noProvider")){
  68. $path = base_path("app\\Providers\\AppServiceProvider.php");
  69. $i = 0;
  70. $fop = fopen($path,"r+");
  71. $endLine = 0;
  72. $startLine = 0;
  73. $sign = 0;
  74. while (!feof($fop)){
  75. $i += 1;
  76. $lineStr = fgets($fop);
  77. if (strpos($lineStr,"loadingService(){"))$sign = $i;
  78. if (strpos($lineStr,"app()->singleton"))$endLine = $i;
  79. if (strpos($lineStr,"App\Services"))$startLine = $i;
  80. };
  81. fclose($fop);
  82. $fop = fopen($path,"r+");
  83. $i = 0;
  84. $header = [];
  85. $content = [];
  86. $footer = [];
  87. while (!feof($fop)){
  88. $i += 1;
  89. $lineStr = fgets($fop);
  90. if ($i == $startLine){
  91. $lineStr = $lineStr."use App\Services\\$fileName;".PHP_EOL;
  92. }
  93. if ($i<=$sign)$header[] = $lineStr;
  94. if ($i>$sign && $i<= $endLine){
  95. $content[] = $lineStr;
  96. }
  97. if ($i>$endLine)$footer[] = $lineStr;
  98. };
  99. $content[] = " app()->singleton('{$fileName}',{$fileName}::class);".PHP_EOL;
  100. array_multisort($content);
  101. fclose($fop);
  102. unlink($path);
  103. foreach (array_merge($header,$content,$footer) as $value){
  104. file_put_contents($path,$value,FILE_APPEND);
  105. }
  106. }
  107. $this->info("File create success!");
  108. }
  109. }