YamlDriver.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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']) ? $config['exclusion_policy'] : 'NONE';
  38. $excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false;
  39. if (isset($config['xml_root_name'])) {
  40. $metadata->xmlRootName = (string) $config['xml_root_name'];
  41. }
  42. if (!$excludeAll) {
  43. foreach ($class->getProperties() as $property) {
  44. if ($name !== $property->getDeclaringClass()->getName()) {
  45. continue;
  46. }
  47. $pMetadata = new PropertyMetadata($name, $pName = $property->getName());
  48. $isExclude = $isExpose = false;
  49. if (isset($config['properties'][$pName])) {
  50. $pConfig = $config['properties'][$pName];
  51. if (isset($pConfig['exclude'])) {
  52. $isExclude = (Boolean) $pConfig['exclude'];
  53. }
  54. if (isset($pConfig['expose'])) {
  55. $isExpose = (Boolean) $pConfig['expose'];
  56. }
  57. if (isset($pConfig['since_version'])) {
  58. $pMetadata->sinceVersion = (string) $pConfig['since_version'];
  59. }
  60. if (isset($pConfig['until_version'])) {
  61. $pMetadata->untilVersion = (string) $pConfig['until_version'];
  62. }
  63. if (isset($pConfig['serialized_name'])) {
  64. $pMetadata->serializedName = (string) $pConfig['serialized_name'];
  65. }
  66. if (isset($pConfig['type'])) {
  67. $pMetadata->type = (string) $pConfig['type'];
  68. }
  69. if (isset($pConfig['xml_list'])) {
  70. $pMetadata->xmlCollection = true;
  71. $colConfig = $pConfig['xml_list'];
  72. if (isset($colConfig['inline'])) {
  73. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  74. }
  75. if (isset($colConfig['entry_name'])) {
  76. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  77. }
  78. }
  79. if (isset($pConfig['xml_map'])) {
  80. $pMetadata->xmlCollection = true;
  81. $colConfig = $pConfig['xml_map'];
  82. if (isset($colConfig['inline'])) {
  83. $pMetadata->xmlCollectionInline = (Boolean) $colConfig['inline'];
  84. }
  85. if (isset($colConfig['entry_name'])) {
  86. $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
  87. }
  88. if (isset($colConfig['key_attribute_name'])) {
  89. $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
  90. }
  91. }
  92. if (isset($pConfig['xml_attribute'])) {
  93. $pMetadata->xmlAttribute = (Boolean) $pConfig['xml_attribute'];
  94. }
  95. if (isset($pConfig['xml_value'])) {
  96. $pMetadata->xmlValue = (Boolean) $pConfig['xml_value'];
  97. }
  98. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  99. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  100. $metadata->addPropertyMetadata($pMetadata);
  101. }
  102. }
  103. }
  104. }
  105. if (isset($config['callback_methods'])) {
  106. $cConfig = $config['callback_methods'];
  107. if (isset($cConfig['pre_serialize'])) {
  108. $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
  109. }
  110. if (isset($cConfig['post_serialize'])) {
  111. $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
  112. }
  113. if (isset($cConfig['post_deserialize'])) {
  114. $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
  115. }
  116. }
  117. return $metadata;
  118. }
  119. protected function getExtension()
  120. {
  121. return 'yml';
  122. }
  123. private function getCallbackMetadata(\ReflectionClass $class, $config)
  124. {
  125. if (is_string($config)) {
  126. $config = array($config);
  127. } else if (!is_array($config)) {
  128. 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'])));
  129. }
  130. $methods = array();
  131. foreach ($config as $name) {
  132. if (!$class->hasMethod($name)) {
  133. throw new RuntimeException(sprintf('The method %s does not exist in class %s.', $mName, $name));
  134. }
  135. $methods[] = new MethodMetadata($class->getName(), $name);
  136. }
  137. return $methods;
  138. }
  139. }