YamlDriver.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\Exception\RuntimeException;
  19. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  20. use Metadata\MethodMetadata;
  21. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  22. use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  23. use JMS\SerializerBundle\Metadata\ClassMetadata;
  24. use Symfony\Component\Yaml\Yaml;
  25. use Metadata\Driver\AbstractFileDriver;
  26. class YamlDriver extends AbstractFileDriver
  27. {
  28. protected function loadMetadataFromFile(\ReflectionClass $class, $file)
  29. {
  30. $config = Yaml::parse(file_get_contents($file));
  31. if (!isset($config[$name = $class->getName()])) {
  32. throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->getName(), $file));
  33. }
  34. $config = $config[$name];
  35. $metadata = new ClassMetadata($name);
  36. $metadata->fileResources[] = $file;
  37. $metadata->fileResources[] = $class->getFileName();
  38. $exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE';
  39. $excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false;
  40. $classAccessType = isset($config['access_type']) ? $config['access_type'] : PropertyMetadata::ACCESS_TYPE_PROPERTY;
  41. $propertiesMetadata = array();
  42. if (isset($config['accessor_order'])) {
  43. $metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
  44. }
  45. if (isset($config['xml_root_name'])) {
  46. $metadata->xmlRootName = (string) $config['xml_root_name'];
  47. }
  48. if (array_key_exists('virtual_properties', $config) ) {
  49. foreach ( $config['virtual_properties'] as $methodName => $propertySettings ) {
  50. if ( !$class->hasMethod( $methodName ) ) {
  51. throw new RuntimeException('The method '.$methodName.' not found in class ' . $class->name);
  52. }
  53. $virtualPropertyMetadata = new VirtualPropertyMetadata( $name, $methodName );
  54. $propertiesMetadata[$methodName] = $virtualPropertyMetadata;
  55. $config['properties'][$methodName] = $propertySettings;
  56. }
  57. }
  58. if (!$excludeAll) {
  59. foreach ($class->getProperties() as $property) {
  60. if ($name !== $property->getDeclaringClass()->getName()) {
  61. continue;
  62. }
  63. $pName = $property->getName();
  64. $propertiesMetadata[$pName] = new PropertyMetadata($name, $pName);
  65. }
  66. foreach ($propertiesMetadata as $pName => $pMetadata) {
  67. $isExclude = false;
  68. $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
  69. if (isset($config['properties'][$pName])) {
  70. $pConfig = $config['properties'][$pName];
  71. if (isset($pConfig['exclude'])) {
  72. $isExclude = (Boolean) $pConfig['exclude'];
  73. }
  74. if (isset($pConfig['expose'])) {
  75. $isExpose = (Boolean) $pConfig['expose'];
  76. }
  77. if (isset($pConfig['since_version'])) {
  78. $pMetadata->sinceVersion = (string) $pConfig['since_version'];
  79. }
  80. if (isset($pConfig['until_version'])) {
  81. $pMetadata->untilVersion = (string) $pConfig['until_version'];
  82. }
  83. if (isset($pConfig['serialized_name'])) {
  84. $pMetadata->serializedName = (string) $pConfig['serialized_name'];
  85. }
  86. if (isset($pConfig['type'])) {
  87. $pMetadata->type = (string) $pConfig['type'];
  88. }
  89. if (isset($pConfig['groups'])) {
  90. $pMetadata->groups = $pConfig['groups'];
  91. }
  92. if (isset($pConfig['xml_list'])) {
  93. $pMetadata->xmlCollection = true;
  94. $colConfig = $pConfig['xml_list'];
  95. if (isset($colConfig['inline'])) {
  96. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  97. }
  98. if (isset($colConfig['entry_name'])) {
  99. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  100. }
  101. }
  102. if (isset($pConfig['xml_map'])) {
  103. $pMetadata->xmlCollection = true;
  104. $colConfig = $pConfig['xml_map'];
  105. if (isset($colConfig['inline'])) {
  106. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  107. }
  108. if (isset($colConfig['entry_name'])) {
  109. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  110. }
  111. if (isset($colConfig['key_attribute_name'])) {
  112. $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
  113. }
  114. }
  115. if (isset($pConfig['xml_attribute'])) {
  116. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
  117. }
  118. if (isset($pConfig['xml_value'])) {
  119. $pMetadata->xmlValue = (Boolean) $pConfig['xml_value'];
  120. }
  121. if (isset($pConfig['xml_key_value_pairs'])) {
  122. $pMetadata->xmlKeyValuePairs = (Boolean) $pConfig['xml_key_value_pairs'];
  123. }
  124. $pMetadata->setAccessor(
  125. isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
  126. isset($pConfig['accessor']) ? $pConfig['accessor'] : null
  127. );
  128. if (isset($pConfig['inline'])) {
  129. $pMetadata->inline = (Boolean) $pConfig['inline'];
  130. }
  131. if (isset($pConfig['read_only'])) {
  132. $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
  133. }
  134. }
  135. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  136. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  137. $metadata->addPropertyMetadata($pMetadata);
  138. }
  139. }
  140. }
  141. if (isset($config['callback_methods'])) {
  142. $cConfig = $config['callback_methods'];
  143. if (isset($cConfig['pre_serialize'])) {
  144. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  145. }
  146. if (isset($cConfig['post_serialize'])) {
  147. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  148. }
  149. if (isset($cConfig['post_deserialize'])) {
  150. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  151. }
  152. }
  153. return $metadata;
  154. }
  155. protected function getExtension()
  156. {
  157. return 'yml';
  158. }
  159. private function getCallbackMetadata(\ReflectionClass $class, $config)
  160. {
  161. if (is_string($config)) {
  162. $config = array($config);
  163. } else if (!is_array($config)) {
  164. 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'])));
  165. }
  166. $methods = array();
  167. foreach ($config as $name) {
  168. if (!$class->hasMethod($name)) {
  169. throw new RuntimeException(sprintf('The method %s does not exist in class %s.', $name, $class->getName()));
  170. }
  171. $methods[] = new MethodMetadata($class->getName(), $name);
  172. }
  173. return $methods;
  174. }
  175. }