YamlDriver.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Metadata\Driver;
  18. use JMS\SerializerBundle\Serializer\GraphNavigator;
  19. use JMS\SerializerBundle\Exception\RuntimeException;
  20. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  21. use Metadata\MethodMetadata;
  22. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  23. use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  24. use JMS\SerializerBundle\Metadata\ClassMetadata;
  25. use Symfony\Component\Yaml\Yaml;
  26. use Metadata\Driver\AbstractFileDriver;
  27. class YamlDriver extends AbstractFileDriver
  28. {
  29. protected function loadMetadataFromFile(\ReflectionClass $class, $file)
  30. {
  31. $config = Yaml::parse(file_get_contents($file));
  32. if (!isset($config[$name = $class->getName()])) {
  33. throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->getName(), $file));
  34. }
  35. $config = $config[$name];
  36. $metadata = new ClassMetadata($name);
  37. $metadata->fileResources[] = $file;
  38. $metadata->fileResources[] = $class->getFileName();
  39. $exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE';
  40. $excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false;
  41. $classAccessType = isset($config['access_type']) ? $config['access_type'] : PropertyMetadata::ACCESS_TYPE_PROPERTY;
  42. $propertiesMetadata = array();
  43. if (isset($config['accessor_order'])) {
  44. $metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
  45. }
  46. if (isset($config['xml_root_name'])) {
  47. $metadata->xmlRootName = (string) $config['xml_root_name'];
  48. }
  49. if (array_key_exists('virtual_properties', $config) ) {
  50. foreach ( $config['virtual_properties'] as $methodName => $propertySettings ) {
  51. if ( !$class->hasMethod( $methodName ) ) {
  52. throw new RuntimeException('The method '.$methodName.' not found in class ' . $class->name);
  53. }
  54. $virtualPropertyMetadata = new VirtualPropertyMetadata( $name, $methodName );
  55. $propertiesMetadata[$methodName] = $virtualPropertyMetadata;
  56. $config['properties'][$methodName] = $propertySettings;
  57. }
  58. }
  59. if (!$excludeAll) {
  60. foreach ($class->getProperties() as $property) {
  61. if ($name !== $property->getDeclaringClass()->getName()) {
  62. continue;
  63. }
  64. $pName = $property->getName();
  65. $propertiesMetadata[$pName] = new PropertyMetadata($name, $pName);
  66. }
  67. foreach ($propertiesMetadata as $pName => $pMetadata) {
  68. $isExclude = false;
  69. $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
  70. if (isset($config['properties'][$pName])) {
  71. $pConfig = $config['properties'][$pName];
  72. if (isset($pConfig['exclude'])) {
  73. $isExclude = (Boolean) $pConfig['exclude'];
  74. }
  75. if (isset($pConfig['expose'])) {
  76. $isExpose = (Boolean) $pConfig['expose'];
  77. }
  78. if (isset($pConfig['since_version'])) {
  79. $pMetadata->sinceVersion = (string) $pConfig['since_version'];
  80. }
  81. if (isset($pConfig['until_version'])) {
  82. $pMetadata->untilVersion = (string) $pConfig['until_version'];
  83. }
  84. if (isset($pConfig['serialized_name'])) {
  85. $pMetadata->serializedName = (string) $pConfig['serialized_name'];
  86. }
  87. if (isset($pConfig['type'])) {
  88. $pMetadata->setType((string) $pConfig['type']);
  89. }
  90. if (isset($pConfig['groups'])) {
  91. $pMetadata->groups = $pConfig['groups'];
  92. }
  93. if (isset($pConfig['xml_list'])) {
  94. $pMetadata->xmlCollection = true;
  95. $colConfig = $pConfig['xml_list'];
  96. if (isset($colConfig['inline'])) {
  97. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  98. }
  99. if (isset($colConfig['entry_name'])) {
  100. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  101. }
  102. }
  103. if (isset($pConfig['xml_map'])) {
  104. $pMetadata->xmlCollection = true;
  105. $colConfig = $pConfig['xml_map'];
  106. if (isset($colConfig['inline'])) {
  107. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  108. }
  109. if (isset($colConfig['entry_name'])) {
  110. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  111. }
  112. if (isset($colConfig['key_attribute_name'])) {
  113. $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
  114. }
  115. }
  116. if (isset($pConfig['xml_attribute'])) {
  117. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
  118. }
  119. if (isset($pConfig['xml_attribute_map'])) {
  120. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute_map'];
  121. }
  122. if (isset($pConfig['xml_value'])) {
  123. $pMetadata->xmlValue = (Boolean) $pConfig['xml_value'];
  124. }
  125. if (isset($pConfig['xml_key_value_pairs'])) {
  126. $pMetadata->xmlKeyValuePairs = (Boolean) $pConfig['xml_key_value_pairs'];
  127. }
  128. //we need read_only before setter and getter set, because that method depends on flag being set
  129. if (isset($pConfig['read_only'])) {
  130. $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
  131. }
  132. $pMetadata->setAccessor(
  133. isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
  134. isset($pConfig['accessor']['getter']) ? $pConfig['accessor']['getter'] : null,
  135. isset($pConfig['accessor']['setter']) ? $pConfig['accessor']['setter'] : null
  136. );
  137. if (isset($pConfig['inline'])) {
  138. $pMetadata->inline = (Boolean) $pConfig['inline'];
  139. }
  140. }
  141. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  142. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  143. $metadata->addPropertyMetadata($pMetadata);
  144. }
  145. }
  146. }
  147. if (isset($config['handler_callbacks'])) {
  148. foreach ($config['handler_callbacks'] as $direction => $formats) {
  149. foreach ($formats as $format => $methodName) {
  150. $direction = GraphNavigator::parseDirection($direction);
  151. $metadata->addHandlerCallback($direction, $format, $methodName);
  152. }
  153. }
  154. }
  155. if (isset($config['callback_methods'])) {
  156. $cConfig = $config['callback_methods'];
  157. if (isset($cConfig['pre_serialize'])) {
  158. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  159. }
  160. if (isset($cConfig['post_serialize'])) {
  161. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  162. }
  163. if (isset($cConfig['post_deserialize'])) {
  164. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  165. }
  166. }
  167. return $metadata;
  168. }
  169. protected function getExtension()
  170. {
  171. return 'yml';
  172. }
  173. private function getCallbackMetadata(\ReflectionClass $class, $config)
  174. {
  175. if (is_string($config)) {
  176. $config = array($config);
  177. } else if (!is_array($config)) {
  178. throw new RuntimeException(sprintf('callback methods expects a string, or an array of strings that represent method names, but got %s.', json_encode($cConfig['pre_serialize'])));
  179. }
  180. $methods = array();
  181. foreach ($config as $name) {
  182. if (!$class->hasMethod($name)) {
  183. throw new RuntimeException(sprintf('The method %s does not exist in class %s.', $name, $class->getName()));
  184. }
  185. $methods[] = new MethodMetadata($class->getName(), $name);
  186. }
  187. return $methods;
  188. }
  189. }