Skip to content
This repository was archived by the owner on Jan 6, 2026. It is now read-only.

Commit 1e89064

Browse files
Refactor FilamentData and FilamentService for enhanced inheritance handling; add UserProfileService and ArrayToStringCast for streamlined user filament import.
1 parent f0d6d9a commit 1e89064

File tree

7 files changed

+131
-13
lines changed

7 files changed

+131
-13
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Data\Casts;
4+
5+
use Spatie\LaravelData\Casts\Cast;
6+
use Spatie\LaravelData\Support\Creation\CreationContext;
7+
use Spatie\LaravelData\Support\DataProperty;
8+
9+
class ArrayToStringCast implements Cast
10+
{
11+
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): string
12+
{
13+
if (is_array($value)) {
14+
return (string) array_first($value);
15+
}
16+
17+
return (string) $value;
18+
}
19+
}

app/Data/OrcaSlicer/FilamentData.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use App\Data\Casts\ArrayToFloatCast;
66
use App\Data\Casts\ArrayToIntegerCast;
7+
use App\Data\Casts\ArrayToStringCast;
78
use Spatie\LaravelData\Attributes\MapInputName;
89
use Spatie\LaravelData\Attributes\MapName;
9-
use Spatie\LaravelData\Attributes\MapOutputName;
1010
use Spatie\LaravelData\Attributes\WithCast;
1111
use Spatie\LaravelData\Data;
1212
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
@@ -16,13 +16,14 @@ class FilamentData extends Data
1616
{
1717
public function __construct(
1818
#[MapInputName('filament_settings_id')]
19-
#[MapOutputName('external_id')]
20-
public string $settingsId,
19+
#[WithCast(ArrayToStringCast::class)]
20+
public string $externalId,
2121

2222
#[MapInputName('default_filament_colour')]
23-
public ?string $color = null,
23+
#[WithCast(ArrayToStringCast::class)]
24+
public string $color,
2425

25-
public ?string $inherits = null,
26+
public string $inherits,
2627

2728
#[WithCast(ArrayToFloatCast::class)]
2829
public float $pressureAdvance = 0,

app/Models/User.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Illuminate\Contracts\Auth\MustVerifyEmail;
88
use Illuminate\Database\Eloquent\Factories\HasFactory;
9-
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
use Illuminate\Database\Eloquent\Relations\Relation;
1010
use Illuminate\Foundation\Auth\User as Authenticatable;
1111
use Illuminate\Notifications\Notifiable;
1212

@@ -34,17 +34,22 @@ protected function casts(): array
3434
];
3535
}
3636

37-
public function machines(): BelongsToMany
37+
public function machines(): Relation
3838
{
3939
return $this->belongsToMany(Machine::class, 'user_machine')
4040
->using(UserMachine::class)
4141
->withTimestamps();
4242
}
4343

44-
public function filaments(): BelongsToMany
44+
public function filaments(): Relation
4545
{
4646
return $this->belongsToMany(Filament::class, 'user_filament')
4747
->using(UserFilament::class)
4848
->withTimestamps();
4949
}
50+
51+
public function userFilaments(): Relation
52+
{
53+
return $this->hasMany(UserFilament::class, 'user_id', 'id');
54+
}
5055
}

app/Services/OrcaSlicer/FilamentService.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,40 @@ public function __construct(
2121
protected string $directory,
2222
) {}
2323

24-
public function get(FilamentData $filament): FilamentData {}
24+
public function get(FilamentData $filament): FilamentData
25+
{
26+
if (! $filament->inherits) {
27+
return $filament;
28+
}
29+
30+
$profile = $this->profile($filament->inherits);
31+
$content = $this->perform($profile, $filament->inherits);
32+
33+
$source = $filament->toArray();
34+
35+
$source['filament_settings_id'] = $filament->externalId;
36+
37+
return FilamentData::from(
38+
array_merge($this->clean($content), $this->clean($source))
39+
);
40+
}
41+
42+
protected function perform(string $profile, string $key, array $values = []): array
43+
{
44+
if (! $path = $this->findPath($profile, $key)) {
45+
return $values;
46+
}
47+
48+
$content = $this->clean(
49+
$this->read($path)
50+
);
51+
52+
if (! $parent = $content['inherits'] ?? null) {
53+
return array_merge($content, $values);
54+
}
55+
56+
return $this->perform($profile, $parent, array_merge($content, $values));
57+
}
2558

2659
protected function findPath(string $profile, string $key): ?string
2760
{
@@ -43,4 +76,9 @@ protected function read(string $filename): array
4376
$this->directory . '/resources/profiles/' . $filename
4477
);
4578
}
79+
80+
protected function clean(array $values): array
81+
{
82+
return array_filter($values);
83+
}
4684
}

app/Services/OrcaSlicer/FilamentTypeService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function store(Vendor $vendor, Map $map): void
125125
]);
126126
}
127127

128-
protected function detect(string $vendor, string $filament, string $path): string
128+
public function detect(string $vendor, string $filament, string $path): string
129129
{
130130
return Str::of($filament)
131131
->when(
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Services\OrcaSlicer;
6+
7+
use App\Concerns\WithColor;
8+
use App\Data\OrcaSlicer\FilamentData;
9+
use App\Models\Filament;
10+
use App\Models\Machine;
11+
use App\Models\User;
12+
use Illuminate\Support\Str;
13+
14+
class UserProfileService
15+
{
16+
use WithColor;
17+
18+
public function __construct(
19+
protected FilamentService $filament,
20+
protected FilamentTypeService $filamentType,
21+
) {}
22+
23+
public function import(User $user, Machine $machine, FilamentData $profile): void
24+
{
25+
$vendor = $this->vendor($profile->inherits);
26+
27+
$filament = $this->filament($vendor, $profile->inherits);
28+
$color = $this->color($profile->color);
29+
30+
$content = $profile->toArray();
31+
32+
$user->userFilaments()->updateOrCreate([
33+
'machine_id' => $machine->id,
34+
'filament_id' => $filament->id,
35+
'color_id' => $color->id,
36+
], $content);
37+
}
38+
39+
protected function filament(string $vendor, string $filament): Filament
40+
{
41+
$path = $vendor . '/filament/' . $filament . '.json';
42+
43+
$detected = $this->filamentType->detect($vendor, $filament, $path);
44+
45+
return Filament::query()
46+
->whereRelation('vendor', 'title', 'ilike', '%' . $vendor . '%')
47+
->whereRelation('type', 'title', $detected)
48+
->firstOrFail();
49+
}
50+
51+
protected function vendor(string $name): string
52+
{
53+
return Str::of($name)->before(' ')->trim()->toString();
54+
}
55+
}

bootstrap/app.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
return Application::configure(basePath: dirname(__DIR__))
1212
->withRouting(
13-
web: __DIR__ . '/../routes/web.php',
14-
commands: __DIR__ . '/../routes/console.php',
15-
health: '/up',
13+
web : __DIR__ . '/../routes/web.php',
14+
commands: __DIR__ . '/../routes/playground.php',
15+
health : '/up',
1616
)
1717
->withMiddleware(function (Middleware $middleware): void {
1818
$middleware->web(append: [

0 commit comments

Comments
 (0)