33namespace Silverhand7 \LaravelArtisanMaker \Console ;
44
55use Illuminate \Console \GeneratorCommand ;
6+ use Illuminate \Support \Facades \Artisan ;
67use Illuminate \Support \Str ;
78
89class 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