Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make class component if project uses Volt Class components #106

Merged
merged 3 commits into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading