| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- class MakeServiceCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'make:service
- {--provider : Register in the provider}
- {name : fileName}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Create Service file';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return void
- */
- public function handle()
- {
- if (!file_exists(base_path('app\\Services'))){
- mkdir('app\\Services',666,false);
- }
- $fileName=$this->argument('name');
- if (!file_exists(base_path('app\\Services\\'.$fileName.'.php'))){
- file_put_contents(base_path('app\\Services\\'.$fileName.'.php'),
- '<?php '.PHP_EOL.PHP_EOL.'namespace App\Services; '.PHP_EOL.PHP_EOL.'Class '.$fileName.PHP_EOL.'{ '.PHP_EOL.PHP_EOL.PHP_EOL.'}');
- }
- else $this->error("ERROR: file exists;");
- if ($this->option("provider")){
- $path = base_path("app\\Providers\\AppServiceProvider.php");
- $i = 0;
- $fop = fopen($path,"r+");
- $endLine = 0;
- $startLine = 0;
- while (!feof($fop)){
- $i += 1;
- $lineStr = fgets($fop);
- if (strpos($lineStr,"app()->singleton"))$endLine = $i;
- if (strpos($lineStr,"App\Services"))$startLine = $i;
- };
- fclose($fop);
- $fop = fopen($path,"r+");
- $i = 0;
- $arr = [];
- while (!feof($fop)){
- $i += 1;
- $lineStr = fgets($fop);
- if ($i == $endLine){
- $lineStr = $lineStr." app()->singleton('{$fileName}',{$fileName}::class);".PHP_EOL;
- }
- if ($i == $startLine){
- $lineStr = $lineStr."use App\Services\\$fileName;".PHP_EOL;
- }
- $arr[] = $lineStr;
- };
- fclose($fop);
- unlink($path);
- foreach ($arr as $value){
- file_put_contents($path,$value,FILE_APPEND);
- }
- }
- $this->info("File create success!");
- }
- }
|