|
4 | 4 |
|
5 | 5 | use Illuminate\Console\GeneratorCommand;
|
6 | 6 |
|
| 7 | +/** |
| 8 | + * Class MakeService |
| 9 | + * @package Getsolaris\LaravelMakeService |
| 10 | + * @author getsolaris (https://github.com/getsolaris) |
| 11 | + */ |
7 | 12 | class MakeService extends GeneratorCommand
|
8 | 13 | {
|
9 | 14 | /**
|
10 | 15 | * The name and signature of the console command.
|
11 | 16 | *
|
12 | 17 | * @var string
|
13 | 18 | */
|
14 |
| - protected $signature = 'make:service {name}'; |
| 19 | + protected $signature = 'make:service {name : Create a service class} {--c : Create a service contract}'; |
15 | 20 |
|
16 | 21 | /**
|
17 | 22 | * The console command description.
|
18 | 23 | *
|
19 | 24 | * @var string
|
20 | 25 | */
|
21 |
| - protected $description = 'Create a new service class'; |
| 26 | + protected $description = 'Create a new service class and contract'; |
22 | 27 |
|
23 | 28 | /**
|
24 | 29 | * The type of class being generated.
|
25 | 30 | *
|
26 | 31 | * @var string
|
27 | 32 | */
|
28 | 33 | protected $type = 'Service';
|
29 |
| - |
| 34 | + |
| 35 | + protected function getStub() { } |
| 36 | + |
30 | 37 | /**
|
| 38 | + * @param bool $isContract |
31 | 39 | * @return string
|
32 | 40 | */
|
33 |
| - protected function getStub(): string |
| 41 | + protected function getServiceStub(bool $isContract): string |
| 42 | + { |
| 43 | + if ($isContract) { |
| 44 | + return __DIR__ . '/Stubs/service.stub'; |
| 45 | + } |
| 46 | + |
| 47 | + return __DIR__ . '/Stubs/service.origin.stub'; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return string |
| 52 | + */ |
| 53 | + protected function getServiceContractStub(): string |
| 54 | + { |
| 55 | + return __DIR__ . '/Stubs/service.contract.stub'; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Execute the console command. |
| 60 | + * |
| 61 | + * @return bool|null |
| 62 | + * |
| 63 | + * @see \Illuminate\Console\GeneratorCommand |
| 64 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 65 | + */ |
| 66 | + public function handle() |
34 | 67 | {
|
35 |
| - return __DIR__ . '/Stubs/service.stub'; |
| 68 | + if ($this->isReservedName($this->getNameInput())) { |
| 69 | + $this->error('The name "'.$this->getNameInput().'" is reserved by PHP.'); |
| 70 | + |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + $name = $this->qualifyClass($this->getNameInput()); |
| 75 | + |
| 76 | + $path = $this->getPath($name); |
| 77 | + |
| 78 | + if ((! $this->hasOption('force') || |
| 79 | + ! $this->option('force')) && |
| 80 | + $this->alreadyExists($this->getNameInput())) { |
| 81 | + $this->error($this->type.' already exists!'); |
| 82 | + |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + $this->makeDirectory($path); |
| 87 | + $isContract = $this->option('c'); |
| 88 | + |
| 89 | + $this->files->put($path, $this->sortImports( |
| 90 | + $this->buildServiceClass($name, $isContract) |
| 91 | + )); |
| 92 | + $message = $this->type; |
| 93 | + |
| 94 | + // Whether to create contract |
| 95 | + if ($isContract) { |
| 96 | + $contractName = str_replace($this->getNameInput(), $this->getNameInput() . 'Contract.php', $this->getNameInput()); |
| 97 | + $contractPath = str_replace($this->getNameInput() . '.php', 'Contracts/', $path); |
| 98 | + $this->makeDirectory($contractPath . $contractName); |
| 99 | + $this->files->put($contractPath . $contractName, $this->sortImports($this->buildServiceContractInterface($this->getNameInput()))); |
| 100 | + $message .= ' and Contract'; |
| 101 | + } |
| 102 | + |
| 103 | + $this->info($message . ' created successfully.'); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Build the class with the given name. |
| 108 | + * |
| 109 | + * @param string $name |
| 110 | + * @param $isContract |
| 111 | + * @return string |
| 112 | + * |
| 113 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 114 | + */ |
| 115 | + protected function buildServiceClass($name, $isContract): string |
| 116 | + { |
| 117 | + $stub = $this->files->get($this->getServiceStub($isContract)); |
| 118 | + |
| 119 | + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Build the class with the given name. |
| 124 | + * |
| 125 | + * @param string $name |
| 126 | + * @return string |
| 127 | + * |
| 128 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 129 | + */ |
| 130 | + protected function buildServiceContractInterface($name): string |
| 131 | + { |
| 132 | + $stub = $this->files->get($this->getServiceContractStub()); |
| 133 | + |
| 134 | + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
36 | 135 | }
|
37 | 136 |
|
38 | 137 | /**
|
|
0 commit comments