Skip to content

Commit c9e1560

Browse files
committed
fix create service with multiple folder
1 parent 8750005 commit c9e1560

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

src/Commands/MakeRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function createRepositoryInterface(string $className)
8383

8484
$repositoryInterfacePath = $this->getRepositoryInterfacePath($className);
8585
if (file_exists($repositoryInterfacePath)) {
86-
$this->error("file $className repository interface already exist: {$repositoryInterfaceName}");
86+
$this->error("file $className repository interface already exist");
8787
return ;
8888
}
8989

@@ -118,7 +118,7 @@ public function createRepository(string $className, $isDefault = true)
118118
$stubName = $isDefault ? "eloquent-repository.stub" : "custom-repository.stub";
119119
$repositoryPath = $this->getRepositoryPath($className, $isDefault);
120120
if (file_exists($repositoryPath)) {
121-
$this->error("file $className repository already exist: {$repositoryName}");
121+
$this->error("file $className repository already exist");
122122
return ;
123123
}
124124
new CreateFile(

src/Commands/MakeService.php

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MakeService extends Command
1515
public $signature = 'make:service
1616
{name : The name of the service }
1717
{--repository : Create a repository along with the service}?
18-
{--blank : Create a service with blank template}?';
18+
{--blank : Create a service with the blank template}?';
1919

2020
public $description = 'Create a new service class';
2121

@@ -24,7 +24,7 @@ public function handle()
2424
$name = str_replace(config("easy-repository.service_interface_suffix"), "", $this->argument("name"));
2525
$className = Str::studly($name);
2626

27-
$this->checkIfRequiredDirectoriesExist($className);
27+
$this->checkIfRequiredDirectoriesExist();
2828

2929
$this->createServiceInterface($className);
3030

@@ -43,22 +43,27 @@ public function handle()
4343
*/
4444
public function createService(string $className)
4545
{
46-
$serviceName = $className . config("easy-repository.service_suffix");
46+
$nameOfService = $this->getServiceName($className);
47+
$serviceName = $nameOfService . config("easy-repository.service_suffix");
4748

4849
$namespace = $this->getNameSpace($className);
4950
$stubProperties = [
5051
"{namespace}" => $namespace,
5152
"{serviceName}" => $serviceName,
52-
"{serviceInterface}" => $className. config("easy-repository.service_interface_suffix"),
53-
"{repositoryInterfaceName}" => $this->getRepositoryInterfaceName($className),
54-
"{repositoryInterfaceNamespace}" => $this->getRepositoryInterfaceNamespace($className),
53+
"{serviceInterface}" => $nameOfService. config("easy-repository.service_interface_suffix"),
54+
"{repositoryInterfaceName}" => $this->getRepositoryInterfaceName($nameOfService),
55+
"{repositoryInterfaceNamespace}" => $this->getRepositoryInterfaceNamespace($nameOfService),
5556
];
56-
// check file exist
57-
$filePath = $this->getServicePath($className);
58-
if (file_exists($filePath)) {
59-
$this->error("file $className service already exist");
57+
// cek file exist
58+
if(file_exists($this->getServicePath($className,$nameOfService))) {
59+
$this->error("file $className repository already exist");
6060
return ;
6161
}
62+
// check folder exist
63+
$folder = $this->getPath($className);
64+
if (!file_exists($folder)) {
65+
File::makeDirectory($folder, 0775, true, true);
66+
}
6267

6368
// check command blank
6469
if($this->option("blank")) {
@@ -70,7 +75,7 @@ public function createService(string $className)
7075
// create file
7176
new CreateFile(
7277
$stubProperties,
73-
$filePath,
78+
$this->getServicePath($className, $nameOfService),
7479
$stubPath
7580
);
7681
$this->line("<info>Created $className service implement:</info> {$serviceName}");
@@ -84,23 +89,28 @@ public function createService(string $className)
8489
*/
8590
public function createServiceInterface(string $className)
8691
{
87-
$serviceName = $className . config("easy-repository.service_interface_suffix");
92+
$nameOfService = $this->getServiceName($className);
93+
$serviceName = $nameOfService . config("easy-repository.service_interface_suffix");
8894

8995
$namespace = $this->getNameSpace($className);
9096
$stubProperties = [
9197
"{namespace}" => $namespace,
9298
"{serviceInterface}" => $serviceName,
9399
];
94-
// check folder exist
95-
$interfacePath = $this->getServiceInterfacePath($className,$serviceName);
96-
if (file_exists($interfacePath)) {
97-
$this->error("file $className service interface already exist");
100+
// cek file exist
101+
if(file_exists($this->getServiceInterfacePath($className,$serviceName))) {
102+
$this->error("file $className repository interface already exist");
98103
return ;
99104
}
105+
// check folder exist
106+
$folder = $this->getPath($className);
107+
if (!file_exists($folder)) {
108+
File::makeDirectory($folder, 0775, true, true);
109+
}
100110
// create file
101111
new CreateFile(
102112
$stubProperties,
103-
$interfacePath,
113+
$this->getServiceInterfacePath($className,$serviceName),
104114
__DIR__ . "/stubs/service-interface.stub"
105115
);
106116
$this->line("<info>Created $className service interface:</info> {$serviceName}");
@@ -111,11 +121,11 @@ public function createServiceInterface(string $className)
111121
*
112122
* @return string
113123
*/
114-
private function getServicePath($className)
124+
private function getServicePath($className, $servicename)
115125
{
116126
return $this->appPath() . "/" .
117127
config("easy-repository.service_directory") .
118-
"/$className". "/$className" . config("easy-repository.service_suffix") .".php";
128+
"/$className". "/$servicename" . config("easy-repository.service_suffix") .".php";
119129
}
120130

121131
/**
@@ -174,10 +184,9 @@ private function getRepositoryName(string $className) {
174184
*
175185
* @return void
176186
*/
177-
private function checkIfRequiredDirectoriesExist(string $className)
187+
private function checkIfRequiredDirectoriesExist()
178188
{
179189
$this->ensureDirectoryExists(config("easy-repository.service_directory"));
180-
$this->ensureDirectoryExists(config("easy-repository.service_directory"). "/". $className);
181190
}
182191

183192
/**
@@ -196,7 +205,29 @@ private function getServiceName($className):string {
196205
* @return string
197206
*/
198207
private function getNameSpace($className):string {
199-
return config("easy-repository.service_namespace")."\\".$className;
208+
$explode = explode('/', $className);
209+
if (count($explode) > 1) {
210+
$namespace = '';
211+
for($i=0; $i < count($explode)-1; $i++) {
212+
$namespace .= '\\'.$explode[$i];
213+
}
214+
return config("easy-repository.service_namespace").$namespace."\\".end($explode);
215+
} else {
216+
return config("easy-repository.service_namespace")."\\".$className;
217+
}
218+
}
219+
220+
private function getPath($className):string {
221+
$explode = explode('/', $className);
222+
if (count($explode) > 1) {
223+
$namespace = '';
224+
for($i=0; $i < count($explode)-1; $i++) {
225+
$namespace .= '\\'.$explode[$i];
226+
}
227+
return config("easy-repository.service_directory").$namespace."\\".end($explode);
228+
} else {
229+
return config("easy-repository.service_directory")."\\".$className;
230+
}
200231
}
201232

202233
/**
@@ -213,4 +244,4 @@ private function createRepository()
213244
"name" => $name,
214245
]);
215246
}
216-
}
247+
}

0 commit comments

Comments
 (0)