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

feature: MakeField command #39

Merged
merged 12 commits into from
Mar 2, 2021
11 changes: 8 additions & 3 deletions src/Commands/Concerns/CanManipulateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ trait CanManipulateFiles
{
protected function checkForCollision($paths)
{
$filesystem = new Filesystem();

foreach ($paths as $path) {
if ($filesystem->exists($path)) {
if ($this->fileExists($path)) {
$this->error("$path already exists, aborting.");

return true;
Expand All @@ -37,6 +35,13 @@ protected function copyStubToApp($stub, $targetPath, $replacements = [])
$this->writeFile($targetPath, $stub);
}

protected function fileExists($path)
{
$filesystem = new Filesystem();

return $filesystem->exists($path);
}

protected function writeFile($path, $contents)
{
$filesystem = new Filesystem();
Expand Down
71 changes: 71 additions & 0 deletions src/Commands/MakeFieldCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Filament\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class MakeFieldCommand extends Command
{
use Concerns\CanManipulateFiles;

protected $description = 'Creates a Filament field class and view.';

protected $signature = 'make:filament-field {name} {--R|resource}';

public function handle()
{
$field = (string) Str::of($this->argument('name'))
->trim('/')
->trim('\\')
->trim(' ')
->replace('/', '\\');
$fieldClass = (string) Str::of($field)->afterLast('\\');
$fieldNamespace = Str::of($field)->contains('\\') ?
(string) Str::of($field)->beforeLast('\\') :
'';

$view = Str::of($field)
->prepend('filament\\forms\\components\\')
->explode('\\')
->map(fn ($segment) => Str::kebab($segment))
->implode('.');

$path = app_path(
(string) Str::of($field)
->prepend($this->option('resource') ? 'Filament\\Resources\\Forms\\Components\\' : "Filament\\Forms\\Components\\")
->replace('\\', '/')
->append('.php'),
);
$viewPath = resource_path(
(string) Str::of($view)
->replace('.', '/')
->prepend('views/')
->append('.blade.php'),
);

if ($this->checkForCollision([
$path,
])) return;

if (! $this->option('resource')) {
$this->copyStubToApp('Field', $path, [
'class' => $fieldClass,
'namespace' => 'App\\Filament\\Forms\\Components' . ($fieldNamespace !== '' ? "\\{$fieldNamespace}" : ''),
'view' => $view,
]);
} else {
$this->copyStubToApp('ResourceField', $path, [
'class' => $fieldClass,
'namespace' => 'App\\Filament\\Resources\\Forms\\Components' . ($fieldNamespace !== '' ? "\\{$fieldNamespace}" : ''),
'view' => $view,
]);
}

if (! $this->fileExists($viewPath)) {
$this->copyStubToApp('FieldView', $viewPath);
}

$this->info("Successfully created {$field}!");
}
}
1 change: 1 addition & 0 deletions src/FilamentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected function bootCommands()
Commands\MakePageCommand::class,
Commands\MakeUserCommand::class,
Commands\MakeWidgetCommand::class,
Commands\MakeFieldCommand::class,
]);
}

Expand Down
10 changes: 10 additions & 0 deletions stubs/Field.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace {{namespace}};

use Filament\Forms\Components\Field;

class {{class}} extends Field
{
public $view = '{{view}}';
}
18 changes: 18 additions & 0 deletions stubs/FieldView.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<x-forms::field-group
:column-span="$formComponent->columnSpan"
:error-key="$formComponent->name"
:for="$formComponent->id"
:help-message="__($formComponent->helpMessage)"
:hint="__($formComponent->hint)"
:label="__($formComponent->label)"
:required="$formComponent->required"
>
<div
x-data="{
value: @entangle($formComponent->name){{ Str::of($formComponent->nameAttribute)->after('wire:model') }},
}"
id="{{ $formComponent->id }}"
>
{{-- Field content --}}
</div>
</x-forms::field-group>
22 changes: 11 additions & 11 deletions stubs/Resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ class {{resourceClass}} extends Resource

public static function form(Form $form)
{
return $form
->schema([
//
]);
return $form
->schema([
//
]);
}

public static function table(Table $table)
{
return $table
->columns([
//
])
->filters([
//
]);
return $table
->columns([
//
])
->filters([
//
]);
}

public static function relations()
Expand Down
13 changes: 13 additions & 0 deletions stubs/ResourceField.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace {{namespace}};

use Filament\Forms\Components\Field;
use Filament\Resources\Forms\Components\Concerns;

class {{class}} extends Field
{
use Concerns\InteractsWithResource;

public $view = '{{view}}';
}