XmlFileLoader.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Mapping\Loader;
  11. use Symfony\Component\Config\Util\XmlUtils;
  12. use Symfony\Component\Serializer\Exception\MappingException;
  13. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  14. use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
  15. use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
  16. /**
  17. * Loads XML mapping files.
  18. *
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. */
  21. class XmlFileLoader extends FileLoader
  22. {
  23. /**
  24. * An array of {@class \SimpleXMLElement} instances.
  25. *
  26. * @var \SimpleXMLElement[]|null
  27. */
  28. private $classes;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function loadClassMetadata(ClassMetadataInterface $classMetadata)
  33. {
  34. if (null === $this->classes) {
  35. $this->classes = $this->getClassesFromXml();
  36. }
  37. if (!$this->classes) {
  38. return false;
  39. }
  40. $attributesMetadata = $classMetadata->getAttributesMetadata();
  41. if (isset($this->classes[$classMetadata->getName()])) {
  42. $xml = $this->classes[$classMetadata->getName()];
  43. foreach ($xml->attribute as $attribute) {
  44. $attributeName = (string) $attribute['name'];
  45. if (isset($attributesMetadata[$attributeName])) {
  46. $attributeMetadata = $attributesMetadata[$attributeName];
  47. } else {
  48. $attributeMetadata = new AttributeMetadata($attributeName);
  49. $classMetadata->addAttributeMetadata($attributeMetadata);
  50. }
  51. foreach ($attribute->group as $group) {
  52. $attributeMetadata->addGroup((string) $group);
  53. }
  54. if (isset($attribute['max-depth'])) {
  55. $attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
  56. }
  57. if (isset($attribute['serialized-name'])) {
  58. $attributeMetadata->setSerializedName((string) $attribute['serialized-name']);
  59. }
  60. if (isset($attribute['ignore'])) {
  61. $attributeMetadata->setIgnore((bool) $attribute['ignore']);
  62. }
  63. }
  64. if (isset($xml->{'discriminator-map'})) {
  65. $mapping = [];
  66. foreach ($xml->{'discriminator-map'}->mapping as $element) {
  67. $elementAttributes = $element->attributes();
  68. $mapping[(string) $elementAttributes->type] = (string) $elementAttributes->class;
  69. }
  70. $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
  71. (string) $xml->{'discriminator-map'}->attributes()->{'type-property'},
  72. $mapping
  73. ));
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. /**
  80. * Return the names of the classes mapped in this file.
  81. *
  82. * @return string[] The classes names
  83. */
  84. public function getMappedClasses()
  85. {
  86. if (null === $this->classes) {
  87. $this->classes = $this->getClassesFromXml();
  88. }
  89. return array_keys($this->classes);
  90. }
  91. /**
  92. * Parses an XML File.
  93. *
  94. * @throws MappingException
  95. */
  96. private function parseFile(string $file): \SimpleXMLElement
  97. {
  98. try {
  99. $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
  100. } catch (\Exception $e) {
  101. throw new MappingException($e->getMessage(), $e->getCode(), $e);
  102. }
  103. return simplexml_import_dom($dom);
  104. }
  105. private function getClassesFromXml(): array
  106. {
  107. $xml = $this->parseFile($this->file);
  108. $classes = [];
  109. foreach ($xml->class as $class) {
  110. $classes[(string) $class['name']] = $class;
  111. }
  112. return $classes;
  113. }
  114. }