1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flying\Struct\Metadata; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Flying\Struct\ConfigurationManager; |
7
|
|
|
use Flying\Struct\StructInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Structures metadata manager |
11
|
|
|
*/ |
12
|
|
|
class MetadataManager implements MetadataManagerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Structures metadata parser |
16
|
|
|
* |
17
|
|
|
* @var MetadataParserInterface |
18
|
|
|
*/ |
19
|
|
|
private $parser; |
20
|
|
|
/** |
21
|
|
|
* Structures metadata cache |
22
|
|
|
* |
23
|
|
|
* @var Cache |
24
|
|
|
*/ |
25
|
|
|
private $cache; |
26
|
|
|
/** |
27
|
|
|
* Structures metadata |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $metadata = []; |
32
|
|
|
/** |
33
|
|
|
* Prefix for cache entries for structure metadata |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $cachePrefix = 'StructMetadata_'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get structure metadata information for given structure |
41
|
|
|
* |
42
|
|
|
* @param string|StructInterface $struct Either structure class name or instance of structure object |
43
|
|
|
* to get metadata for |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
* @return StructMetadata|null |
46
|
|
|
*/ |
47
|
188 |
|
public function getMetadata($struct) |
48
|
|
|
{ |
49
|
188 |
|
$class = null; |
|
|
|
|
50
|
|
|
// Get class name of given structure |
51
|
188 |
|
if (is_object($struct)) { |
52
|
163 |
|
if ($struct instanceof StructInterface) { |
53
|
162 |
|
$class = get_class($struct); |
54
|
162 |
|
} else { |
55
|
1 |
|
throw new \InvalidArgumentException('Structure class must implement StructInterface interface'); |
56
|
|
|
} |
57
|
187 |
|
} elseif (is_string($struct)) { |
58
|
|
|
try { |
59
|
183 |
|
$reflection = new \ReflectionClass($struct); |
60
|
183 |
|
} catch (\ReflectionException $e) { |
61
|
|
|
throw new \InvalidArgumentException('Invalid structure class "' . $struct . '"'); |
62
|
|
|
} |
63
|
183 |
|
if (in_array(StructInterface::class, $reflection->getInterfaceNames(), true)) { |
64
|
182 |
|
$class = $reflection->getName(); |
|
|
|
|
65
|
182 |
|
} else { |
66
|
1 |
|
throw new \InvalidArgumentException('Structure class must implement StructInterface interface'); |
67
|
|
|
} |
68
|
182 |
|
} else { |
69
|
4 |
|
throw new \InvalidArgumentException('Invalid structure information is given'); |
70
|
|
|
} |
71
|
|
|
// Check local metadata storage - fastest possible way |
72
|
182 |
|
if (array_key_exists($class, $this->metadata)) { |
73
|
67 |
|
return clone $this->metadata[$class]; |
74
|
|
|
} |
75
|
|
|
// Check metadata cache |
76
|
182 |
|
$cacheKey = $this->getCacheKey($class); |
77
|
182 |
|
if ($this->getCache()->contains($cacheKey)) { |
78
|
2 |
|
$cachedMetadata = $this->getCache()->fetch($cacheKey); |
79
|
2 |
|
if ($cachedMetadata instanceof StructMetadata) { |
80
|
1 |
|
$this->metadata[$class] = $cachedMetadata; |
81
|
1 |
|
return clone $cachedMetadata; |
82
|
|
|
} |
83
|
|
|
// Cache has incorrect or corrupted entry |
84
|
1 |
|
$this->getCache()->delete($cacheKey); |
85
|
1 |
|
} |
86
|
|
|
// Get metadata from parser |
87
|
182 |
|
$metadata = $this->getParser()->getMetadata($class); |
88
|
182 |
|
if ($metadata instanceof StructMetadata) { |
89
|
181 |
|
$this->metadata[$class] = $metadata; |
90
|
181 |
|
$this->getCache()->save($cacheKey, $metadata); |
91
|
181 |
|
return clone $metadata; |
92
|
|
|
} |
93
|
|
|
// No metadata is found for structure |
94
|
1 |
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get cache key for given class |
99
|
|
|
* |
100
|
|
|
* @param string $class |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
182 |
|
protected function getCacheKey($class) |
104
|
|
|
{ |
105
|
182 |
|
return $this->cachePrefix . str_replace('\\', '_', $class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get metadata cache |
110
|
|
|
* |
111
|
|
|
* @return Cache |
112
|
|
|
*/ |
113
|
184 |
|
public function getCache() |
114
|
|
|
{ |
115
|
184 |
|
if (!$this->cache) { |
116
|
182 |
|
$this->cache = ConfigurationManager::getConfiguration()->getCache(); |
117
|
182 |
|
} |
118
|
184 |
|
return $this->cache; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set metadata cache |
123
|
|
|
* |
124
|
|
|
* @param Cache $cache |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
6 |
|
public function setCache(Cache $cache) |
128
|
|
|
{ |
129
|
6 |
|
$this->cache = $cache; |
130
|
6 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get metadata parser |
135
|
|
|
* |
136
|
|
|
* @return MetadataParserInterface |
137
|
|
|
*/ |
138
|
185 |
|
public function getParser() |
139
|
|
|
{ |
140
|
185 |
|
if (!$this->parser) { |
141
|
183 |
|
$this->parser = ConfigurationManager::getConfiguration()->getMetadataParser(); |
142
|
183 |
|
if ($this->parser instanceof AbstractMetadataParser) { |
143
|
183 |
|
$this->parser->setMetadataManager($this); |
144
|
183 |
|
} |
145
|
183 |
|
} |
146
|
185 |
|
return $this->parser; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set metadata parser |
151
|
|
|
* |
152
|
|
|
* @param MetadataParserInterface $parser |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
7 |
|
public function setParser(MetadataParserInterface $parser) |
156
|
|
|
{ |
157
|
7 |
|
$this->parser = $parser; |
158
|
7 |
|
if ($this->parser instanceof AbstractMetadataParser) { |
159
|
2 |
|
$this->parser->setMetadataManager($this); |
160
|
2 |
|
} |
161
|
7 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.