1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Annotation; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
class AnnotationManager |
10
|
|
|
{ |
11
|
|
|
/** @var \Doctrine\Common\Annotations\Reader */ |
12
|
|
|
protected $reader; |
13
|
|
|
|
14
|
|
|
/** @var \Psr\SimpleCache\CacheInterface */ |
15
|
|
|
protected $cache; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param \Doctrine\Common\Annotations\Reader $reader |
19
|
|
|
* @param \Psr\SimpleCache\CacheInterface $cache |
20
|
|
|
*/ |
21
|
1 |
|
public function __construct(Reader $reader = null, CacheInterface $cache = null) |
22
|
|
|
{ |
23
|
1 |
|
$this->reader = $reader ?? new AnnotationReader(); |
24
|
1 |
|
$this->cache = $cache; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $className |
29
|
|
|
* @return \Wandu\Annotation\AnnotationBag |
30
|
|
|
*/ |
31
|
1 |
|
public function read(string $className): AnnotationBag |
32
|
|
|
{ |
33
|
1 |
|
if ($this->cache && $this->cache->has($className)) { |
34
|
|
|
return $this->cache->get($className); |
35
|
|
|
} |
36
|
1 |
|
$reflClass = new ReflectionClass($className); |
37
|
1 |
|
$classAnnotations = []; |
38
|
1 |
|
foreach ($this->reader->getClassAnnotations($reflClass) as $classAnnotation) { |
39
|
1 |
|
$classAnnotations[] = $classAnnotation; |
40
|
|
|
} |
41
|
1 |
|
$propsAnnotations = []; |
42
|
1 |
|
foreach ($reflClass->getProperties() as $reflProperty) { |
43
|
1 |
|
$propsAnnotations[$reflProperty->getName()] = []; |
44
|
1 |
|
foreach ($this->reader->getPropertyAnnotations($reflProperty) as $propertyAnnotation) { |
45
|
1 |
|
$propsAnnotations[$reflProperty->getName()][] = $propertyAnnotation; |
46
|
|
|
} |
47
|
|
|
} |
48
|
1 |
|
$methodsAnnotations = []; |
49
|
1 |
|
foreach ($reflClass->getMethods() as $reflMethod) { |
50
|
1 |
|
$methodsAnnotations[$reflMethod->getName()] = []; |
|
|
|
|
51
|
1 |
|
foreach ($this->reader->getMethodAnnotations($reflMethod) as $methodAnnotation) { |
52
|
1 |
|
$methodsAnnotations[$reflMethod->getName()][] = $methodAnnotation; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
1 |
|
$result = new AnnotationBag($classAnnotations, $propsAnnotations, $methodsAnnotations); |
56
|
1 |
|
if ($this->cache) { |
57
|
|
|
$this->cache->set($className, $result); |
58
|
|
|
} |
59
|
1 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|