Skip to content

Commit 47c94a3

Browse files
committed
optional option to generate service with interface implementation
1 parent 826a4e8 commit 47c94a3

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/Console/FileGenerable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function buildClass($name): string
1919
->replaceClass($stub, $name);
2020
}
2121

22-
protected function replaceClass(&$stub, $name): string
22+
protected function replaceClass($stub, $name): string
2323
{
2424
$className = Str::replace($this->getNamespace($name).'\\', '', $name);
2525

src/Console/ServiceMakeCommand.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Silverhand7\LaravelArtisanMaker\Console;
44

55
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Facades\Artisan;
67
use Illuminate\Support\Str;
78

89
class ServiceMakeCommand extends GeneratorCommand
@@ -26,13 +27,22 @@ protected function buildClass($name): string
2627
$interfaceName = $this->option('interface') ?? $this->option('i');
2728
$interfaceUseNamespace = config('artisan-maker.interface_namespace') . '\\' . $interfaceName;
2829

30+
if (!$this->checkInterfaceExists($interfaceUseNamespace)) {
31+
if ($this->confirm('Interface does not exist, do you want to create one?', true)) {
32+
Artisan::call('make:interface', [
33+
'name' => $interfaceName
34+
]);
35+
}
36+
}
37+
38+
2939
$stub = $this->files->get($this->getClassImplementInterfaceStub());
3040

3141
return $this
32-
->replaceInterfaceUseNamespace($stub, $interfaceUseNamespace)
33-
->replaceInterfaceClass($stub, $interfaceName)
34-
->replaceNamespace($stub, $name)
35-
->replaceClass($stub, $name);
42+
->replaceInterfaceUseNamespace($stub, $interfaceUseNamespace)
43+
->replaceInterfaceClass($stub, $interfaceName)
44+
->replaceNamespace($stub, $name)
45+
->replaceClass($stub, $name);
3646
}
3747

3848
$stub = $this->files->get($this->getStub());
@@ -53,4 +63,11 @@ protected function replaceInterfaceUseNamespace(&$stub, $interfaceUseNamespace)
5363
$stub = Str::replace("{{ interfaceUseNamespace }}", $interfaceUseNamespace, $stub);
5464
return $this;
5565
}
66+
67+
protected function checkInterfaceExists($interfaceUseNamespace): bool
68+
{
69+
$paths = explode('\\', $interfaceUseNamespace);
70+
array_shift($paths);
71+
return file_exists($this->laravel['path'] . '/' . implode('/', $paths) . '.php');
72+
}
5673
}

0 commit comments

Comments
 (0)