1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Spiral\Annotations; |
11
|
|
|
|
12
|
|
|
final class Reader |
13
|
|
|
{ |
14
|
|
|
/** @var Parser */ |
15
|
|
|
private $classParser; |
16
|
|
|
|
17
|
|
|
/** @var Parser */ |
18
|
|
|
private $methodParser; |
19
|
|
|
|
20
|
|
|
/** @var Parser */ |
21
|
|
|
private $propertyParser; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Parser|null $parser |
25
|
|
|
* @param array $classAnnotations |
26
|
|
|
* @param array $methodAnnotations |
27
|
|
|
* @param array $propertyAnnotations |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
Parser $parser = null, |
31
|
|
|
array $classAnnotations = [], |
32
|
|
|
array $methodAnnotations = [], |
33
|
|
|
array $propertyAnnotations = [] |
34
|
|
|
) { |
35
|
|
|
$parser = $parser ?? new Parser(); |
36
|
|
|
|
37
|
|
|
$this->classParser = clone $parser; |
38
|
|
|
foreach ($classAnnotations as $annotation) { |
39
|
|
|
$this->classParser->register($annotation); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->methodParser = clone $parser; |
43
|
|
|
foreach ($methodAnnotations as $annotation) { |
44
|
|
|
$this->methodParser->register($annotation); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->propertyParser = clone $parser; |
48
|
|
|
foreach ($propertyAnnotations as $annotation) { |
49
|
|
|
$this->propertyParser->register($annotation); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all annotations found in class doc comment. |
55
|
|
|
* |
56
|
|
|
* @param \ReflectionClass $class |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getClassAnnotations(\ReflectionClass $class): array |
60
|
|
|
{ |
61
|
|
|
return $this->classParser->parse($class->getDocComment()); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get all annotations found in class method doc comment. |
66
|
|
|
* |
67
|
|
|
* @param \ReflectionClass $class |
68
|
|
|
* @param string $method |
69
|
|
|
* @return array |
70
|
|
|
* |
71
|
|
|
* @throws \ReflectionException |
72
|
|
|
*/ |
73
|
|
|
public function getMethodAnnotations(\ReflectionClass $class, string $method): array |
74
|
|
|
{ |
75
|
|
|
return $this->classParser->parse($class->getMethod($method)->getDocComment()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get all annotations found in class property doc comment. |
80
|
|
|
* |
81
|
|
|
* @param \ReflectionClass $class |
82
|
|
|
* @param string $property |
83
|
|
|
* @return array |
84
|
|
|
* |
85
|
|
|
* @throws \ReflectionException |
86
|
|
|
*/ |
87
|
|
|
public function getPropertyAnnotations(\ReflectionClass $class, string $property) |
88
|
|
|
{ |
89
|
|
|
return $this->classParser->parse($class->getProperty($property)->getDocComment()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |