From ab1f7bf89eb792e6788af2ba5796e051bf08400d Mon Sep 17 00:00:00 2001 From: Ryan Scherler Date: Sun, 5 Apr 2020 20:10:20 -0700 Subject: [PATCH] add new command for creating fieldsets --- resources/stubs/fieldset.stub | 28 +++++++++++++++ src/Commands/MakeFieldset.php | 34 +++++++++++++++++++ src/Commands/MakeForm.php | 14 ++++---- .../{CreateUserCommand.php => MakeUser.php} | 2 +- src/FilamentServiceProvider.php | 6 ++-- src/Support/BladeDirectives.php | 2 +- src/Support/Fields/BaseField.php | 2 +- src/Support/Fields/Field.php | 2 +- src/Traits/Fields/HandlesFiles.php | 4 +-- 9 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 resources/stubs/fieldset.stub create mode 100644 src/Commands/MakeFieldset.php rename src/Commands/{CreateUserCommand.php => MakeUser.php} (97%) diff --git a/resources/stubs/fieldset.stub b/resources/stubs/fieldset.stub new file mode 100644 index 0000000000..f508e1711f --- /dev/null +++ b/resources/stubs/fieldset.stub @@ -0,0 +1,28 @@ +input() + ->rules(['required', 'string', 'max:255']), + ]; + } + + public static function rulesIgnoreRealtime(): array + { + return []; + } +} \ No newline at end of file diff --git a/src/Commands/MakeFieldset.php b/src/Commands/MakeFieldset.php new file mode 100644 index 0000000000..fe7ff177c5 --- /dev/null +++ b/src/Commands/MakeFieldset.php @@ -0,0 +1,34 @@ +argument('name')).'Fieldset'; + + $stub = File::get(__DIR__.'/../../resources/stubs/fieldset.stub'); + $stub = str_replace('DummyFieldset', $fieldsetName, $stub); + $stub = str_replace('Dummy', $this->argument('name'), $stub); + + if (!$this->option('package')) { + $stub = str_replace('namespace Filament', 'namespace App', $stub); + } + + $fieldsetPath = $this->option('package') ? __DIR__.'/../Http/Fieldsets/' : app_path('Http/Fieldsets/'); + $path = $fieldsetPath.$fieldsetName.'.php'; + + if (!File::exists($path) || $this->confirm($fieldsetName.' already exists. Overwrite it?')) { + File::put($path, $stub); + $this->info($fieldsetName.' was created in '. $fieldsetPath); + } + } +} diff --git a/src/Commands/MakeForm.php b/src/Commands/MakeForm.php index 2e4eb16e06..e5ccc85ca3 100644 --- a/src/Commands/MakeForm.php +++ b/src/Commands/MakeForm.php @@ -6,8 +6,6 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; -use Filament; - class MakeForm extends Command { protected $signature = 'filament:form {name} {--model=Model} {--package}'; @@ -17,9 +15,9 @@ class MakeForm extends Command { $componentName = Str::studly($this->argument('name')); - $stub = File::get(__DIR__ . '/../../resources/stubs/component.stub'); + $stub = File::get(__DIR__.'/../../resources/stubs/component.stub'); $stub = str_replace('DummyComponent', $componentName, $stub); - $stub = str_replace('Contracts\Dummy', 'Contracts\\' . $this->option('model'), $stub); + $stub = str_replace('Contracts\Dummy', 'Contracts\\'.$this->option('model'), $stub); $stub = str_replace('DummyClass', lcFirst($this->option('model')).'Class', $stub); $stub = str_replace('DummyContract', $this->option('model').'Contract', $stub); $stub = str_replace('DummyRoute', Str::slug(Str::plural($this->option('model'))), $stub); @@ -29,12 +27,12 @@ class MakeForm extends Command $stub = str_replace('namespace Filament', 'namespace App', $stub); } - $formPath = $this->option('package') ? __DIR__ . '/../Http/Livewire/' : app_path('Http/Livewire/'); - $path = $formPath . $componentName . '.php'; + $formPath = $this->option('package') ? __DIR__.'/../Http/Livewire/' : app_path('Http/Livewire/'); + $path = $formPath.$componentName.'.php'; - if (!File::exists($path) || $this->confirm($componentName . ' already exists. Overwrite it?')) { + if (!File::exists($path) || $this->confirm($componentName.' already exists. Overwrite it?')) { File::put($path, $stub); - $this->info($componentName . ' was created in '. $formPath); + $this->info($componentName.' was created in '. $formPath); } } } diff --git a/src/Commands/CreateUserCommand.php b/src/Commands/MakeUser.php similarity index 97% rename from src/Commands/CreateUserCommand.php rename to src/Commands/MakeUser.php index 330073b8ec..d9408467d0 100644 --- a/src/Commands/CreateUserCommand.php +++ b/src/Commands/MakeUser.php @@ -6,7 +6,7 @@ use Illuminate\Console\Command; use Filament\Traits\ConsoleValidation; use Filament\Contracts\User as UserContract; -class CreateUserCommand extends Command +class MakeUser extends Command { use ConsoleValidation; diff --git a/src/FilamentServiceProvider.php b/src/FilamentServiceProvider.php index 3ecc36bf3b..39f4428669 100644 --- a/src/FilamentServiceProvider.php +++ b/src/FilamentServiceProvider.php @@ -14,8 +14,9 @@ use Filament\Support\BladeDirectives; use Filament\Contracts\User as UserContract; use Filament\Traits\EventMap; use Filament\Http\Middleware\Authenticate; -use Filament\Commands\CreateUserCommand; +use Filament\Commands\MakeUser; use Filament\Commands\MakeForm; +use Filament\Commands\MakeFieldset; class FilamentServiceProvider extends ServiceProvider { @@ -125,8 +126,9 @@ class FilamentServiceProvider extends ServiceProvider { if ($this->app->runningInConsole()) { $this->commands([ - CreateUserCommand::class, + MakeUser::class, MakeForm::class, + MakeFieldset::class, ]); } } diff --git a/src/Support/BladeDirectives.php b/src/Support/BladeDirectives.php index 3aa4c26fab..86a1aebf35 100644 --- a/src/Support/BladeDirectives.php +++ b/src/Support/BladeDirectives.php @@ -25,7 +25,7 @@ class BladeDirectives { */ public static function pushOnce($expression) { - $var = '$__env->{"__pushonce_" . md5(__FILE__ . ":" . __LINE__)}'; + $var = '$__env->{"__pushonce_".md5(__FILE__.":".__LINE__)}'; return "startPush({$expression}); ?>"; } diff --git a/src/Support/Fields/BaseField.php b/src/Support/Fields/BaseField.php index d295db5849..7491756006 100644 --- a/src/Support/Fields/BaseField.php +++ b/src/Support/Fields/BaseField.php @@ -168,6 +168,6 @@ class BaseField public function getView() { - return $this->view ?? 'filament::fields.' . $this->type; + return $this->view ?? 'filament::fields.'.$this->type; } } diff --git a/src/Support/Fields/Field.php b/src/Support/Fields/Field.php index e1c7d11784..ce7dcbc3a2 100644 --- a/src/Support/Fields/Field.php +++ b/src/Support/Fields/Field.php @@ -14,7 +14,7 @@ class Field extends BaseField { $this->name = $name ?? Str::snake(Str::lower($label)); $this->label = __($label); - $this->key = $key_prefix ? 'form_data.' . $this->name : $this->name; + $this->key = $key_prefix ? 'form_data.'.$this->name : $this->name; } public static function make($label, $name = null, $key_prefix = true) diff --git a/src/Traits/Fields/HandlesFiles.php b/src/Traits/Fields/HandlesFiles.php index 4ef7945a4a..f98df7ed6c 100644 --- a/src/Traits/Fields/HandlesFiles.php +++ b/src/Traits/Fields/HandlesFiles.php @@ -63,7 +63,7 @@ trait HandlesFiles $this->form_data[$field_name] = $value ?? []; $this->saveField($field_name); - $this->updated('form_data.' . $field_name); + $this->updated('form_data.'.$field_name); } public function fileRemove($field_name, $file_name, $key) @@ -79,7 +79,7 @@ trait HandlesFiles $this->arrayRemove($field_name, $key); $this->saveField($field_name); - $this->updated('form_data.' . $field_name); + $this->updated('form_data.'.$field_name); } public function fileInfo($mime_type)