XmlDriver.php 9.8 KB

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