1 | <?php |
||
15 | class YamlDriver extends AbstractFileDriver |
||
16 | { |
||
17 | /** |
||
18 | * @var DriverInterface |
||
19 | */ |
||
20 | private $doctrineDriver; |
||
21 | |||
22 | 84 | public function __construct(FileLocatorInterface $locator, DriverInterface $doctrineDriver) |
|
23 | { |
||
24 | 84 | parent::__construct($locator); |
|
25 | 84 | $this->doctrineDriver = $doctrineDriver; |
|
26 | 84 | } |
|
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 48 | protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
|
32 | { |
||
33 | /** @var ClassMetadata $ddrRestClassMetadata */ |
||
34 | 48 | $classMetadata = $this->doctrineDriver->loadMetadataForClass($class); |
|
35 | 48 | if (null === $classMetadata) { |
|
36 | $classMetadata = new ClassMetadata($class->getName()); |
||
37 | } |
||
38 | |||
39 | 48 | $config = Yaml::parse(file_get_contents($file)); |
|
40 | 48 | $className = key($config); |
|
41 | |||
42 | 48 | if ($className !== $class->name) { |
|
43 | throw new \RuntimeException( |
||
44 | sprintf('Class definition mismatch for "%s" in "%s": %s', $class->getName(), $file, key($config)) |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | 48 | $config = $config[$className]; |
|
49 | 48 | if (!is_array($config)) { |
|
50 | $config = []; |
||
51 | } |
||
52 | |||
53 | 48 | if (array_key_exists('rootResource', $config) && true === $config['rootResource']) { |
|
54 | 48 | $classMetadata->setRestResource(true); |
|
55 | } |
||
56 | |||
57 | 48 | $classMetadata->service = $config['service'] ?? null; |
|
|
|||
58 | 48 | $classMetadata->idField = $config['idField'] ?? null; |
|
59 | |||
60 | 48 | $classMetadata->setMethods($this->parseMethods($config)); |
|
61 | |||
62 | 48 | $fieldConfigs = []; |
|
63 | 48 | if (array_key_exists('fields', $config)) { |
|
64 | 48 | $fieldConfigs = $config['fields']; |
|
65 | } |
||
66 | |||
67 | 48 | foreach ($class->getProperties() as $reflectionProperty) { |
|
68 | |||
69 | 48 | $propertyName = $reflectionProperty->getName(); |
|
70 | 48 | $propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $propertyName); |
|
71 | |||
72 | 48 | if (array_key_exists($propertyName, $fieldConfigs)) { |
|
73 | 48 | $fieldConfig = $fieldConfigs[$propertyName]; |
|
74 | 48 | $this->parseFieldConfig($propertyName, $fieldConfig, $propertyMetadata); |
|
75 | 48 | unset($fieldConfigs[$propertyName]); |
|
76 | } |
||
77 | |||
78 | 48 | $classMetadata->addPropertyMetadata($propertyMetadata); |
|
79 | } |
||
80 | |||
81 | /* Parse unbacked field definitions */ |
||
82 | 48 | foreach ($fieldConfigs as $name => $fieldConfig) { |
|
83 | 30 | $propertyMetadata = $this->getOrCreatePropertymetadata($classMetadata, $name); |
|
84 | 30 | $this->parseFieldConfig($name, $fieldConfig, $propertyMetadata); |
|
85 | 30 | $classMetadata->addPropertyMetadata($propertyMetadata); |
|
86 | } |
||
87 | |||
88 | 48 | return $classMetadata; |
|
89 | } |
||
90 | |||
91 | 48 | protected function parseFieldConfig(string $name, array $fieldConfig, PropertyMetadata $propertyMetadata): void |
|
92 | { |
||
93 | 48 | $propertyMetadata->setPostable(Postable::parse($fieldConfig['postable'] ?? null)); |
|
94 | 48 | $propertyMetadata->setPuttable(Puttable::parse($fieldConfig['puttable'] ?? null)); |
|
95 | |||
96 | 48 | if (null !== $value = $this->getBool('excluded', $fieldConfig)) { |
|
97 | 30 | $propertyMetadata->setExcluded($value); |
|
98 | } |
||
99 | |||
100 | 48 | if (null !== $value = $this->getBool('virtual', $fieldConfig)) { |
|
101 | 28 | $propertyMetadata->setVirtual($value); |
|
102 | } |
||
103 | |||
104 | 48 | if (null !== $subResourceConfig = $fieldConfig['subResource'] ?? null) { |
|
105 | 46 | $propertyMetadata->setSubResource(true); |
|
106 | 46 | $propertyMetadata->setMethods($this->parseMethods($subResourceConfig)); |
|
107 | } |
||
108 | |||
109 | 48 | if (array_key_exists('includable', $fieldConfig)) { |
|
110 | 46 | $value = $fieldConfig['includable']; |
|
111 | 46 | if (is_array($value)) { |
|
112 | 30 | $propertyMetadata->setIncludable(true); |
|
113 | 30 | $propertyMetadata->setIncludablePaths($value); |
|
114 | 46 | } elseif (true === $value) { |
|
115 | 46 | $propertyMetadata->setIncludable(true); |
|
116 | 46 | $propertyMetadata->setIncludablePaths([$name]); |
|
117 | } |
||
118 | } |
||
119 | 48 | } |
|
120 | |||
121 | 48 | private function getBool(string $key, array $haystack, bool $required = false) |
|
134 | |||
135 | 48 | private function getArrayValue(string $key, array $haystack, bool $required = false) |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 84 | protected function getExtension() |
|
155 | |||
156 | 48 | protected function getOrCreatePropertymetadata(ClassMetadata $classMetadata, $propertyName): PropertyMetadata |
|
167 | |||
168 | /** |
||
169 | * @param array $config |
||
170 | * |
||
171 | * @return Method[] |
||
172 | */ |
||
173 | 48 | private function parseMethods(array $config) |
|
188 | } |
||
189 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.