Skip to content

Commit 6c3b660

Browse files
author
Mingeun Kim
committed
feat: command option contract
1 parent eabdb29 commit 6c3b660

File tree

1 file changed

+104
-5
lines changed

1 file changed

+104
-5
lines changed

src/MakeService.php

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,134 @@
44

55
use Illuminate\Console\GeneratorCommand;
66

7+
/**
8+
* Class MakeService
9+
* @package Getsolaris\LaravelMakeService
10+
* @author getsolaris (https://github.com/getsolaris)
11+
*/
712
class MakeService extends GeneratorCommand
813
{
914
/**
1015
* The name and signature of the console command.
1116
*
1217
* @var string
1318
*/
14-
protected $signature = 'make:service {name}';
19+
protected $signature = 'make:service {name : Create a service class} {--c : Create a service contract}';
1520

1621
/**
1722
* The console command description.
1823
*
1924
* @var string
2025
*/
21-
protected $description = 'Create a new service class';
26+
protected $description = 'Create a new service class and contract';
2227

2328
/**
2429
* The type of class being generated.
2530
*
2631
* @var string
2732
*/
2833
protected $type = 'Service';
29-
34+
35+
protected function getStub() { }
36+
3037
/**
38+
* @param bool $isContract
3139
* @return string
3240
*/
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()
3467
{
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);
36135
}
37136

38137
/**

0 commit comments

Comments
 (0)