Skip to content

Commit 72a9bde

Browse files
joshcirreJosh Cirretaylorotwell
authored
Make class component if project uses Volt Class components (#106)
* Update Make Command, add functional option * change nested ternary to if statement * formatting --------- Co-authored-by: Josh Cirre <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1312839 commit 72a9bde

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/Console/MakeCommand.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,50 @@ protected function getPath($name): string
5656
*/
5757
protected function getStub(): string
5858
{
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+
}
6068

6169
return file_exists($customPath = $this->laravel->basePath('stubs/'.$stubName))
6270
? $customPath
6371
: __DIR__.'/../../stubs/'.$stubName;
6472
}
6573

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+
66103
/**
67104
* Create the matching test case if requested.
68105
*
@@ -173,6 +210,7 @@ protected function getOptions(): array
173210
{
174211
return [
175212
['class', null, InputOption::VALUE_NONE, 'Create a class based component'],
213+
['functional', null, InputOption::VALUE_NONE, 'Create a functional component'],
176214
['force', 'f', InputOption::VALUE_NONE, 'Create the Volt component even if the component already exists'],
177215
];
178216
}

0 commit comments

Comments
 (0)