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
|
11 |
|
public function __construct(Reader $reader, ManagerRegistry $managerRegistry) |
34
|
|
|
{ |
35
|
11 |
|
$this->reader = $reader; |
36
|
11 |
|
$this->initRegistry($managerRegistry); |
37
|
11 |
|
} |
38
|
|
|
|
39
|
|
|
abstract public function getConfiguration($class); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param object|string $class |
43
|
|
|
*/ |
44
|
11 |
|
public function isConfigured($class): bool |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
11 |
|
$this->getConfiguration($class); |
48
|
11 |
|
} catch (InvalidArgumentException $e) { |
49
|
11 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
11 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param object|string $class |
57
|
|
|
* |
58
|
|
|
* @throws \ReflectionException |
59
|
|
|
*/ |
60
|
11 |
|
protected function getClassAnnotationConfiguration($class, string $annotationClass): ?object |
61
|
|
|
{ |
62
|
11 |
|
if (null === $class || (!\is_object($class) && !\is_string($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
|
11 |
|
$className = \is_object($class) ? \get_class($class) : $class; |
67
|
11 |
|
if (\array_key_exists($className, $this->configurationCache)) { |
68
|
11 |
|
return $this->configurationCache[$className]; |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
$annotation = $this->findAnnotationConfiguration($class, $annotationClass); |
72
|
|
|
|
73
|
11 |
|
$this->configurationCache[$className] = $annotation; |
74
|
|
|
|
75
|
11 |
|
return $annotation; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|object $class |
80
|
|
|
* |
81
|
|
|
* @throws \ReflectionException |
82
|
|
|
*/ |
83
|
11 |
|
private function findAnnotationConfiguration($class, string $annotationClass): ?object |
84
|
|
|
{ |
85
|
11 |
|
$reflection = new \ReflectionClass($class); |
86
|
11 |
|
$annotation = $this->reader->getClassAnnotation($reflection, $annotationClass); |
87
|
11 |
|
if (!$annotation) { |
88
|
11 |
|
$annotation = $this->getConfigurationFromParentClasses($reflection, $annotationClass); |
|
|
|
|
89
|
11 |
|
if (!$annotation) { |
|
|
|
|
90
|
11 |
|
$annotation = $this->getConfigurationFromTraits($reflection, $annotationClass); |
91
|
11 |
|
if (!$annotation) { |
92
|
11 |
|
throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
11 |
|
return $annotation; |
98
|
|
|
} |
99
|
|
|
|
100
|
11 |
|
private function getConfigurationFromParentClasses(\ReflectionClass $reflection, string $annotationClass): ?object |
101
|
|
|
{ |
102
|
11 |
|
$annotation = null; |
103
|
|
|
|
104
|
11 |
|
$parentReflection = $reflection->getParentClass(); |
105
|
|
|
while ( |
106
|
11 |
|
$parentReflection && |
107
|
11 |
|
!$annotation = $this->reader->getClassAnnotation($parentReflection, $annotationClass) |
108
|
|
|
) { |
109
|
11 |
|
$parentReflection = $parentReflection->getParentClass(); |
110
|
|
|
} |
111
|
|
|
|
112
|
11 |
|
return $annotation; |
113
|
|
|
} |
114
|
|
|
|
115
|
11 |
|
private function getConfigurationFromTraits(\ReflectionClass $reflection, string $annotationClass): ?object |
116
|
|
|
{ |
117
|
11 |
|
$annotation = null; |
118
|
11 |
|
$traits = $reflection->getTraits(); |
119
|
11 |
|
foreach ($traits as $trait) { |
120
|
11 |
|
$annotation = $this->reader->getClassAnnotation($trait, $annotationClass); |
121
|
11 |
|
if ($annotation) { |
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
11 |
|
return $annotation; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|