1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config\Parser\Annotation; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
8
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
9
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
use ReflectionException; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use ReflectionProperty; |
14
|
|
|
use RuntimeException; |
15
|
|
|
use function class_exists; |
16
|
|
|
|
17
|
|
|
class GraphClass extends ReflectionClass |
18
|
|
|
{ |
19
|
|
|
private static ?AnnotationReader $annotationReader = null; |
20
|
|
|
|
21
|
|
|
protected array $annotations = []; |
22
|
|
|
|
23
|
|
|
protected array $propertiesExtended = []; |
24
|
|
|
|
25
|
25 |
|
public function __construct(string $className) |
26
|
|
|
{ |
27
|
25 |
|
parent::__construct($className); |
28
|
|
|
|
29
|
25 |
|
$annotationReader = self::getAnnotationReader(); |
30
|
25 |
|
$this->annotations = $annotationReader->getClassAnnotations($this); |
31
|
|
|
|
32
|
25 |
|
$reflection = $this; |
33
|
|
|
do { |
34
|
25 |
|
foreach ($reflection->getProperties() as $property) { |
35
|
24 |
|
if (isset($this->propertiesExtended[$property->getName()])) { |
36
|
24 |
|
continue; |
37
|
|
|
} |
38
|
24 |
|
$this->propertiesExtended[$property->getName()] = $property; |
39
|
|
|
} |
40
|
25 |
|
} while ($reflection = $reflection->getParentClass()); |
41
|
25 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get an array of parent class names. |
45
|
|
|
* |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
|
|
public function getParents(): array |
49
|
|
|
{ |
50
|
|
|
$parents = []; |
51
|
|
|
$class = $this; |
52
|
|
|
while ($parent = $class->getParentClass()) { |
53
|
|
|
$parents[] = $parent->getName(); |
54
|
|
|
$class = $parent; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $parents; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the list of methods name. |
62
|
|
|
* |
63
|
|
|
* @return string[] |
64
|
|
|
*/ |
65
|
|
|
public function getMethodsNames(): array |
66
|
|
|
{ |
67
|
|
|
return array_map(fn (ReflectionMethod $method) => $method->getName(), $this->getMethods()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getMethodAnnotations(string $name): array |
71
|
|
|
{ |
72
|
|
|
return self::getAnnotationReader()->getMethodAnnotations($this->getMethod($name)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getPropertyAnnotations(string $name): array |
76
|
|
|
{ |
77
|
|
|
return self::getAnnotationReader()->getPropertyAnnotations($this->getProperty($name)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return ReflectionProperty[] |
82
|
|
|
*/ |
83
|
25 |
|
public function getPropertiesExtended() |
84
|
|
|
{ |
85
|
25 |
|
return $this->propertiesExtended; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getPropertyExtended(string $name): ReflectionProperty |
89
|
|
|
{ |
90
|
|
|
if (!isset($this->propertiesExtended[$name])) { |
91
|
|
|
throw new ReflectionException(sprintf('Missing property %s on class or parent class %s', $name, $this->getName())); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->propertiesExtended[$name]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param ReflectionMethod|ReflectionProperty|null $from |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
25 |
|
public function getAnnotations(object $from = null) |
103
|
|
|
{ |
104
|
25 |
|
if (!$from) { |
105
|
25 |
|
return $this->annotations; |
106
|
|
|
} |
107
|
|
|
|
108
|
24 |
|
if ($from instanceof ReflectionMethod) { |
109
|
24 |
|
return self::getAnnotationReader()->getMethodAnnotations($from); |
110
|
|
|
} |
111
|
|
|
|
112
|
24 |
|
if ($from instanceof ReflectionProperty) { |
113
|
24 |
|
return self::getAnnotationReader()->getPropertyAnnotations($from); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
throw new AnnotationException(sprintf('Unable to retrieve annotations from object of class "%s".', get_class($from))); |
117
|
|
|
} |
118
|
|
|
|
119
|
25 |
|
private static function getAnnotationReader(): AnnotationReader |
120
|
|
|
{ |
121
|
25 |
|
if (null === self::$annotationReader) { |
122
|
1 |
|
if (!class_exists(AnnotationReader::class) || |
123
|
1 |
|
!class_exists(AnnotationRegistry::class)) { |
124
|
|
|
throw new RuntimeException('In order to use graphql annotation, you need to require doctrine annotations'); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
128
|
1 |
|
self::$annotationReader = new AnnotationReader(); |
129
|
|
|
} |
130
|
|
|
|
131
|
25 |
|
return self::$annotationReader; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.