-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMakeComponent.php
More file actions
63 lines (48 loc) · 1.95 KB
/
Copy pathMakeComponent.php
File metadata and controls
63 lines (48 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Aerni\LivewireForms\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Form;
use Statamic\Facades\Path;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
use function Laravel\Prompts\select;
class MakeComponent extends Command
{
use RunsInPlease;
protected $signature = 'livewire-forms:component';
protected $description = 'Create a new Livewire Forms component';
public function handle(): void
{
$forms = Form::all();
if ($forms->isEmpty()) {
error('There are no Statamic forms. You need at least one form to create a Livewire component.');
return;
}
$name = select(
label: 'Select the form for which you want to create a Livewire component.',
options: $forms->mapWithKeys(fn ($form) => [$form->handle() => $form->title()]),
);
$classNamespace = config('livewire.class_namespace');
$className = Str::of($name)->endsWith('Form') ? $name : Str::of($name)->append('Form')->studly();
$classPath = config('livewire.class_path', app_path('Livewire'))."/{$className}.php";
$stub = File::get(__DIR__.'/form.stub');
$stub = preg_replace(
['/\[namespace\]/', '/\[class\]/'],
[$classNamespace, $className],
$stub
);
if (! File::exists($classPath) || confirm(label: 'A component with this name already exists. Do you want to overwrite it?', default: false)) {
File::ensureDirectoryExists(dirname($classPath));
File::put($classPath, $stub);
info("The component was successfully created: <comment>{$this->getRelativePath($classPath)}</comment>");
}
}
protected function getRelativePath($path): string
{
return Path::makeRelative($path);
}
}