1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine\Factory\MetadataReader; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
8
|
|
|
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver; |
9
|
|
|
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; |
10
|
|
|
use GraphQL\Doctrine\Exception; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use ReflectionProperty; |
14
|
|
|
|
15
|
|
|
final class MappingDriverChainAdapter implements Reader |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var MappingDriverChain |
19
|
|
|
*/ |
20
|
|
|
private $chainDriver; |
21
|
|
|
|
22
|
3 |
|
public function __construct(MappingDriverChain $chainDriver) |
23
|
|
|
{ |
24
|
3 |
|
$this->chainDriver = $chainDriver; |
25
|
3 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Find the reader for the class |
29
|
|
|
*/ |
30
|
3 |
|
private function findReader(ReflectionClass $class): Reader |
31
|
|
|
{ |
32
|
3 |
|
$className = $class->getName(); |
33
|
3 |
|
foreach ($this->chainDriver->getDrivers() as $namespace => $driver) { |
34
|
1 |
|
if (mb_stripos($className, $namespace) === 0) { |
35
|
1 |
|
if ($driver instanceof AnnotationDriver) { |
36
|
1 |
|
return $driver->getReader(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
if ($this->chainDriver->getDefaultDriver() instanceof AnnotationDriver) { |
42
|
1 |
|
return $this->chainDriver->getDefaultDriver()->getReader(); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
throw new Exception('graphql-doctrine requires ' . $className . ' entity to be configured with a `' . AnnotationDriver::class . '`.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets the annotations applied to a class. |
50
|
|
|
* |
51
|
|
|
* @param ReflectionClass $class the ReflectionClass of the class from which |
52
|
|
|
* the class annotations should be read |
53
|
|
|
* |
54
|
|
|
* @return array an array of Annotations |
55
|
|
|
*/ |
56
|
|
|
public function getClassAnnotations(ReflectionClass $class) |
57
|
|
|
{ |
58
|
|
|
return $this->findReader($class) |
59
|
|
|
->getClassAnnotations($class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets a class annotation. |
64
|
|
|
* |
65
|
|
|
* @param ReflectionClass $class the ReflectionClass of the class from which |
66
|
|
|
* the class annotations should be read |
67
|
|
|
* @param string $annotationName the name of the annotation |
68
|
|
|
* |
69
|
|
|
* @return null|object the Annotation or NULL, if the requested annotation does not exist |
70
|
|
|
*/ |
71
|
|
|
public function getClassAnnotation(ReflectionClass $class, $annotationName) |
72
|
|
|
{ |
73
|
|
|
return $this->findReader($class) |
74
|
|
|
->getClassAnnotation($class, $annotationName); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets the annotations applied to a method. |
79
|
|
|
* |
80
|
|
|
* @param ReflectionMethod $method the ReflectionMethod of the method from which |
81
|
|
|
* the annotations should be read |
82
|
|
|
* |
83
|
|
|
* @return array an array of Annotations |
84
|
|
|
*/ |
85
|
|
|
public function getMethodAnnotations(ReflectionMethod $method) |
86
|
|
|
{ |
87
|
|
|
return $this->findReader($method->getDeclaringClass()) |
88
|
|
|
->getMethodAnnotations($method); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets a method annotation. |
93
|
|
|
* |
94
|
|
|
* @param ReflectionMethod $method the ReflectionMethod to read the annotations from |
95
|
|
|
* @param string $annotationName the name of the annotation |
96
|
|
|
* |
97
|
|
|
* @return null|object the Annotation or NULL, if the requested annotation does not exist |
98
|
|
|
*/ |
99
|
3 |
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName) |
100
|
|
|
{ |
101
|
3 |
|
return $this->findReader($method->getDeclaringClass()) |
102
|
2 |
|
->getMethodAnnotation($method, $annotationName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets the annotations applied to a property. |
107
|
|
|
* |
108
|
|
|
* @param ReflectionProperty $property the ReflectionProperty of the property |
109
|
|
|
* from which the annotations should be read |
110
|
|
|
* |
111
|
|
|
* @return array an array of Annotations |
112
|
|
|
*/ |
113
|
|
|
public function getPropertyAnnotations(ReflectionProperty $property) |
114
|
|
|
{ |
115
|
|
|
return $this->findReader($property->getDeclaringClass()) |
116
|
|
|
->getPropertyAnnotations($property); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets a property annotation. |
121
|
|
|
* |
122
|
|
|
* @param ReflectionProperty $property the ReflectionProperty to read the annotations from |
123
|
|
|
* @param string $annotationName the name of the annotation |
124
|
|
|
* |
125
|
|
|
* @return null|object the Annotation or NULL, if the requested annotation does not exist |
126
|
|
|
*/ |
127
|
|
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) |
128
|
|
|
{ |
129
|
|
|
return $this->findReader($property->getDeclaringClass()) |
130
|
|
|
->getPropertyAnnotation($property, $annotationName); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|