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