XmlDriver.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\Serializer\GraphNavigator;
  19. use JMS\SerializerBundle\Exception\RuntimeException;
  20. use JMS\SerializerBundle\Exception\XmlErrorException;
  21. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  22. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  23. use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  24. use Metadata\MethodMetadata;
  25. use JMS\SerializerBundle\Metadata\ClassMetadata;
  26. use Metadata\Driver\AbstractFileDriver;
  27. class XmlDriver extends AbstractFileDriver
  28. {
  29. protected function loadMetadataFromFile(\ReflectionClass $class, $path)
  30. {
  31. $previous = libxml_use_internal_errors(true);
  32. $elem = simplexml_load_file($path);
  33. libxml_use_internal_errors($previous);
  34. if (false === $elem) {
  35. throw new XmlErrorException(libxml_get_last_error());
  36. }
  37. $metadata = new ClassMetadata($name = $class->getName());
  38. if (!$elems = $elem->xpath("./class[@name = '".$name."']")) {
  39. throw new RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
  40. }
  41. $elem = reset($elems);
  42. $metadata->fileResources[] = $path;
  43. $metadata->fileResources[] = $class->getFileName();
  44. $exclusionPolicy = strtoupper($elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
  45. $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower($exclude) : false;
  46. $classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
  47. $propertiesMetadata = array();
  48. $propertiesNodes = array();
  49. if (null !== $accessorOrder = $elem->attributes()->{'accessor-order'}) {
  50. $metadata->setAccessorOrder((string) $accessorOrder, preg_split('/\s*,\s*/', (string) $elem->attributes()->{'custom-accessor-order'}));
  51. }
  52. if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
  53. $metadata->xmlRootName = (string) $xmlRootName;
  54. }
  55. foreach ($elem->xpath('./virtual-property') as $method) {
  56. if (!isset($method->attributes()->method)) {
  57. throw new RuntimeException('The method attribute must be set for all virtual-property elements.');
  58. }
  59. $virtualPropertyMetadata = new VirtualPropertyMetadata( $name, (string) $method->attributes()->method );
  60. $propertiesMetadata[] = $virtualPropertyMetadata;
  61. $propertiesNodes[] = $method;
  62. }
  63. if (!$excludeAll) {
  64. foreach ($class->getProperties() as $property) {
  65. if ($name !== $property->getDeclaringClass()->getName()) {
  66. continue;
  67. }
  68. $propertiesMetadata[] = new PropertyMetadata($name, $pName = $property->getName());
  69. $pElems = $elem->xpath("./property[@name = '".$pName."']");
  70. $propertiesNodes[] = $pElems ? reset( $pElems ) : null;
  71. }
  72. foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
  73. $isExclude = false;
  74. $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
  75. $pElem = $propertiesNodes[$propertyKey];
  76. if (!empty( $pElem )) {
  77. if (null !== $exclude = $pElem->attributes()->exclude) {
  78. $isExclude = 'true' === strtolower($exclude);
  79. }
  80. if (null !== $expose = $pElem->attributes()->expose) {
  81. $isExpose = 'true' === strtolower($expose);
  82. }
  83. if (null !== $version = $pElem->attributes()->{'since-version'}) {
  84. $pMetadata->sinceVersion = (string) $version;
  85. }
  86. if (null !== $version = $pElem->attributes()->{'until-version'}) {
  87. $pMetadata->untilVersion = (string) $version;
  88. }
  89. if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
  90. $pMetadata->serializedName = (string) $serializedName;
  91. }
  92. if (null !== $type = $pElem->attributes()->type) {
  93. $pMetadata->setType((string) $type);
  94. } else if (isset($pElem->type)) {
  95. $pMetadata->setType((string) $pElem->type);
  96. }
  97. if (null !== $groups = $pElem->attributes()->groups) {
  98. $pMetadata->groups = preg_split('/\s*,\s*/', (string) $groups);
  99. }
  100. if (isset($pElem->{'xml-list'})) {
  101. $pMetadata->xmlCollection = true;
  102. $colConfig = $pElem->{'xml-list'};
  103. if (isset($colConfig->attributes()->inline)) {
  104. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  105. }
  106. if (isset($colConfig->attributes()->{'entry-name'})) {
  107. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  108. }
  109. }
  110. if (isset($pElem->{'xml-map'})) {
  111. $pMetadata->xmlCollection = true;
  112. $colConfig = $pElem->{'xml-map'};
  113. if (isset($colConfig->attributes()->inline)) {
  114. $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
  115. }
  116. if (isset($colConfig->attributes()->{'entry-name'})) {
  117. $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
  118. }
  119. if (isset($colConfig->attributes()->{'key-attribute-name'})) {
  120. $pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'};
  121. }
  122. }
  123. if (isset($pElem->attributes()->{'xml-attribute'})) {
  124. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
  125. }
  126. if (isset($pElem->attributes()->{'xml-attribute-map'})) {
  127. $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute-map'};
  128. }
  129. if (isset($pElem->attributes()->{'xml-value'})) {
  130. $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'};
  131. }
  132. if (isset($pElem->attributes()->{'xml-key-value-pairs'})) {
  133. $pMetadata->xmlKeyValuePairs = 'true' === (string) $pElem->attributes()->{'xml-key-value-pairs'};
  134. }
  135. //we need read-only before setter and getter set, because that method depends on flag being set
  136. if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
  137. $pMetadata->readOnly = 'true' === strtolower($readOnly);
  138. }
  139. $getter = $pElem->attributes()->{'accessor-getter'};
  140. $setter = $pElem->attributes()->{'accessor-setter'};
  141. $pMetadata->setAccessor(
  142. (string) ($pElem->attributes()->{'access-type'} ?: $classAccessType),
  143. $getter ? (string) $getter : null,
  144. $setter ? (string) $setter : null
  145. );
  146. if (null !== $inline = $pElem->attributes()->inline) {
  147. $pMetadata->inline = 'true' === strtolower($inline);
  148. }
  149. }
  150. if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
  151. || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)) {
  152. $metadata->addPropertyMetadata($pMetadata);
  153. }
  154. }
  155. }
  156. foreach ($elem->xpath('./callback-method') as $method) {
  157. if (!isset($method->attributes()->type)) {
  158. throw new RuntimeException('The type attribute must be set for all callback-method elements.');
  159. }
  160. if (!isset($method->attributes()->name)) {
  161. throw new RuntimeException('The name attribute must be set for all callback-method elements.');
  162. }
  163. switch ((string) $method->attributes()->type) {
  164. case 'pre-serialize':
  165. $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  166. break;
  167. case 'post-serialize':
  168. $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  169. break;
  170. case 'post-deserialize':
  171. $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
  172. break;
  173. case 'handler':
  174. if ( ! isset($method->attributes()->format)) {
  175. throw new RuntimeException('The format attribute must be set for "handler" callback methods.');
  176. }
  177. if ( ! isset($method->attributes()->direction)) {
  178. throw new RuntimeException('The direction attribute must be set for "handler" callback methods.');
  179. }
  180. $direction = GraphNavigator::parseDirection((string) $method->attributes()->direction);
  181. $format = (string) $method->attributes()->format;
  182. $metadata->addHandlerCallback($direction, $format, (string) $method->attributes()->name);
  183. break;
  184. default:
  185. throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
  186. }
  187. }
  188. return $metadata;
  189. }
  190. protected function getExtension()
  191. {
  192. return 'xml';
  193. }
  194. }