|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Configuration\Metadata\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader as AnnotationsReader; |
|
6
|
|
|
use Hateoas\Configuration\Annotation; |
|
7
|
|
|
use Hateoas\Configuration\Embedded; |
|
8
|
|
|
use Hateoas\Configuration\Exclusion; |
|
9
|
|
|
use Hateoas\Configuration\Metadata\ClassMetadata; |
|
10
|
|
|
use Hateoas\Configuration\Relation; |
|
11
|
|
|
use Hateoas\Configuration\RelationProvider; |
|
12
|
|
|
use Hateoas\Configuration\Route; |
|
13
|
|
|
use Metadata\Driver\DriverInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Adrien Brault <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class AnnotationDriver implements DriverInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var AnnotationsReader |
|
22
|
|
|
*/ |
|
23
|
|
|
private $reader; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param AnnotationsReader $reader |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(AnnotationsReader $reader) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->reader = $reader; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function loadMetadataForClass(\ReflectionClass $class) |
|
37
|
|
|
{ |
|
38
|
|
|
$annotations = $this->reader->getClassAnnotations($class); |
|
39
|
|
|
|
|
40
|
|
|
if (0 === count($annotations)) { |
|
41
|
|
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$classMetadata = new ClassMetadata($class->getName()); |
|
45
|
|
|
$classMetadata->fileResources[] = $class->getFilename(); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($annotations as $annotation) { |
|
48
|
|
|
if ($annotation instanceof Annotation\Relation) { |
|
49
|
|
|
$classMetadata->addRelation(new Relation( |
|
50
|
|
|
$annotation->name, |
|
51
|
|
|
$this->createHref($annotation->href), |
|
52
|
|
|
$this->createEmbedded($annotation->embedded), |
|
53
|
|
|
$annotation->attributes ?: array(), |
|
54
|
|
|
$this->createExclusion($annotation->exclusion) |
|
55
|
|
|
)); |
|
56
|
|
|
} elseif ($annotation instanceof Annotation\RelationProvider) { |
|
57
|
|
|
$classMetadata->addRelationProvider(new RelationProvider($annotation->name)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (0 === count($classMetadata->getRelations()) && 0 === count($classMetadata->getRelationProviders())) { |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $classMetadata; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function parseExclusion(Annotation\Exclusion $exclusion) |
|
69
|
|
|
{ |
|
70
|
|
|
return new Exclusion( |
|
71
|
|
|
$exclusion->groups, |
|
72
|
|
|
$exclusion->sinceVersion, |
|
73
|
|
|
$exclusion->untilVersion, |
|
74
|
|
|
$exclusion->maxDepth, |
|
75
|
|
|
$exclusion->excludeIf |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function createHref($href) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($href instanceof Annotation\Route) { |
|
82
|
|
|
$href = new Route($href->name, $href->parameters, $href->absolute, $href->generator); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $href; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function createEmbedded($embedded) |
|
89
|
|
|
{ |
|
90
|
|
|
if ($embedded instanceof Annotation\Embedded) { |
|
91
|
|
|
$embeddedExclusion = $embedded->exclusion; |
|
92
|
|
|
|
|
93
|
|
|
if (null !== $embeddedExclusion) { |
|
94
|
|
|
$embeddedExclusion = $this->parseExclusion($embeddedExclusion); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$embedded = new Embedded($embedded->content, $embedded->xmlElementName, $embeddedExclusion); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $embedded; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function createExclusion($exclusion) |
|
104
|
|
|
{ |
|
105
|
|
|
if (null !== $exclusion) { |
|
106
|
|
|
$exclusion = $this->parseExclusion($exclusion); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $exclusion; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|