1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Config\Parser\ClassParser; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
8
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
9
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
10
|
|
|
use Overblog\GraphQLBundle\Annotation\Annotation; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionException; |
13
|
|
|
use ReflectionMethod; |
14
|
|
|
use ReflectionProperty; |
15
|
|
|
use Reflector; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use function class_exists; |
18
|
|
|
|
19
|
|
|
class GraphClass extends ReflectionClass |
20
|
|
|
{ |
21
|
|
|
private static ?AnnotationReader $annotationReader = null; |
22
|
|
|
|
23
|
|
|
protected array $annotations = []; |
24
|
|
|
protected array $propertiesExtended = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed $className |
28
|
|
|
* |
29
|
|
|
* @throws ReflectionException |
30
|
|
|
*/ |
31
|
|
|
public function __construct($className) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($className); |
34
|
|
|
|
35
|
|
|
$annotationReader = self::getAnnotationReader(); |
36
|
|
|
$this->annotations = $annotationReader->getClassAnnotations($this); |
37
|
|
|
|
38
|
|
|
$reflection = $this; |
39
|
|
|
do { |
40
|
|
|
foreach ($reflection->getProperties() as $property) { |
41
|
|
|
if (isset($this->propertiesExtended[$property->getName()])) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
$this->propertiesExtended[$property->getName()] = $property; |
45
|
|
|
} |
46
|
|
|
} while ($reflection = $reflection->getParentClass()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return ReflectionProperty[] |
51
|
|
|
*/ |
52
|
|
|
public function getPropertiesExtended() |
53
|
|
|
{ |
54
|
|
|
return $this->propertiesExtended; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @phpstan-param ReflectionMethod|ReflectionProperty|null $from |
59
|
|
|
* |
60
|
|
|
* @return array<Annotation> |
61
|
|
|
* |
62
|
|
|
* @throws AnnotationException |
63
|
|
|
*/ |
64
|
|
|
public function getMetadatas(Reflector $from = null): array |
65
|
|
|
{ |
66
|
|
|
switch (true) { |
67
|
|
|
case null === $from: |
68
|
|
|
return $this->annotations; |
69
|
|
|
case $from instanceof ReflectionMethod: |
70
|
|
|
// @phpstan-ignore-next-line |
71
|
|
|
return self::getAnnotationReader()->getMethodAnnotations($from); |
72
|
|
|
case $from instanceof ReflectionProperty: |
73
|
|
|
// @phpstan-ignore-next-line |
74
|
|
|
return self::getAnnotationReader()->getPropertyAnnotations($from); |
75
|
|
|
default: |
76
|
|
|
throw new AnnotationException(sprintf('Unable to retrieve annotations from object of class "%s".', get_class($from))); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private static function getAnnotationReader(): AnnotationReader |
81
|
|
|
{ |
82
|
|
|
if (null === self::$annotationReader) { |
83
|
|
|
if (!class_exists(AnnotationReader::class) || |
84
|
|
|
!class_exists(AnnotationRegistry::class)) { |
85
|
|
|
throw new RuntimeException('In order to use graphql annotation, you need to require doctrine annotations'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
89
|
|
|
self::$annotationReader = new AnnotationReader(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return self::$annotationReader; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.