1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\Helper; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
17
|
|
|
use Silverback\ApiComponentBundle\Annotation\Timestamped; |
18
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException; |
19
|
|
|
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait; |
20
|
|
|
|
21
|
|
|
abstract class AbstractHelper |
22
|
|
|
{ |
23
|
|
|
use ClassMetadataTrait; |
24
|
|
|
|
25
|
|
|
protected Reader $reader; |
26
|
|
|
|
27
|
|
|
abstract public function getConfiguration($class); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param object|string $class |
31
|
|
|
*/ |
32
|
9 |
|
public function isConfigured($class): bool |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
9 |
|
$this->getConfiguration($class); |
36
|
9 |
|
} catch (InvalidArgumentException $e) { |
37
|
9 |
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @required |
45
|
|
|
*/ |
46
|
9 |
|
protected function initReader(Reader $reader): void |
47
|
|
|
{ |
48
|
9 |
|
$this->reader = $reader; |
49
|
9 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param object|string $class |
53
|
|
|
* |
54
|
|
|
* @throws \ReflectionException |
55
|
|
|
*/ |
56
|
9 |
|
protected function getAnnotationConfiguration($class, string $annotationClass) |
57
|
|
|
{ |
58
|
9 |
|
if (null === $class || (\is_string($class) && !class_exists($class))) { |
59
|
|
|
throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object', __CLASS__)); |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
$originalReflection = $reflection = new \ReflectionClass($class); |
63
|
|
|
/** @var $annotationClass|null $annotation */ |
64
|
|
|
while ( |
65
|
9 |
|
!($annotation = $this->reader->getClassAnnotation($reflection, $annotationClass)) && |
66
|
9 |
|
($reflection = $reflection->getParentClass()) |
67
|
|
|
) { |
68
|
9 |
|
continue; |
69
|
|
|
} |
70
|
9 |
|
if (!$annotation && Timestamped::class === $annotationClass) { |
71
|
9 |
|
$traits = $originalReflection->getTraits(); |
72
|
9 |
|
foreach ($traits as $trait) { |
73
|
9 |
|
$annotation = $this->reader->getClassAnnotation($trait, $annotationClass); |
74
|
9 |
|
if ($annotation) { |
75
|
9 |
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
if (!$annotation) { |
81
|
9 |
|
throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass)); |
82
|
|
|
} |
83
|
|
|
|
84
|
9 |
|
return $annotation; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|