/
SupportFormObjects.php
54 lines (40 loc) · 1.36 KB
/
SupportFormObjects.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Livewire\Features\SupportFormObjects;
use ReflectionClass;
use Livewire\ComponentHook;
use ReflectionNamedType;
use function Livewire\on;
class SupportFormObjects extends ComponentHook
{
public static function provide()
{
app('livewire')->propertySynthesizer(
FormObjectSynth::class
);
}
function boot()
{
$this->initializeFormObjects();
}
protected function initializeFormObjects()
{
foreach ((new ReflectionClass($this->component))->getProperties() as $property) {
// Public properties only...
if ($property->isPublic() !== true) continue;
// Uninitialized properties only...
if ($property->isInitialized($this->component)) continue;
$type = $property->getType();
if (! $type instanceof ReflectionNamedType) continue;
$typeName = $type->getName();
// "Form" object property types only...
if (! is_subclass_of($typeName, Form::class)) continue;
$form = new $typeName(
$this->component,
$name = $property->getName()
);
$callBootMethod = FormObjectSynth::bootFormObject($this->component, $form, $name);
$property->setValue($this->component, $form);
$callBootMethod();
}
}
}