From 6ec281ded0dc04012057767b4b43665421e0251c Mon Sep 17 00:00:00 2001 From: Josh Cirre Date: Wed, 29 May 2024 00:41:42 -0700 Subject: [PATCH 1/3] Update Make Command, add functional option --- src/Console/MakeCommand.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Console/MakeCommand.php b/src/Console/MakeCommand.php index b616300..d26882e 100644 --- a/src/Console/MakeCommand.php +++ b/src/Console/MakeCommand.php @@ -56,13 +56,46 @@ protected function getPath($name): string */ protected function getStub(): string { - $stubName = $this->option('class') ? 'volt-component-class.stub' : 'volt-component.stub'; + $stubName = $this->option('class') + ? 'volt-component-class.stub' + : ($this->option('functional') + ? 'volt-component.stub' + : ($this->usingClass() + ? 'volt-component-class.stub' + : 'volt-component.stub' + )); return file_exists($customPath = $this->laravel->basePath('stubs/'.$stubName)) ? $customPath : __DIR__.'/../../stubs/'.$stubName; } + /** + * Determine if the project is using class-based components. + * + * @return bool + */ + protected function usingClass(): 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. * @@ -173,6 +206,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'], ]; } From 7ee52c68ed3dcd943926d87011c37c0cfa60c131 Mon Sep 17 00:00:00 2001 From: Josh Cirre Date: Thu, 30 May 2024 09:28:31 -0700 Subject: [PATCH 2/3] change nested ternary to if statement --- src/Console/MakeCommand.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Console/MakeCommand.php b/src/Console/MakeCommand.php index d26882e..4dbd7db 100644 --- a/src/Console/MakeCommand.php +++ b/src/Console/MakeCommand.php @@ -56,14 +56,15 @@ protected function getPath($name): string */ protected function getStub(): string { - $stubName = $this->option('class') - ? 'volt-component-class.stub' - : ($this->option('functional') - ? 'volt-component.stub' - : ($this->usingClass() - ? '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->usingClass()) { + $stubName = 'volt-component-class.stub'; + } else { + $stubName = 'volt-component.stub'; + } return file_exists($customPath = $this->laravel->basePath('stubs/'.$stubName)) ? $customPath From 789a908562f520fae7607fc152d3a1d528aed20a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 31 May 2024 14:06:27 -0500 Subject: [PATCH 3/3] formatting --- src/Console/MakeCommand.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Console/MakeCommand.php b/src/Console/MakeCommand.php index 4dbd7db..744b1f3 100644 --- a/src/Console/MakeCommand.php +++ b/src/Console/MakeCommand.php @@ -60,7 +60,7 @@ protected function getStub(): string $stubName = 'volt-component-class.stub'; } elseif ($this->option('functional')) { $stubName = 'volt-component.stub'; - } elseif ($this->usingClass()) { + } elseif ($this->alreadyUsingClasses()) { $stubName = 'volt-component-class.stub'; } else { $stubName = 'volt-component.stub'; @@ -72,14 +72,17 @@ protected function getStub(): string } /** - * Determine if the project is using class-based components. + * Determine if the project is currently using class-based components. * * @return bool */ - protected function usingClass(): bool + protected function alreadyUsingClasses(): bool { $paths = Volt::paths(); - $mountPath = isset($paths[0]) ? $paths[0]->path : config('livewire.view_path', resource_path('views/livewire')); + + $mountPath = isset($paths[0]) + ? $paths[0]->path + : config('livewire.view_path', resource_path('views/livewire')); $files = collect(File::allFiles($mountPath));