|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flying\Struct\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
use Flying\Struct\ConfigurationManager; |
|
6
|
|
|
use Flying\Struct\Exception; |
|
7
|
|
|
use Flying\Struct\Property\PropertyInterface; |
|
8
|
|
|
use Flying\Struct\StructInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Base implementation of structures metadata parser |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractMetadataParser implements MetadataParserInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Namespaces for property classes |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $nsProperty = []; |
|
21
|
|
|
/** |
|
22
|
|
|
* Namespaces for structure classes |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $nsStruct = []; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var MetadataManagerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $metadataManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get structure metadata information for given class |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $class Structure class name to parse metadata from |
|
36
|
|
|
* |
|
37
|
|
|
* @throws Exception |
|
38
|
|
|
* @return StructMetadata |
|
39
|
|
|
* @throws \InvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
182 |
|
public function getMetadata($class) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
182 |
|
$reflection = new \ReflectionClass($class); |
|
45
|
182 |
|
$parent = $reflection->getParentClass(); |
|
46
|
182 |
|
$metadata = null; |
|
47
|
182 |
|
if (($parent instanceof \ReflectionClass) && $parent->implementsInterface(StructInterface::class)) { |
|
48
|
182 |
|
$metadata = $this->getMetadataManager()->getMetadata($parent->getName()); |
|
|
|
|
|
|
49
|
182 |
|
} |
|
50
|
182 |
|
if (!$metadata instanceof StructMetadata) { |
|
51
|
182 |
|
$metadata = new StructMetadata(); |
|
52
|
182 |
|
} |
|
53
|
182 |
|
$metadata->setClass($class); |
|
54
|
182 |
|
$this->parseMetadata($reflection, $metadata); |
|
55
|
181 |
|
$reflection = new \ReflectionClass($class); |
|
56
|
181 |
|
if ($reflection->implementsInterface(MetadataModificationInterface::class)) { |
|
57
|
|
|
/** @var $class MetadataModificationInterface */ |
|
58
|
162 |
|
$class::modifyMetadata($metadata); |
|
59
|
162 |
|
} |
|
60
|
182 |
|
} catch (\ReflectionException $e) { |
|
61
|
|
|
throw new \InvalidArgumentException('Failed to obtain exception for metadata class "' . $class . '"'); |
|
62
|
|
|
} |
|
63
|
181 |
|
return $metadata; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return MetadataManagerInterface |
|
68
|
|
|
*/ |
|
69
|
184 |
|
public function getMetadataManager() |
|
70
|
|
|
{ |
|
71
|
184 |
|
if (!$this->metadataManager) { |
|
72
|
16 |
|
$this->metadataManager = ConfigurationManager::getConfiguration()->getMetadataManager(); |
|
73
|
16 |
|
} |
|
74
|
184 |
|
return $this->metadataManager; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param MetadataManagerInterface $metadataManager |
|
79
|
|
|
*/ |
|
80
|
185 |
|
public function setMetadataManager(MetadataManagerInterface $metadataManager) |
|
81
|
|
|
{ |
|
82
|
185 |
|
$this->metadataManager = $metadataManager; |
|
83
|
185 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Actual implementation of structure metadata parsing |
|
87
|
|
|
* |
|
88
|
|
|
* @param \ReflectionClass $reflection |
|
89
|
|
|
* @param StructMetadata $metadata |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
abstract protected function parseMetadata(\ReflectionClass $reflection, StructMetadata $metadata); |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Resolve given property type into property FQCN |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $class Structure property class |
|
98
|
|
|
* @return string|null |
|
99
|
|
|
* @throws \InvalidArgumentException |
|
100
|
|
|
*/ |
|
101
|
179 |
View Code Duplication |
protected function resolvePropertyClass($class) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
179 |
|
if (empty($this->nsProperty)) { |
|
104
|
179 |
|
$ns = ConfigurationManager::getConfiguration()->getPropertyNamespacesMap()->getAll(); |
|
105
|
179 |
|
$this->nsProperty = array_reverse($ns, true); |
|
106
|
179 |
|
} |
|
107
|
179 |
|
return $this->resolveClass($class, $this->nsProperty, PropertyInterface::class); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Resolve given class into FQCN class and check if it supports given namespace |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $class Class name to resolve |
|
114
|
|
|
* @param array $namespaces List of namespace to use to expand given class name |
|
115
|
|
|
* @param string $interface OPTIONAL Interface, class must implement |
|
116
|
|
|
* @return string|null |
|
117
|
|
|
*/ |
|
118
|
181 |
|
protected function resolveClass($class, array $namespaces, $interface = null) |
|
119
|
|
|
{ |
|
120
|
181 |
|
$class = ucfirst(trim($class, '\\')); |
|
121
|
181 |
View Code Duplication |
if (class_exists($class) && (($interface === null) || in_array($interface, class_implements($class), true))) { |
|
|
|
|
|
|
122
|
|
|
return $class; |
|
123
|
|
|
} |
|
124
|
181 |
|
foreach ($namespaces as $ns) { |
|
125
|
181 |
|
$fqcn = $ns . '\\' . $class; |
|
126
|
181 |
View Code Duplication |
if (class_exists($fqcn) && (($interface === null) || in_array($interface, class_implements($fqcn), true))) { |
|
|
|
|
|
|
127
|
178 |
|
return $fqcn; |
|
128
|
|
|
} |
|
129
|
20 |
|
} |
|
130
|
3 |
|
return null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Resolve given structure class into structure FQCN |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $class Structure class |
|
137
|
|
|
* @return string|null |
|
138
|
|
|
* @throws \InvalidArgumentException |
|
139
|
|
|
*/ |
|
140
|
67 |
View Code Duplication |
protected function resolveStructClass($class) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
67 |
|
if (empty($this->nsStruct)) { |
|
143
|
67 |
|
$ns = ConfigurationManager::getConfiguration()->getStructNamespacesMap()->getAll(); |
|
144
|
67 |
|
$this->nsStruct = array_reverse($ns, true); |
|
145
|
67 |
|
} |
|
146
|
67 |
|
return $this->resolveClass($class, $this->nsStruct, StructInterface::class); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|