YamlDriver.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. //we need read_only before setter and getter set, because that method depends on flag being set
  125. if (isset($pConfig['read_only'])) {
  126. $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
  127. }
  128. $pMetadata->setAccessor(
  129. isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
  130. isset($pConfig['accessor']['getter']) ? $pConfig['accessor']['getter'] : null,
  131. isset($pConfig['accessor']['setter']) ? $pConfig['accessor']['setter'] : null
  132. );
  133. if (isset($pConfig['inline'])) {
  134. $pMetadata->inline = (Boolean) $pConfig['inline'];
  135. }
  136. }
  137. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  138. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  139. $metadata->addPropertyMetadata($pMetadata);
  140. }
  141. }
  142. }
  143. if (isset($config['callback_methods'])) {
  144. $cConfig = $config['callback_methods'];
  145. if (isset($cConfig['pre_serialize'])) {
  146. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  147. }
  148. if (isset($cConfig['post_serialize'])) {
  149. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  150. }
  151. if (isset($cConfig['post_deserialize'])) {
  152. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  153. }
  154. }
  155. return $metadata;
  156. }
  157. protected function getExtension()
  158. {
  159. return 'yml';
  160. }
  161. private function getCallbackMetadata(\ReflectionClass $class, $config)
  162. {
  163. if (is_string($config)) {
  164. $config = array($config);
  165. } else if (!is_array($config)) {
  166. 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'])));
  167. }
  168. $methods = array();
  169. foreach ($config as $name) {
  170. if (!$class->hasMethod($name)) {
  171. throw new RuntimeException(sprintf('The method %s does not exist in class %s.', $name, $class->getName()));
  172. }
  173. $methods[] = new MethodMetadata($class->getName(), $name);
  174. }
  175. return $methods;
  176. }
  177. }