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
|
9 |
|
public function __construct(Reader $reader, ManagerRegistry $managerRegistry) |
32
|
|
|
{ |
33
|
9 |
|
$this->reader = $reader; |
34
|
9 |
|
$this->initRegistry($managerRegistry); |
35
|
9 |
|
} |
36
|
|
|
|
37
|
|
|
abstract public function getConfiguration($class); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param object|string $class |
41
|
|
|
*/ |
42
|
9 |
|
public function isConfigured($class): bool |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
9 |
|
$this->getConfiguration($class); |
46
|
9 |
|
} catch (InvalidArgumentException $e) { |
47
|
9 |
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param object|string $class |
55
|
|
|
* |
56
|
|
|
* @throws \ReflectionException |
57
|
|
|
*/ |
58
|
9 |
|
protected function getClassAnnotationConfiguration($class, string $annotationClass) |
59
|
|
|
{ |
60
|
9 |
|
if (null === $class || (\is_string($class) && !class_exists($class))) { |
61
|
|
|
throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__)); |
62
|
|
|
} |
63
|
|
|
|
64
|
9 |
|
$reflection = new \ReflectionClass($class); |
65
|
9 |
|
$annotation = $this->reader->getClassAnnotation($reflection, $annotationClass); |
66
|
9 |
|
if (!$annotation) { |
67
|
9 |
|
$annotation = $this->getConfigurationFromParentClasses($reflection, $annotationClass); |
|
|
|
|
68
|
9 |
|
if (!$annotation) { |
|
|
|
|
69
|
9 |
|
$annotation = $this->getConfigurationFromTraits($reflection, $annotationClass); |
70
|
9 |
|
if (!$annotation) { |
71
|
9 |
|
throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
9 |
|
return $annotation; |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
private function getConfigurationFromParentClasses(\ReflectionClass $reflection, string $annotationClass) |
80
|
|
|
{ |
81
|
9 |
|
$annotation = null; |
82
|
9 |
|
$parentReflection = $reflection->getParentClass(); |
83
|
9 |
|
while ($parentReflection && !($annotation = $this->reader->getClassAnnotation($parentReflection, $annotationClass))) { |
84
|
9 |
|
$parentReflection = $parentReflection->getParentClass(); |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
return $annotation; |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
private function getConfigurationFromTraits(\ReflectionClass $reflection, string $annotationClass) |
91
|
|
|
{ |
92
|
9 |
|
$annotation = null; |
93
|
9 |
|
$traits = $reflection->getTraits(); |
94
|
9 |
|
foreach ($traits as $trait) { |
95
|
9 |
|
$annotation = $this->reader->getClassAnnotation($trait, $annotationClass); |
96
|
9 |
|
if ($annotation) { |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
9 |
|
return $annotation; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.