1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\Aop; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
8
|
|
|
|
9
|
|
|
use function array_key_exists; |
10
|
|
|
use function assert; |
11
|
|
|
use function class_exists; |
12
|
|
|
use function is_object; |
13
|
|
|
use function is_string; |
14
|
|
|
use function property_exists; |
15
|
|
|
use function unserialize; |
16
|
|
|
|
17
|
|
|
final class ReflectionMethod extends \ReflectionMethod implements Reader |
18
|
|
|
{ |
19
|
|
|
/** @var ?WeavedInterface */ |
20
|
|
|
private $object; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $method = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
3 |
|
* Set dependencies |
27
|
|
|
*/ |
28
|
3 |
|
public function setObject(WeavedInterface $object, \ReflectionMethod $method): void |
29
|
3 |
|
{ |
30
|
3 |
|
$this->object = $object; |
31
|
|
|
$this->method = $method->name; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getDeclaringClass(): ReflectionClass |
35
|
2 |
|
{ |
36
|
|
|
if (! is_object($this->object)) { |
37
|
2 |
|
assert(class_exists($this->class)); |
38
|
2 |
|
|
39
|
|
|
return new ReflectionClass($this->class); |
40
|
|
|
} |
41
|
2 |
|
|
42
|
2 |
|
$parencClass = (new \ReflectionClass($this->object))->getParentClass(); |
43
|
2 |
|
assert($parencClass instanceof \ReflectionClass); |
44
|
|
|
$originalClass = $parencClass->name; |
45
|
2 |
|
assert(class_exists($originalClass)); |
46
|
|
|
$class = new ReflectionClass($originalClass); |
47
|
|
|
$class->setObject($this->object); |
48
|
|
|
|
49
|
|
|
return $class; |
50
|
|
|
} |
51
|
2 |
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
2 |
|
* |
55
|
2 |
|
* @psalm-suppress NoInterfaceProperties |
56
|
2 |
|
*/ |
57
|
1 |
|
public function getAnnotations(): array |
58
|
|
|
{ |
59
|
|
|
$object = $this->object; |
60
|
1 |
|
if (is_object($object) && property_exists($object, 'methodAnnotations') && is_string($object->methodAnnotations)) { |
61
|
|
|
return $this->getCachedAnnotations($object->methodAnnotations); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
assert(class_exists($this->class)); |
65
|
|
|
/** @var list<object> $annotations */ |
|
|
|
|
66
|
2 |
|
$annotations = (new AnnotationReader())->getMethodAnnotations(new \ReflectionMethod($this->class, $this->name)); |
67
|
|
|
|
68
|
2 |
|
return $annotations; |
69
|
2 |
|
} |
70
|
1 |
|
|
71
|
1 |
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getAnnotation(string $annotationName) |
75
|
1 |
|
{ |
76
|
|
|
$annotations = $this->getAnnotations(); |
77
|
|
|
foreach ($annotations as $annotation) { |
78
|
|
|
if ($annotation instanceof $annotationName) { |
79
|
|
|
return $annotation; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return list<object> |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
private function getCachedAnnotations(string $methodAnnotations): array |
90
|
|
|
{ |
91
|
|
|
/** @var array<string, list<object>> $annotations */ |
92
|
|
|
$annotations = unserialize((string) $methodAnnotations, ['allowed_classes' => true]); |
93
|
|
|
if (array_key_exists($this->method, $annotations)) { |
94
|
|
|
return $annotations[$this->method]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return []; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.