@@ -56,13 +56,50 @@ protected function getPath($name): string
56
56
*/
57
57
protected function getStub (): string
58
58
{
59
- $ stubName = $ this ->option ('class ' ) ? 'volt-component-class.stub ' : 'volt-component.stub ' ;
59
+ if ($ this ->option ('class ' )) {
60
+ $ stubName = 'volt-component-class.stub ' ;
61
+ } elseif ($ this ->option ('functional ' )) {
62
+ $ stubName = 'volt-component.stub ' ;
63
+ } elseif ($ this ->alreadyUsingClasses ()) {
64
+ $ stubName = 'volt-component-class.stub ' ;
65
+ } else {
66
+ $ stubName = 'volt-component.stub ' ;
67
+ }
60
68
61
69
return file_exists ($ customPath = $ this ->laravel ->basePath ('stubs/ ' .$ stubName ))
62
70
? $ customPath
63
71
: __DIR__ .'/../../stubs/ ' .$ stubName ;
64
72
}
65
73
74
+ /**
75
+ * Determine if the project is currently using class-based components.
76
+ *
77
+ * @return bool
78
+ */
79
+ protected function alreadyUsingClasses (): bool
80
+ {
81
+ $ paths = Volt::paths ();
82
+
83
+ $ mountPath = isset ($ paths [0 ])
84
+ ? $ paths [0 ]->path
85
+ : config ('livewire.view_path ' , resource_path ('views/livewire ' ));
86
+
87
+ $ files = collect (File::allFiles ($ mountPath ));
88
+
89
+ foreach ($ files as $ file ) {
90
+ if ($ file ->getExtension () === 'php ' && str_ends_with ($ file ->getFilename (), '.blade.php ' )) {
91
+ $ content = File::get ($ file ->getPathname ());
92
+
93
+ if (str_contains ($ content , 'use Livewire\Volt\Component ' ) ||
94
+ str_contains ($ content , 'new class extends Component ' )) {
95
+ return true ;
96
+ }
97
+ }
98
+ }
99
+
100
+ return false ;
101
+ }
102
+
66
103
/**
67
104
* Create the matching test case if requested.
68
105
*
@@ -173,6 +210,7 @@ protected function getOptions(): array
173
210
{
174
211
return [
175
212
['class ' , null , InputOption::VALUE_NONE , 'Create a class based component ' ],
213
+ ['functional ' , null , InputOption::VALUE_NONE , 'Create a functional component ' ],
176
214
['force ' , 'f ' , InputOption::VALUE_NONE , 'Create the Volt component even if the component already exists ' ],
177
215
];
178
216
}
0 commit comments