|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\AnnotationReader; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
18
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
|
19
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
|
23
|
|
|
* @author Daniel West <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AnnotationReader implements AnnotationReaderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use ClassMetadataTrait; |
|
28
|
|
|
|
|
29
|
|
|
protected Reader $reader; |
|
30
|
|
|
|
|
31
|
|
|
private array $configurationCache = []; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(Reader $reader, ManagerRegistry $managerRegistry) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->reader = $reader; |
|
36
|
|
|
$this->initRegistry($managerRegistry); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
abstract public function getConfiguration($class); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param object|string $class |
|
43
|
|
|
*/ |
|
44
|
|
|
public function isConfigured($class): bool |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
|
|
$this->getConfiguration($class); |
|
48
|
|
|
} catch (InvalidArgumentException $e) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param object|string $class |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \ReflectionException |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getClassAnnotationConfiguration($class, string $annotationClass): ?object |
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $class || (\is_string($class) && !class_exists($class))) { |
|
63
|
|
|
throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$className = \is_object($class) ? \get_class($class) : $class; |
|
67
|
|
|
if (\array_key_exists($className, $this->configurationCache)) { |
|
68
|
|
|
return $this->configurationCache[$className]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$annotation = $this->findAnnotationConfiguration($class, $annotationClass); |
|
72
|
|
|
|
|
73
|
|
|
$this->configurationCache[$className] = $annotation; |
|
74
|
|
|
|
|
75
|
|
|
return $annotation; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string|object $class |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \ReflectionException |
|
82
|
|
|
*/ |
|
83
|
|
|
private function findAnnotationConfiguration($class, string $annotationClass): ?object |
|
84
|
|
|
{ |
|
85
|
|
|
$reflection = new \ReflectionClass($class); |
|
86
|
|
|
$annotation = $this->reader->getClassAnnotation($reflection, $annotationClass); |
|
87
|
|
|
if (!$annotation) { |
|
88
|
|
|
$annotation = $this->getConfigurationFromParentClasses($reflection, $annotationClass); |
|
|
|
|
|
|
89
|
|
|
if (!$annotation) { |
|
|
|
|
|
|
90
|
|
|
$annotation = $this->getConfigurationFromTraits($reflection, $annotationClass); |
|
91
|
|
|
if (!$annotation) { |
|
92
|
|
|
throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $annotation; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function getConfigurationFromParentClasses(\ReflectionClass $reflection, string $annotationClass): ?object |
|
101
|
|
|
{ |
|
102
|
|
|
$annotation = null; |
|
103
|
|
|
|
|
104
|
|
|
$parentReflection = $reflection->getParentClass(); |
|
105
|
|
|
while ( |
|
106
|
|
|
$parentReflection && |
|
107
|
|
|
!$annotation = $this->reader->getClassAnnotation($parentReflection, $annotationClass) |
|
108
|
|
|
) { |
|
109
|
|
|
$parentReflection = $parentReflection->getParentClass(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $annotation; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function getConfigurationFromTraits(\ReflectionClass $reflection, string $annotationClass): ?object |
|
116
|
|
|
{ |
|
117
|
|
|
$annotation = null; |
|
118
|
|
|
$traits = $reflection->getTraits(); |
|
119
|
|
|
foreach ($traits as $trait) { |
|
120
|
|
|
$annotation = $this->reader->getClassAnnotation($trait, $annotationClass); |
|
121
|
|
|
if ($annotation) { |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $annotation; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.