YamlDriver.php 7.3 KB

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