1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Metadata\Driver; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; |
7
|
|
|
use Metadata\Driver\AbstractFileDriver; |
8
|
|
|
use Metadata\Driver\DriverInterface; |
9
|
|
|
use Metadata\Driver\FileLocatorInterface; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class YamlDriver extends AbstractFileDriver |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DriverInterface |
16
|
|
|
*/ |
17
|
|
|
private $doctrineDriver; |
18
|
|
|
|
19
|
38 |
|
public function __construct(FileLocatorInterface $locator, DriverInterface $doctrineDriver) |
20
|
|
|
{ |
21
|
38 |
|
parent::__construct($locator); |
22
|
38 |
|
$this->doctrineDriver = $doctrineDriver; |
23
|
38 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
16 |
|
protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
29
|
|
|
{ |
30
|
|
|
/** @var ClassMetadata $ddrRestClassMetadata */ |
31
|
16 |
|
$classMetadata = $this->doctrineDriver->loadMetadataForClass($class); |
32
|
16 |
|
if (null === $classMetadata) { |
33
|
|
|
$classMetadata = new ClassMetadata($class->getName()); |
34
|
|
|
} |
35
|
|
|
|
36
|
16 |
|
$config = Yaml::parse(file_get_contents($file)); |
37
|
16 |
|
$className = key($config); |
38
|
|
|
|
39
|
16 |
|
if ($className !== $class->name) { |
40
|
|
|
throw new \RuntimeException( |
41
|
|
|
sprintf('Class definition mismatch for "%s" in "%s": %s', $class->getName(), $file, key($config)) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
16 |
|
$config = $config[$className]; |
46
|
16 |
|
if (!is_array($config)) { |
47
|
|
|
$config = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
16 |
|
if (array_key_exists('rootResource', $config) && true === $config['rootResource']) { |
51
|
16 |
|
$classMetadata->setRestResource(true); |
52
|
|
|
} |
53
|
|
|
|
54
|
16 |
|
if (array_key_exists('service', $config)) { |
55
|
4 |
|
$classMetadata->setService($config['service']); |
56
|
|
|
} |
57
|
|
|
|
58
|
16 |
|
$fieldConfigs = []; |
59
|
16 |
|
if (array_key_exists('fields', $config)) { |
60
|
16 |
|
$fieldConfigs = $config['fields']; |
61
|
|
|
} |
62
|
|
|
|
63
|
16 |
|
foreach ($class->getProperties() as $reflectionProperty) { |
64
|
|
|
|
65
|
16 |
|
$propertyName = $reflectionProperty->getName(); |
66
|
16 |
|
$propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $propertyName); |
|
|
|
|
67
|
|
|
|
68
|
16 |
|
if (array_key_exists($propertyName, $fieldConfigs)) { |
69
|
16 |
|
$fieldConfig = $fieldConfigs[$propertyName]; |
70
|
16 |
|
$this->parseFieldConfig($propertyName, $fieldConfig, $propertyMetadata); |
71
|
16 |
|
unset($fieldConfigs[$propertyName]); |
72
|
|
|
} |
73
|
|
|
|
74
|
16 |
|
$classMetadata->addPropertyMetadata($propertyMetadata); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* Parse unbacked field definitions */ |
78
|
16 |
|
foreach ($fieldConfigs as $name => $fieldConfig) { |
79
|
4 |
|
$propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $name); |
|
|
|
|
80
|
4 |
|
$this->parseFieldConfig($propertyName, $fieldConfig, $propertyMetadata); |
|
|
|
|
81
|
4 |
|
$classMetadata->addPropertyMetadata($propertyMetadata); |
82
|
|
|
} |
83
|
|
|
|
84
|
16 |
|
return $classMetadata; |
85
|
|
|
} |
86
|
|
|
|
87
|
16 |
|
protected function parseFieldConfig(string $name, array $fieldConfig, PropertyMetadata $propertyMetadata): void |
88
|
|
|
{ |
89
|
16 |
|
if (null !== $value = $this->getBool('puttable', $fieldConfig)) { |
90
|
|
|
$propertyMetadata->setPuttable($value); |
91
|
|
|
} |
92
|
|
|
|
93
|
16 |
|
if (null !== $value = $this->getBool('excluded', $fieldConfig)) { |
94
|
4 |
|
$propertyMetadata->setExcluded($value); |
95
|
|
|
} |
96
|
|
|
|
97
|
16 |
|
if (null !== $value = $this->getBool('postable', $fieldConfig)) { |
98
|
|
|
$propertyMetadata->setPostable($value); |
99
|
|
|
} |
100
|
|
|
|
101
|
16 |
|
if (array_key_exists('includable', $fieldConfig)) { |
102
|
14 |
|
$value = $fieldConfig['includable']; |
103
|
14 |
|
if (is_array($value)) { |
104
|
14 |
|
$propertyMetadata->setIncludable(true); |
105
|
14 |
|
$propertyMetadata->setIncludablePaths($value); |
106
|
|
|
} elseif (true === $value) { |
107
|
|
|
$propertyMetadata->setIncludable(true); |
108
|
|
|
$propertyMetadata->setIncludablePaths([$name]); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
16 |
|
} |
112
|
|
|
|
113
|
16 |
|
private function getBool(string $key, array $haystack, bool $required = false) |
114
|
|
|
{ |
115
|
16 |
|
if (!array_key_exists($key, $haystack)) { |
116
|
16 |
|
if ($required) { |
117
|
|
|
throw new \RuntimeException(sprintf('Value %s is required', $key)); |
118
|
|
|
} |
119
|
|
|
|
120
|
16 |
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
4 |
|
$value = $haystack[$key]; |
124
|
4 |
|
if (!is_bool($value)) { |
125
|
|
|
throw new \RuntimeException(sprintf('Value %s must be of type bool', $key)); |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
return $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
38 |
|
protected function getExtension() |
135
|
|
|
{ |
136
|
38 |
|
return 'rest.yml'; |
137
|
|
|
} |
138
|
|
|
|
139
|
16 |
|
protected function getOrCreatePropertymetadata(ClassMetadata $classMetadata, $propertyName): PropertyMetadata |
140
|
|
|
{ |
141
|
16 |
|
$propertyMetadata = $classMetadata->getPropertyMetadata($propertyName); |
142
|
16 |
|
if (null === $propertyMetadata) { |
143
|
4 |
|
$propertyMetadata = new PropertyMetadata($classMetadata->name, $propertyName); |
144
|
|
|
|
145
|
4 |
|
return $propertyMetadata; |
146
|
|
|
} |
147
|
|
|
|
148
|
14 |
|
return $propertyMetadata; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.