1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleAnnotation; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use SimpleAnnotation\Concerns\CacheInterface; |
8
|
|
|
use SimpleAnnotation\Concerns\ParsedAnnotationInterface; |
9
|
|
|
use SimpleAnnotation\Concerns\ParserInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait AnnotationTrait |
13
|
|
|
* |
14
|
|
|
* @package SimpleAnnotation |
15
|
|
|
*/ |
16
|
|
|
trait AnnotationTrait |
17
|
|
|
{ |
18
|
|
|
/** @var ReflectionClass */ |
19
|
|
|
private ReflectionClass $reflectionClass; |
20
|
|
|
|
21
|
|
|
/** @var ParserInterface */ |
22
|
|
|
private ParserInterface $annotationParser; |
23
|
|
|
|
24
|
|
|
/** @var ParsedAnnotationInterface[] */ |
25
|
|
|
private array $methodsAnnotations = []; |
26
|
|
|
|
27
|
|
|
/** @var ParsedAnnotationInterface[] */ |
28
|
|
|
private array $propertiesAnnotations = []; |
29
|
|
|
|
30
|
|
|
/** @var CacheInterface */ |
31
|
|
|
private $cache; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Annotation parser setter. |
35
|
|
|
* |
36
|
|
|
* @param ParserInterface $annotationParser |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
1 |
|
public function setAnnotationParser(ParserInterface $annotationParser) |
40
|
|
|
{ |
41
|
1 |
|
$this->annotationParser = $annotationParser; |
42
|
|
|
|
43
|
1 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Cache handler setter. |
48
|
|
|
* |
49
|
|
|
* @param ?CacheInterface $cache |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
6 |
|
public function setCacheHandler(?CacheInterface $cache = null) |
53
|
|
|
{ |
54
|
6 |
|
$this->cache = $cache; |
55
|
|
|
|
56
|
6 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns an array with the annotations of all properties of the class. |
61
|
|
|
* |
62
|
|
|
* @return ParsedAnnotationInterface[] |
63
|
|
|
*/ |
64
|
7 |
|
public final function getPropertiesAnnotations() : array |
65
|
|
|
{ |
66
|
7 |
|
if ($this->cache !== null && $this->cache->has('properties')) { |
67
|
1 |
|
return $this->cache->get('properties'); |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
foreach ($this->reflectionClass->getProperties() as $property) { |
71
|
6 |
|
$this->propertiesAnnotations[$property->name] = $this->annotationParser->parse((string)$property->getDocComment()); |
72
|
|
|
} |
73
|
|
|
|
74
|
7 |
|
$this->cache !== null && $this->cache->set('properties', $this->propertiesAnnotations); |
75
|
|
|
|
76
|
7 |
|
return $this->propertiesAnnotations; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the annotations of the property given by the $key parameter. |
81
|
|
|
* |
82
|
|
|
* @param string $key |
83
|
|
|
* @return ParsedAnnotationInterface |
84
|
|
|
* @throws ReflectionException |
85
|
|
|
*/ |
86
|
4 |
|
public final function getPropertyAnnotations(string $key) |
87
|
|
|
{ |
88
|
4 |
|
if ($this->cache !== null && $this->cache->has('properties')) { |
89
|
1 |
|
return ((array)$this->cache->get('properties'))[$key]; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
if ($this->cache !== null) { |
93
|
1 |
|
$this->getPropertiesAnnotations(); |
94
|
1 |
|
$this->cache->set('properties', $this->propertiesAnnotations); |
95
|
|
|
} else { |
96
|
3 |
|
$this->propertiesAnnotations[$key] = $this->annotationParser->parse((string)$this->reflectionClass->getProperty($key)->getDocComment()); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
return $this->propertiesAnnotations[$key]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns an array with the annotations of all methods of the class. |
104
|
|
|
* |
105
|
|
|
* @return ParsedAnnotationInterface[] |
106
|
|
|
*/ |
107
|
7 |
|
public final function getMethodsAnnotations() : array |
108
|
|
|
{ |
109
|
7 |
|
if ($this->cache !== null && $this->cache->has('methods')) { |
110
|
1 |
|
return $this->cache->get('methods'); |
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
foreach ($this->reflectionClass->getMethods() as $method) { |
114
|
6 |
|
$this->methodsAnnotations[$method->name] = $this->annotationParser->parse((string)$method->getDocComment()); |
115
|
|
|
} |
116
|
|
|
|
117
|
7 |
|
$this->cache !== null && $this->cache->set('methods', $this->methodsAnnotations); |
118
|
|
|
|
119
|
7 |
|
return $this->methodsAnnotations; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the annotations of the method given by the $key parameter. |
124
|
|
|
* |
125
|
|
|
* @param string $key |
126
|
|
|
* @return ParsedAnnotationInterface |
127
|
|
|
* @throws ReflectionException |
128
|
|
|
*/ |
129
|
4 |
|
public final function getMethodAnnotations(string $key) |
130
|
|
|
{ |
131
|
4 |
|
if ($this->cache !== null && $this->cache->has('methods')) { |
132
|
1 |
|
return ((array)$this->cache->get('methods'))[$key]; |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
if ($this->cache !== null) { |
136
|
1 |
|
$this->getMethodsAnnotations(); |
137
|
1 |
|
$this->cache->set('methods', $this->methodsAnnotations); |
138
|
|
|
} else { |
139
|
3 |
|
$this->methodsAnnotations[$key] = $this->annotationParser->parse((string)$this->reflectionClass->getMethod($key)->getDocComment()); |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
return $this->methodsAnnotations[$key]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return the annotations of the given class. |
147
|
|
|
* |
148
|
|
|
* @return ParsedAnnotationInterface |
149
|
|
|
*/ |
150
|
4 |
|
public final function getClassAnnotations() |
151
|
|
|
{ |
152
|
4 |
|
if ($this->cache !== null && $this->cache->has('class')) { |
153
|
1 |
|
return $this->cache->get('class'); |
154
|
|
|
} |
155
|
|
|
|
156
|
4 |
|
$classAnnotation = $this->annotationParser->parse((string)$this->reflectionClass->getDocComment()); |
157
|
|
|
|
158
|
4 |
|
if ($this->cache !== null) { |
159
|
1 |
|
$this->cache->set('class', $classAnnotation); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
return $classAnnotation; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|