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\Annotation\Timestamped; |
19
|
|
|
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException; |
20
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Vincent Chalamon <[email protected]> |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractAnnotationReader |
27
|
|
|
{ |
28
|
|
|
use ClassMetadataTrait; |
29
|
|
|
|
30
|
|
|
protected Reader $reader; |
31
|
|
|
|
32
|
9 |
|
public function __construct(Reader $reader, ManagerRegistry $managerRegistry) |
33
|
|
|
{ |
34
|
9 |
|
$this->reader = $reader; |
35
|
9 |
|
$this->initRegistry($managerRegistry); |
36
|
9 |
|
} |
37
|
|
|
|
38
|
|
|
abstract public function getConfiguration($class); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param object|string $class |
42
|
|
|
*/ |
43
|
9 |
|
public function isConfigured($class): bool |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
9 |
|
$this->getConfiguration($class); |
47
|
9 |
|
} catch (InvalidArgumentException $e) { |
48
|
9 |
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
9 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param object|string $class |
56
|
|
|
* |
57
|
|
|
* @throws \ReflectionException |
58
|
|
|
*/ |
59
|
9 |
|
protected function getClassAnnotationConfiguration($class, string $annotationClass) |
60
|
|
|
{ |
61
|
9 |
|
if (null === $class || (\is_string($class) && !class_exists($class))) { |
62
|
|
|
throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__)); |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
$originalReflection = $reflection = new \ReflectionClass($class); |
66
|
|
|
/** @var $annotationClass|null $annotation */ |
67
|
|
|
while ( |
68
|
9 |
|
!($annotation = $this->reader->getClassAnnotation($reflection, $annotationClass)) && |
69
|
9 |
|
($reflection = $reflection->getParentClass()) |
70
|
|
|
) { |
71
|
9 |
|
continue; |
72
|
|
|
} |
73
|
9 |
|
if (!$annotation && Timestamped::class === $annotationClass) { |
74
|
9 |
|
$traits = $originalReflection->getTraits(); |
75
|
9 |
|
foreach ($traits as $trait) { |
76
|
9 |
|
$annotation = $this->reader->getClassAnnotation($trait, $annotationClass); |
77
|
9 |
|
if ($annotation) { |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
if (!$annotation) { |
84
|
9 |
|
throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass)); |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
return $annotation; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|