|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Attributes\Bridge; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
15
|
|
|
use Spiral\Attributes\ReaderInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This bridge provides the ability to use the {@see ReaderInterface} in |
|
19
|
|
|
* doctrine-dependent ({@see Reader}) classes. |
|
20
|
|
|
* |
|
21
|
|
|
* For example in {@see \Doctrine\Common\Annotations\CachedReader} class. |
|
22
|
|
|
* |
|
23
|
|
|
* <code> |
|
24
|
|
|
* // |
|
25
|
|
|
* // Creating bridge |
|
26
|
|
|
* // |
|
27
|
|
|
* $bridge = new \Spiral\Attributes\Bridge\DoctrineReaderBridge( |
|
28
|
|
|
* new \Spiral\Attributes\AttributeReader() |
|
29
|
|
|
* ); |
|
30
|
|
|
* |
|
31
|
|
|
* // |
|
32
|
|
|
* // Using bridge in doctrine-dependent class instead |
|
33
|
|
|
* // of real doctrine class. |
|
34
|
|
|
* // |
|
35
|
|
|
* $doctrine = new \Doctrine\Common\Annotations\CachedReader($bridge, $cache); |
|
36
|
|
|
* </code> |
|
37
|
|
|
*/ |
|
38
|
|
|
final class DoctrineReaderBridge implements Reader |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var ReaderInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private ReaderInterface $reader; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param ReaderInterface $reader |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(ReaderInterface $reader) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->reader = $reader; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getClassAnnotations(\ReflectionClass $class): array |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->iterableToArray( |
|
59
|
|
|
$this->reader->getClassMetadata($class) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritDoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getClassAnnotation(\ReflectionClass $class, $annotationName) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->reader->firstClassMetadata($class, $annotationName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getMethodAnnotations(\ReflectionMethod $method): array |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->iterableToArray( |
|
77
|
|
|
$this->reader->getFunctionMetadata($method) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getMethodAnnotation(\ReflectionMethod $method, $annotationName) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->reader->firstFunctionMetadata($method, $annotationName); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritDoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getPropertyAnnotations(\ReflectionProperty $property): array |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->iterableToArray( |
|
95
|
|
|
$this->reader->getPropertyMetadata($property) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->reader->firstPropertyMetadata($property, $annotationName); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param iterable<object> $meta |
|
109
|
|
|
* @return array<object> |
|
110
|
|
|
*/ |
|
111
|
|
|
private function iterableToArray(iterable $meta): array |
|
112
|
|
|
{ |
|
113
|
|
|
if ($meta instanceof \Traversable) { |
|
114
|
|
|
return \iterator_to_array($meta, false); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $meta; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|