YamlDriver.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = $isExpose = false;
  68. if (isset($config['properties'][$pName])) {
  69. $pConfig = $config['properties'][$pName];
  70. if (isset($pConfig['exclude'])) {
  71. $isExclude = (Boolean) $pConfig['exclude'];
  72. }
  73. if (isset($pConfig['expose'])) {
  74. $isExpose = (Boolean) $pConfig['expose'];
  75. }
  76. if (isset($pConfig['since_version'])) {
  77. $pMetadata->sinceVersion = (string) $pConfig['since_version'];
  78. }
  79. if (isset($pConfig['until_version'])) {
  80. $pMetadata->untilVersion = (string) $pConfig['until_version'];
  81. }
  82. if (isset($pConfig['serialized_name'])) {
  83. $pMetadata->serializedName = (string) $pConfig['serialized_name'];
  84. }
  85. if (isset($pConfig['type'])) {
  86. $pMetadata->type = (string) $pConfig['type'];
  87. }
  88. if (isset($pConfig['groups'])) {
  89. $pMetadata->groups = $pConfig['groups'];
  90. }
  91. if (isset($pConfig['xml_list'])) {
  92. $pMetadata->xmlCollection = true;
  93. $colConfig = $pConfig['xml_list'];
  94. if (isset($colConfig['inline'])) {
  95. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  96. }
  97. if (isset($colConfig['entry_name'])) {
  98. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  99. }
  100. }
  101. if (isset($pConfig['xml_map'])) {
  102. $pMetadata->xmlCollection = true;
  103. $colConfig = $pConfig['xml_map'];
  104. if (isset($colConfig['inline'])) {
  105. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  106. }
  107. if (isset($colConfig['entry_name'])) {
  108. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  109. }
  110. if (isset($colConfig['key_attribute_name'])) {
  111. $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
  112. }
  113. }
  114. if (isset($pConfig['xml_attribute'])) {
  115. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
  116. }
  117. if (isset($pConfig['xml_value'])) {
  118. $pMetadata->xmlValue = (Boolean) $pConfig['xml_value'];
  119. }
  120. if (isset($pConfig['xml_key_value_pairs'])) {
  121. $pMetadata->xmlKeyValuePairs = (Boolean) $pConfig['xml_key_value_pairs'];
  122. }
  123. $pMetadata->setAccessor(
  124. isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
  125. isset($pConfig['accessor']) ? $pConfig['accessor'] : null
  126. );
  127. if (isset($pConfig['inline'])) {
  128. $pMetadata->inline = (Boolean) $pConfig['inline'];
  129. }
  130. if (isset($pConfig['read_only'])) {
  131. $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
  132. }
  133. }
  134. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  135. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  136. $metadata->addPropertyMetadata($pMetadata);
  137. }
  138. }
  139. }
  140. if (isset($config['callback_methods'])) {
  141. $cConfig = $config['callback_methods'];
  142. if (isset($cConfig['pre_serialize'])) {
  143. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  144. }
  145. if (isset($cConfig['post_serialize'])) {
  146. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  147. }
  148. if (isset($cConfig['post_deserialize'])) {
  149. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  150. }
  151. }
  152. return $metadata;
  153. }
  154. protected function getExtension()
  155. {
  156. return 'yml';
  157. }
  158. private function getCallbackMetadata(\ReflectionClass $class, $config)
  159. {
  160. if (is_string($config)) {
  161. $config = array($config);
  162. } else if (!is_array($config)) {
  163. 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'])));
  164. }
  165. $methods = array();
  166. foreach ($config as $name) {
  167. if (!$class->hasMethod($name)) {
  168. throw new RuntimeException(sprintf('The method %s does not exist in class %s.', $name, $class->getName()));
  169. }
  170. $methods[] = new MethodMetadata($class->getName(), $name);
  171. }
  172. return $methods;
  173. }
  174. }