1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Novaway\Component\OpenGraph\Metadata\Driver; |
4
|
|
|
|
5
|
|
|
use Novaway\Component\OpenGraph\Annotation\NamespaceNode; |
6
|
|
|
use Novaway\Component\OpenGraph\Annotation\Node; |
7
|
|
|
use Novaway\Component\OpenGraph\Metadata\ClassMetadata; |
8
|
|
|
use Metadata\Driver\AbstractFileDriver; |
9
|
|
|
use Novaway\Component\OpenGraph\Metadata\MethodMetadata; |
10
|
|
|
use Novaway\Component\OpenGraph\Metadata\PropertyMetadata; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
|
13
|
|
|
class YamlDriver extends AbstractFileDriver |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
19
|
|
|
{ |
20
|
|
|
$name = $class->name; |
21
|
|
|
|
22
|
|
|
$config = Yaml::parse(file_get_contents($file)); |
23
|
|
|
if (!isset($config[$name])) { |
24
|
|
|
throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->name, $file)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$config = $config[$name]; |
28
|
|
|
|
29
|
|
|
$classMetadata = new ClassMetadata($name); |
30
|
|
|
|
31
|
|
|
$this->parseNamespaces($classMetadata, $config); |
32
|
|
|
$this->parseNodes($class, $classMetadata, $config); |
33
|
|
|
|
34
|
|
|
return $classMetadata; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
protected function getExtension() |
41
|
|
|
{ |
42
|
|
|
return 'yml'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parse namespaces configuration |
47
|
|
|
* |
48
|
|
|
* @param ClassMetadata $metadata |
49
|
|
|
* @param mixed $config |
50
|
|
|
*/ |
51
|
|
|
protected function parseNamespaces(ClassMetadata $metadata, $config) |
52
|
|
|
{ |
53
|
|
|
if (!isset($config['namespaces'])) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!is_array($config)) { |
58
|
|
|
throw new \RuntimeException(sprintf('Invalid YAML configuration for "%s" : "namespaces" property need to be an array.', $metadata->name)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($config['namespaces'] as $prefix => $uri) { |
62
|
|
|
$metadata->addGraphNamespace(new NamespaceNode([ |
63
|
|
|
'prefix' => $prefix, |
64
|
|
|
'uri' => $uri, |
65
|
|
|
])); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Parse OpenGraph node properties |
71
|
|
|
* |
72
|
|
|
* @param \ReflectionClass $class |
73
|
|
|
* @param ClassMetadata $metadata |
74
|
|
|
* @param mixed $config |
75
|
|
|
*/ |
76
|
|
|
protected function parseNodes(\ReflectionClass $class, ClassMetadata $metadata, $config) |
77
|
|
|
{ |
78
|
|
|
if (!isset($config['nodes'])) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!is_array($config)) { |
83
|
|
|
throw new \RuntimeException(sprintf('Invalid YAML configuration for "%s" : "properties" property need to be an array.', $metadata->name)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($config['nodes'] as $property => $nodeProperties) { |
87
|
|
|
foreach ($nodeProperties as $nodeProperty) { |
88
|
|
|
if (!isset($nodeProperty['namespace']) || !isset($nodeProperty['tag'])) { |
89
|
|
|
throw new \RuntimeException(sprintf('Invalid YAML configuration for "%s" : "namespace" and "tag" are required.', $metadata->name)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$node = new Node([ |
93
|
|
|
'namespace' => $nodeProperty['namespace'], |
94
|
|
|
'tag' => $nodeProperty['tag'], |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
if ($class->hasProperty($property)) { |
98
|
|
|
$metadata->addGraphMetadata($node, new PropertyMetadata($class->name, $property)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($class->hasMethod($property)) { |
102
|
|
|
$metadata->addGraphMetadata($node, new MethodMetadata($class->name, $property)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|