Skip to content

Commit 9053712

Browse files
committed
Field Inspection Service
1 parent 538521c commit 9053712

File tree

3 files changed

+143
-13
lines changed

3 files changed

+143
-13
lines changed

src/Concerns/HasFieldsMapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function mutateFormDataBeforeFill(array $data): array
6464
return $fieldInstance->mutateFormDataCallback($this->record, $field, $data);
6565
}
6666

67-
$data['setting'][$field->slug] = $this->record->values[$field->slug] ?? null;
67+
$data['values'][$field->slug] = $this->record->values[$field->slug] ?? null;
6868

6969
return $data;
7070
});
@@ -106,7 +106,7 @@ private function resolveFormFields(): array
106106
$customFields = $this->resolveCustomFields();
107107

108108
return $this->record->fields
109-
->map(fn ($field) => $this->resolveFieldInput($field, $customFields))
109+
->map(fn($field) => $this->resolveFieldInput($field, $customFields))
110110
->filter()
111111
->values()
112112
->all();
@@ -115,7 +115,7 @@ private function resolveFormFields(): array
115115
private function resolveCustomFields(): Collection
116116
{
117117
return collect(Fields::getFields())
118-
->map(fn ($fieldClass) => new $fieldClass);
118+
->map(fn($fieldClass) => new $fieldClass);
119119
}
120120

121121
private function resolveFieldInput(Model $field, Collection $customFields): ?object
@@ -134,4 +134,4 @@ private function resolveFieldInput(Model $field, Collection $customFields): ?obj
134134

135135
return null;
136136
}
137-
}
137+
}

src/FieldsServiceProvider.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
namespace Vormkracht10\Fields;
44

5-
use Filament\Support\Assets\AlpineComponent;
6-
use Filament\Support\Assets\Asset;
7-
use Filament\Support\Assets\Css;
85
use Filament\Support\Assets\Js;
9-
use Filament\Support\Facades\FilamentAsset;
10-
use Filament\Support\Facades\FilamentIcon;
6+
use Filament\Support\Assets\Css;
7+
use Filament\Support\Assets\Asset;
118
use Illuminate\Filesystem\Filesystem;
12-
use Livewire\Features\SupportTesting\Testable;
13-
use Spatie\LaravelPackageTools\Commands\InstallCommand;
149
use Spatie\LaravelPackageTools\Package;
15-
use Spatie\LaravelPackageTools\PackageServiceProvider;
10+
use Filament\Support\Facades\FilamentIcon;
11+
use Filament\Support\Facades\FilamentAsset;
12+
use Filament\Support\Assets\AlpineComponent;
1613
use Vormkracht10\Fields\Testing\TestsFields;
14+
use Livewire\Features\SupportTesting\Testable;
15+
use Vormkracht10\Fields\Contracts\FieldInspector;
16+
use Spatie\LaravelPackageTools\PackageServiceProvider;
17+
use Spatie\LaravelPackageTools\Commands\InstallCommand;
18+
use Vormkracht10\Fields\Services\FieldInspectionService;
1719

1820
class FieldsServiceProvider extends PackageServiceProvider
1921
{
@@ -86,6 +88,8 @@ public function packageBooted(): void
8688

8789
// Testing
8890
Testable::mixin(new TestsFields);
91+
92+
$this->app->bind(FieldInspector::class, FieldInspectionService::class);
8993
}
9094

9195
protected function getAssetPackageName(): ?string
@@ -146,4 +150,4 @@ protected function getMigrations(): array
146150
'create_fields_table',
147151
];
148152
}
149-
}
153+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Vormkracht10\Fields\Services;
4+
5+
use ReflectionClass;
6+
use Vormkracht10\Fields\Contracts\FieldInspector;
7+
use Illuminate\Support\Str;
8+
use ReflectionMethod;
9+
use ReflectionProperty;
10+
use Vormkracht10\Fields\Facades\Fields;
11+
12+
class FieldInspectionService implements FieldInspector
13+
{
14+
public function initializeDefaultField(string $fieldType): array
15+
{
16+
$className = 'Vormkracht10\\Fields\\Fields\\' . Str::studly($fieldType);
17+
18+
return $this->getClassDetails($className);
19+
}
20+
21+
public function initializeCustomField(string $fieldType): array
22+
{
23+
$className = Fields::getFields()[$fieldType] ?? null;
24+
25+
return $this->getClassDetails($className);
26+
}
27+
28+
private function getClassDetails(?string $className): array
29+
{
30+
if (! $className || ! class_exists($className)) {
31+
return [
32+
'exists' => false,
33+
'class' => $className,
34+
'methods' => [],
35+
'properties' => [],
36+
'constants' => [],
37+
'interfaces' => [],
38+
'instance' => null,
39+
];
40+
}
41+
42+
$reflection = new ReflectionClass($className);
43+
$instance = app($className);
44+
45+
return [
46+
'exists' => true,
47+
'class' => $className,
48+
'methods' => $this->getMethodsDetails($reflection),
49+
'properties' => $this->getPropertiesDetails($reflection),
50+
'constants' => $reflection->getConstants(),
51+
'interfaces' => $reflection->getInterfaceNames(),
52+
'instance' => $instance,
53+
'parentClass' => $reflection->getParentClass() ? $reflection->getParentClass()->getName() : null,
54+
'traits' => $reflection->getTraitNames(),
55+
];
56+
}
57+
58+
private function getMethodsDetails(ReflectionClass $reflection): array
59+
{
60+
$methods = [];
61+
foreach ($reflection->getMethods() as $method) {
62+
$methods[$method->getName()] = [
63+
'visibility' => $this->getVisibility($method),
64+
'static' => $method->isStatic(),
65+
'parameters' => $this->getParametersDetails($method),
66+
'returnType' => $method->getReturnType() ? $method->getReturnType()->getName() : null,
67+
'docComment' => $method->getDocComment() ?: null,
68+
];
69+
}
70+
71+
return $methods;
72+
}
73+
74+
private function getPropertiesDetails(ReflectionClass $reflection): array
75+
{
76+
$properties = [];
77+
foreach ($reflection->getProperties() as $property) {
78+
$properties[$property->getName()] = [
79+
'visibility' => $this->getVisibility($property),
80+
'static' => $property->isStatic(),
81+
'type' => $property->getType() ? $property->getType()->getName() : null,
82+
'docComment' => $property->getDocComment() ?: null,
83+
'defaultValue' => $this->getPropertyDefaultValue($property),
84+
];
85+
}
86+
87+
return $properties;
88+
}
89+
90+
private function getParametersDetails(ReflectionMethod $method): array
91+
{
92+
$parameters = [];
93+
foreach ($method->getParameters() as $param) {
94+
$parameters[$param->getName()] = [
95+
'type' => $param->getType() ? $param->getType()->getName() : null,
96+
'hasDefaultValue' => $param->isDefaultValueAvailable(),
97+
'defaultValue' => $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null,
98+
'isVariadic' => $param->isVariadic(),
99+
'isPassedByReference' => $param->isPassedByReference(),
100+
];
101+
}
102+
103+
return $parameters;
104+
}
105+
106+
private function getVisibility($reflection): string
107+
{
108+
if ($reflection->isPrivate()) {
109+
return 'private';
110+
}
111+
if ($reflection->isProtected()) {
112+
return 'protected';
113+
}
114+
115+
return 'public';
116+
}
117+
118+
private function getPropertyDefaultValue(ReflectionProperty $property): mixed
119+
{
120+
try {
121+
return $property->getDefaultValue();
122+
} catch (\ReflectionException $e) {
123+
return null;
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)