|
5 | 5 | namespace Hateoas\Configuration\Metadata\Driver;
|
6 | 6 |
|
7 | 7 | use Doctrine\Common\Annotations\Reader as AnnotationsReader;
|
8 |
| -use Hateoas\Configuration\Annotation; |
9 |
| -use Hateoas\Configuration\Embedded; |
10 |
| -use Hateoas\Configuration\Exclusion; |
11 |
| -use Hateoas\Configuration\Metadata\ClassMetadata; |
12 | 8 | use Hateoas\Configuration\Provider\RelationProviderInterface;
|
13 |
| -use Hateoas\Configuration\Relation; |
14 |
| -use Hateoas\Configuration\RelationProvider; |
15 |
| -use Hateoas\Configuration\Route; |
16 | 9 | use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
|
17 |
| -use JMS\Serializer\Expression\Expression; |
18 | 10 | use JMS\Serializer\Type\ParserInterface;
|
19 |
| -use Metadata\ClassMetadata as JMSClassMetadata; |
20 |
| -use Metadata\Driver\DriverInterface; |
21 | 11 |
|
22 |
| -class AnnotationDriver implements DriverInterface |
| 12 | +class AnnotationDriver extends AnnotationOrAttributeDriver |
23 | 13 | {
|
24 |
| - use CheckExpressionTrait; |
25 |
| - |
26 | 14 | /**
|
27 | 15 | * @var AnnotationsReader
|
28 | 16 | */
|
29 | 17 | private $reader;
|
30 | 18 |
|
31 |
| - /** |
32 |
| - * @var RelationProviderInterface |
33 |
| - */ |
34 |
| - private $relationProvider; |
35 |
| - |
36 |
| - /** |
37 |
| - * @var ParserInterface |
38 |
| - */ |
39 |
| - private $typeParser; |
40 |
| - |
41 | 19 | public function __construct(
|
42 | 20 | AnnotationsReader $reader,
|
43 | 21 | CompilableExpressionEvaluatorInterface $expressionLanguage,
|
44 | 22 | RelationProviderInterface $relationProvider,
|
45 | 23 | ParserInterface $typeParser
|
46 | 24 | ) {
|
47 |
| - $this->reader = $reader; |
48 |
| - $this->relationProvider = $relationProvider; |
49 |
| - $this->expressionLanguage = $expressionLanguage; |
50 |
| - $this->typeParser = $typeParser; |
51 |
| - } |
52 |
| - |
53 |
| - public function loadMetadataForClass(\ReflectionClass $class): ?JMSClassMetadata |
54 |
| - { |
55 |
| - $annotations = $this->reader->getClassAnnotations($class); |
56 |
| - |
57 |
| - if (0 === count($annotations)) { |
58 |
| - return null; |
59 |
| - } |
60 |
| - |
61 |
| - $classMetadata = new ClassMetadata($class->getName()); |
62 |
| - $classMetadata->fileResources[] = $class->getFilename(); |
63 |
| - |
64 |
| - foreach ($annotations as $annotation) { |
65 |
| - if ($annotation instanceof Annotation\Relation) { |
66 |
| - $classMetadata->addRelation(new Relation( |
67 |
| - $annotation->name, |
68 |
| - $this->createHref($annotation->href), |
69 |
| - $this->createEmbedded($annotation->embedded), |
70 |
| - $this->checkExpressionArray($annotation->attributes) ?: [], |
71 |
| - $this->createExclusion($annotation->exclusion) |
72 |
| - )); |
73 |
| - } elseif ($annotation instanceof Annotation\RelationProvider) { |
74 |
| - $relations = $this->relationProvider->getRelations(new RelationProvider($annotation->name), $class->getName()); |
75 |
| - foreach ($relations as $relation) { |
76 |
| - $classMetadata->addRelation($relation); |
77 |
| - } |
78 |
| - } |
79 |
| - } |
80 |
| - |
81 |
| - if (0 === count($classMetadata->getRelations())) { |
82 |
| - return null; |
83 |
| - } |
84 |
| - |
85 |
| - return $classMetadata; |
86 |
| - } |
87 |
| - |
88 |
| - private function parseExclusion(Annotation\Exclusion $exclusion): Exclusion |
89 |
| - { |
90 |
| - return new Exclusion( |
91 |
| - $exclusion->groups, |
92 |
| - null !== $exclusion->sinceVersion ? (string) $exclusion->sinceVersion : null, |
93 |
| - null !== $exclusion->untilVersion ? (string) $exclusion->untilVersion : null, |
94 |
| - null !== $exclusion->maxDepth ? (int) $exclusion->maxDepth : null, |
95 |
| - $this->checkExpression($exclusion->excludeIf) |
96 |
| - ); |
97 |
| - } |
98 |
| - |
99 |
| - /** |
100 |
| - * @param mixed $href |
101 |
| - * |
102 |
| - * @return Expression|mixed |
103 |
| - */ |
104 |
| - private function createHref($href) |
105 |
| - { |
106 |
| - if ($href instanceof Annotation\Route) { |
107 |
| - return new Route( |
108 |
| - $this->checkExpression($href->name), |
109 |
| - is_array($href->parameters) ? $this->checkExpressionArray($href->parameters) : $this->checkExpression($href->parameters), |
110 |
| - $this->checkExpression($href->absolute), |
111 |
| - $href->generator |
112 |
| - ); |
113 |
| - } |
| 25 | + parent::__construct($expressionLanguage, $relationProvider, $typeParser); |
114 | 26 |
|
115 |
| - return $this->checkExpression($href); |
| 27 | + $this->reader = $reader; |
116 | 28 | }
|
117 | 29 |
|
118 | 30 | /**
|
119 |
| - * @param Annotation\Embedded|mixed $embedded |
120 |
| - * |
121 |
| - * @return Expression|mixed |
| 31 | + * {@inheritdoc} |
122 | 32 | */
|
123 |
| - private function createEmbedded($embedded) |
124 |
| - { |
125 |
| - if ($embedded instanceof Annotation\Embedded) { |
126 |
| - $embeddedExclusion = $embedded->exclusion; |
127 |
| - |
128 |
| - if (null !== $embeddedExclusion) { |
129 |
| - $embeddedExclusion = $this->parseExclusion($embeddedExclusion); |
130 |
| - } |
131 |
| - |
132 |
| - return new Embedded( |
133 |
| - $this->checkExpression($embedded->content), |
134 |
| - $this->checkExpression($embedded->xmlElementName), |
135 |
| - $embeddedExclusion, |
136 |
| - null !== $embedded->type ? $this->typeParser->parse($embedded->type) : null |
137 |
| - ); |
138 |
| - } |
139 |
| - |
140 |
| - return $this->checkExpression($embedded); |
141 |
| - } |
142 |
| - |
143 |
| - private function createExclusion(?Annotation\Exclusion $exclusion = null): ?Exclusion |
| 33 | + protected function getClassAnnotations(\ReflectionClass $class): array |
144 | 34 | {
|
145 |
| - if (null !== $exclusion) { |
146 |
| - $exclusion = $this->parseExclusion($exclusion); |
147 |
| - } |
148 |
| - |
149 |
| - return $exclusion; |
| 35 | + return $this->reader->getClassAnnotations($class); |
150 | 36 | }
|
151 | 37 | }
|
0 commit comments