diff --git a/src/Concerns/CanMapDynamicFields.php b/src/Concerns/CanMapDynamicFields.php index 13d59a7..3b44fad 100644 --- a/src/Concerns/CanMapDynamicFields.php +++ b/src/Concerns/CanMapDynamicFields.php @@ -3,7 +3,6 @@ namespace Backstage\Fields\Concerns; use Backstage\Fields\Contracts\FieldInspector; -use Backstage\Fields\Enums\Field; use Backstage\Fields\Fields; use Backstage\Fields\Fields\Checkbox; use Backstage\Fields\Fields\CheckboxList; diff --git a/src/Fields.php b/src/Fields.php index 5eed4be..ec47abe 100644 --- a/src/Fields.php +++ b/src/Fields.php @@ -6,7 +6,7 @@ class Fields { - private static array $fields = []; + protected static array $fields = []; public static function registerField(string $className): void { diff --git a/src/Fields/Repeater.php b/src/Fields/Repeater.php index 3b464f3..7417cbf 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -8,7 +8,6 @@ use Backstage\Fields\Contracts\FieldContract; use Backstage\Fields\Enums\Field as FieldEnum; use Backstage\Fields\Facades\Fields; -use Backstage\Fields\Fields\Select as FieldsSelect; use Backstage\Fields\Models\Field; use Filament\Forms; use Filament\Forms\Components\Hidden; @@ -28,21 +27,6 @@ class Repeater extends Base implements FieldContract use HasFieldTypeResolver; use HasOptions; - private const FIELD_TYPE_MAP = [ - 'text' => Text::class, - 'textarea' => Textarea::class, - 'rich-editor' => RichEditor::class, - 'repeater' => Repeater::class, - 'select' => FieldsSelect::class, - 'checkbox' => Checkbox::class, - 'checkbox-list' => CheckboxList::class, - 'key-value' => KeyValue::class, - 'radio' => Radio::class, - 'toggle' => Toggle::class, - 'color' => Color::class, - 'date-time' => DateTime::class, - ]; - public static function getDefaultConfig(): array { return [ diff --git a/src/Fields/RichEditor.php b/src/Fields/RichEditor.php index 2f33c5b..c1336fc 100644 --- a/src/Fields/RichEditor.php +++ b/src/Fields/RichEditor.php @@ -2,7 +2,7 @@ namespace Backstage\Fields\Fields; -use Backstage\Enums\ToolbarButton; +use Backstage\Fields\Enums\ToolbarButton; use Backstage\Fields\Contracts\FieldContract; use Backstage\Fields\Models\Field; use Filament\Forms; diff --git a/src/Filament/RelationManagers/FieldsRelationManager.php b/src/Filament/RelationManagers/FieldsRelationManager.php index f2dc552..a7b118d 100644 --- a/src/Filament/RelationManagers/FieldsRelationManager.php +++ b/src/Filament/RelationManagers/FieldsRelationManager.php @@ -84,7 +84,7 @@ public function form(Form $form): Form return; } - $set('config', $this->initializeConfig($state)); + $set('config', $this->getFieldTypeFormSchema($state)); }), Select::make('group') @@ -118,43 +118,6 @@ public function form(Form $form): Form ]); } - private function formatCustomFields(array $fields): array - { - return collect($fields)->mapWithKeys(function ($field, $key) { - $parts = explode('\\', $field); - $lastPart = end($parts); - $formattedName = Str::title(Str::snake($lastPart, ' ')); - - return [$key => $formattedName]; - })->toArray(); - } - - private function initializeDefaultConfig(string $fieldType): array - { - $className = 'Backstage\\Fields\\Fields\\' . Str::studly($fieldType); - - if (! class_exists($className)) { - return []; - } - - $fieldInstance = app($className); - - return $fieldInstance::getDefaultConfig(); - } - - private function initializeCustomConfig(string $fieldType): array - { - $className = Fields::getFields()[$fieldType] ?? null; - - if (! class_exists($className)) { - return []; - } - - $fieldInstance = app($className); - - return $fieldInstance::getDefaultConfig(); - } - public function table(Table $table): Table { return $table @@ -184,14 +147,16 @@ public function table(Table $table): Table Tables\Actions\CreateAction::make() ->slideOver() ->mutateFormDataUsing(function (array $data) { - - $key = $this->ownerRecord->getKeyName() ?? 'id'; + $key = $this->ownerRecord->getKeyName(); + if ($key === '') { + $key = 'id'; + } return [ ...$data, 'position' => Field::where('model_key', $key)->get()->max('position') + 1, 'model_type' => 'setting', - 'model_key' => $this->ownerRecord->slug, + 'model_key' => $this->ownerRecord->slug ?? $key, ]; }) ->after(function (Component $livewire) { @@ -202,8 +167,10 @@ public function table(Table $table): Table Tables\Actions\EditAction::make() ->slideOver() ->mutateRecordDataUsing(function (array $data) { - - $key = $this->ownerRecord->getKeyName() ?? 'id'; + $key = $this->ownerRecord->getKeyName(); + if ($key === '') { + $key = 'id'; + } return [ ...$data, @@ -221,8 +188,10 @@ public function table(Table $table): Table ->getSchemaBuilder() ->hasColumn($this->ownerRecord->getTable(), $record->valueColumn) ) { - - $key = $this->ownerRecord->getKeyName() ?? 'id'; + $key = $this->ownerRecord->getKeyName(); + if ($key === '') { + $key = 'id'; + } $this->ownerRecord->update([ $record->valueColumn => collect($this->ownerRecord->{$record->valueColumn})->forget($record->{$key})->toArray(), diff --git a/src/Models/Field.php b/src/Models/Field.php index 62986bd..cb10610 100644 --- a/src/Models/Field.php +++ b/src/Models/Field.php @@ -11,6 +11,19 @@ use Illuminate\Support\Facades\Config; use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships; +/** + * @property string $ulid + * @property string|null $parent_ulid + * @property string $model_type + * @property string $model_key + * @property string $slug + * @property string $name + * @property string $field_type + * @property array|null $config + * @property int $position + * @property \Carbon\Carbon $created_at + * @property \Carbon\Carbon $updated_at + */ class Field extends Model { use HasPackageFactory; diff --git a/src/Services/FieldInspectionService.php b/src/Services/FieldInspectionService.php index dc4cfc4..576a605 100644 --- a/src/Services/FieldInspectionService.php +++ b/src/Services/FieldInspectionService.php @@ -63,7 +63,7 @@ private function getMethodsDetails(ReflectionClass $reflection): array 'visibility' => $this->getVisibility($method), 'static' => $method->isStatic(), 'parameters' => $this->getParametersDetails($method), - 'returnType' => $method->getReturnType() ? $method->getReturnType()->getName() : null, + 'returnType' => $method->getReturnType() ? $this->getTypeName($method->getReturnType()) : null, 'docComment' => $method->getDocComment() ?: null, ]; } @@ -78,7 +78,7 @@ private function getPropertiesDetails(ReflectionClass $reflection): array $properties[$property->getName()] = [ 'visibility' => $this->getVisibility($property), 'static' => $property->isStatic(), - 'type' => $property->getType() ? $property->getType()->getName() : null, + 'type' => $property->getType() ? $this->getTypeName($property->getType()) : null, 'docComment' => $property->getDocComment() ?: null, 'defaultValue' => $this->getPropertyDefaultValue($property), ]; @@ -92,7 +92,7 @@ private function getParametersDetails(ReflectionMethod $method): array $parameters = []; foreach ($method->getParameters() as $param) { $parameters[$param->getName()] = [ - 'type' => $param->getType() ? $param->getType()->getName() : null, + 'type' => $param->getType() ? $this->getTypeName($param->getType()) : null, 'hasDefaultValue' => $param->isDefaultValueAvailable(), 'defaultValue' => $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, 'isVariadic' => $param->isVariadic(), @@ -123,4 +123,18 @@ private function getPropertyDefaultValue(ReflectionProperty $property): mixed return null; } } + + private function getTypeName(\ReflectionType $type): string + { + if ($type instanceof \ReflectionNamedType) { + return $type->getName(); + } + if ($type instanceof \ReflectionUnionType) { + return implode('|', array_map(fn($t) => $this->getTypeName($t), $type->getTypes())); + } + if ($type instanceof \ReflectionIntersectionType) { + return implode('&', array_map(fn($t) => $this->getTypeName($t), $type->getTypes())); + } + return (string) $type; + } }