-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMetadata.php
More file actions
173 lines (148 loc) · 4.84 KB
/
Metadata.php
File metadata and controls
173 lines (148 loc) · 4.84 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace DataLoader;
use ArrayObject;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
use ReflectionProperty;
use function current;
/**
* Metadata class for storing property information
*/
class Metadata extends ArrayObject
{
private static ?self $instance = null;
/**
* Resolve the metadata for the classes
*
* @param string[] $classes
* @throws ReflectionException
*/
public function resolve(string ...$classes): static
{
foreach ($classes as $class) {
$reflection = new ReflectionClass($class);
$classLoader = $this->getClassLoader($reflection);
$properties = [];
foreach ($reflection->getProperties(filter: ReflectionProperty::IS_PUBLIC) as $reflectionProperty) {
$property = current($reflectionProperty->getAttributes(Property::class));
$property = $property ? $property->newInstance() : new Property(from: $reflectionProperty->getName());
// Set the property name and from attributes
$property->origin = $class;
$property->name ??= $reflectionProperty->getName();
$property->from ??= $reflectionProperty->getName();
$property->type ??= $this->getPropertyType($reflectionProperty);
// resolve value loader
$property->loader ??= $this->getPropertyLoader($reflectionProperty) ?? $classLoader;
// save the property
$properties[$property->name] = $property;
}
$this->offsetSet($reflection->getName(), $properties);
}
return $this;
}
/**
* Try to resolve the class loader
* @param ReflectionClass $reflection
* @return callable
*/
private function getClassLoader(ReflectionClass $reflection): callable
{
$classLoader = current($reflection->getAttributes(Loader::class));
return $classLoader ? $classLoader->newInstance() : new BaseLoader();
}
/**
* Try to resolve the property loader
* @param ReflectionProperty $reflectionProperty
* @return callable|null
*/
private function getPropertyLoader(ReflectionProperty $reflectionProperty): ?callable
{
if ($filter = current($reflectionProperty->getAttributes(Loader::class))) {
return $filter->newInstance();
}
return null;
}
/**
* Try to resolve the property type
*
* @param ReflectionProperty $reflection
* @return Type|null
*/
private function getPropertyType(ReflectionProperty $reflection): ?Type
{
if ($type = current($reflection->getAttributes(Type::class))) {
return $type->newInstance();
}
if ($reflection->getType() instanceof ReflectionNamedType) {
$name = $reflection->getType()->getName();
$allowNull = $reflection->getType()->allowsNull();
// build in types
if ($reflection->getType()->isBuiltin()) {
return new Type(
name: Types::tryFrom($name) ?? trigger_error('Invalid type: ' . $name),
allowNull: $allowNull,
);
}
// enum
if (enum_exists($name)) {
return new Type(name: Types::Enum, allowNull: $allowNull, class: $name);
}
// class
if (class_exists($name)) {
return new Type(name: Types::Object, allowNull: $allowNull, class: $name);
}
}
return null;
}
/**
* Return the properties for the class
*
* @param string $documentClass
* @return array<Property>
* @throws ReflectionException
*/
public static function getProperties(string $documentClass): array
{
return self::getInstance()->offsetGet($documentClass) ?: [];
}
/**
* @param mixed $key
* @return mixed
* @throws ReflectionException
*/
public function offsetGet(mixed $key): mixed
{
if (!$this->offsetExists($key)) {
$this->resolve($key);
}
return parent::offsetGet($key);
}
/**
* Restore the metadata to the singleton instance.
*
* @retur self
* @param array $metadata
* @return Metadata
*/
public static function fromCache(array $metadata): static
{
foreach ($metadata as $key => $value) {
self::getInstance()->offsetSet($key, $value);
}
return self::$instance;
}
/**
* Get the metadata resolver instance
*
* @return static
*/
public static function getInstance(string ...$classes): static
{
if (!self::$instance) {
self::$instance = new static();
self::$instance->resolve(...$classes);
}
return self::$instance;
}
}