Skip to content

Commit

Permalink
Make class component if project uses Volt Class components (#106)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored May 31, 2024
1 parent 1312839 commit 72a9bde
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,50 @@ protected function getPath($name): string
*/
protected function getStub(): string
{
$stubName = $this->option('class') ? 'volt-component-class.stub' : 'volt-component.stub';
if ($this->option('class')) {
$stubName = 'volt-component-class.stub';
} elseif ($this->option('functional')) {
$stubName = 'volt-component.stub';
} elseif ($this->alreadyUsingClasses()) {
$stubName = 'volt-component-class.stub';
} else {
$stubName = 'volt-component.stub';
}

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

/**
* Determine if the project is currently using class-based components.
*
* @return bool
*/
protected function alreadyUsingClasses(): bool
{
$paths = Volt::paths();

$mountPath = isset($paths[0])
? $paths[0]->path
: config('livewire.view_path', resource_path('views/livewire'));

$files = collect(File::allFiles($mountPath));

foreach ($files as $file) {
if ($file->getExtension() === 'php' && str_ends_with($file->getFilename(), '.blade.php')) {
$content = File::get($file->getPathname());

if (str_contains($content, 'use Livewire\Volt\Component') ||
str_contains($content, 'new class extends Component')) {
return true;
}
}
}

return false;
}

/**
* Create the matching test case if requested.
*
Expand Down Expand Up @@ -173,6 +210,7 @@ protected function getOptions(): array
{
return [
['class', null, InputOption::VALUE_NONE, 'Create a class based component'],
['functional', null, InputOption::VALUE_NONE, 'Create a functional component'],
['force', 'f', InputOption::VALUE_NONE, 'Create the Volt component even if the component already exists'],
];
}
Expand Down

0 comments on commit 72a9bde

Please sign in to comment.