MakeServiceCommand.php 3.7 KB

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