|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Configuration\Metadata\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Embedded; |
|
6
|
|
|
use Hateoas\Configuration\Exclusion; |
|
7
|
|
|
use Hateoas\Configuration\Metadata\ClassMetadata; |
|
8
|
|
|
use Hateoas\Configuration\Relation; |
|
9
|
|
|
use Hateoas\Configuration\RelationProvider; |
|
10
|
|
|
use Hateoas\Configuration\Route; |
|
11
|
|
|
use Metadata\Driver\AbstractFileDriver; |
|
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Adrien Brault <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class YamlDriver extends AbstractFileDriver |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
|
23
|
|
|
{ |
|
24
|
|
|
$config = Yaml::parse(file_get_contents($file)); |
|
25
|
|
|
|
|
26
|
|
|
if (!isset($config[$name = $class->getName()])) { |
|
27
|
|
|
throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $name, $file)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$config = $config[$name]; |
|
31
|
|
|
$classMetadata = new ClassMetadata($name); |
|
32
|
|
|
$classMetadata->fileResources[] = $file; |
|
33
|
|
|
$classMetadata->fileResources[] = $class->getFileName(); |
|
34
|
|
|
|
|
35
|
|
|
if (isset($config['relations'])) { |
|
36
|
|
|
foreach ($config['relations'] as $relation) { |
|
37
|
|
|
$classMetadata->addRelation(new Relation( |
|
38
|
|
|
$relation['rel'], |
|
39
|
|
|
$this->createHref($relation), |
|
40
|
|
|
$this->createEmbedded($relation), |
|
41
|
|
|
isset($relation['attributes']) ? $relation['attributes'] : array(), |
|
42
|
|
|
$this->createExclusion($relation) |
|
43
|
|
|
)); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (isset($config['relation_providers'])) { |
|
48
|
|
|
foreach ($config['relation_providers'] as $relationProvider) { |
|
49
|
|
|
$classMetadata->addRelationProvider(new RelationProvider($relationProvider)); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $classMetadata; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getExtension() |
|
60
|
|
|
{ |
|
61
|
|
|
return 'yml'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function parseExclusion(array $exclusion) |
|
65
|
|
|
{ |
|
66
|
|
|
return new Exclusion( |
|
67
|
|
|
isset($exclusion['groups']) ? $exclusion['groups'] : null, |
|
68
|
|
|
isset($exclusion['since_version']) ? $exclusion['since_version'] : null, |
|
69
|
|
|
isset($exclusion['until_version']) ? $exclusion['until_version'] : null, |
|
70
|
|
|
isset($exclusion['max_depth']) ? $exclusion['max_depth'] : null, |
|
71
|
|
|
isset($exclusion['exclude_if']) ? $exclusion['exclude_if'] : null |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function createHref($relation) |
|
76
|
|
|
{ |
|
77
|
|
|
$href = null; |
|
78
|
|
|
if (isset($relation['href']) && is_array($href = $relation['href']) && isset($href['route'])) { |
|
79
|
|
|
$href = new Route( |
|
80
|
|
|
$href['route'], |
|
81
|
|
|
isset($href['parameters']) ? $href['parameters'] : array(), |
|
82
|
|
|
isset($href['absolute']) ? $href['absolute'] : false, |
|
83
|
|
|
isset($href['generator']) ? $href['generator'] : null |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $href; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function createEmbedded($relation) |
|
91
|
|
|
{ |
|
92
|
|
|
$embedded = null; |
|
93
|
|
|
if (isset($relation['embedded'])) { |
|
94
|
|
|
$embedded = $relation['embedded']; |
|
95
|
|
|
|
|
96
|
|
|
if (is_array($embedded)) { |
|
97
|
|
|
$embeddedExclusion = null; |
|
98
|
|
|
if (isset($embedded['exclusion'])) { |
|
99
|
|
|
$embeddedExclusion = $this->parseExclusion($embedded['exclusion']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$xmlElementName = isset($embedded['xmlElementName']) ? $embedded['xmlElementName'] : null; |
|
103
|
|
|
$embedded = new Embedded($embedded['content'], $xmlElementName, $embeddedExclusion); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $embedded; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function createExclusion($relation) |
|
111
|
|
|
{ |
|
112
|
|
|
$exclusion = null; |
|
113
|
|
|
if (isset($relation['exclusion'])) { |
|
114
|
|
|
$exclusion = $this->parseExclusion($relation['exclusion']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $exclusion; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|