3
3
namespace Silverhand7 \LaravelArtisanMaker \Console ;
4
4
5
5
use Illuminate \Console \GeneratorCommand ;
6
+ use Illuminate \Support \Facades \Artisan ;
6
7
use Illuminate \Support \Str ;
7
8
8
9
class ServiceMakeCommand extends GeneratorCommand
@@ -26,13 +27,22 @@ protected function buildClass($name): string
26
27
$ interfaceName = $ this ->option ('interface ' ) ?? $ this ->option ('i ' );
27
28
$ interfaceUseNamespace = config ('artisan-maker.interface_namespace ' ) . '\\' . $ interfaceName ;
28
29
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
+
29
39
$ stub = $ this ->files ->get ($ this ->getClassImplementInterfaceStub ());
30
40
31
41
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 );
36
46
}
37
47
38
48
$ stub = $ this ->files ->get ($ this ->getStub ());
@@ -53,4 +63,11 @@ protected function replaceInterfaceUseNamespace(&$stub, $interfaceUseNamespace)
53
63
$ stub = Str::replace ("{{ interfaceUseNamespace }} " , $ interfaceUseNamespace , $ stub );
54
64
return $ this ;
55
65
}
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
+ }
56
73
}
0 commit comments