AnnotationDriver.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Annotation\AccessorOrder;
  19. use JMS\SerializerBundle\Annotation\Groups;
  20. use JMS\SerializerBundle\Annotation\Accessor;
  21. use JMS\SerializerBundle\Annotation\AccessType;
  22. use JMS\SerializerBundle\Annotation\XmlMap;
  23. use JMS\SerializerBundle\Annotation\XmlRoot;
  24. use JMS\SerializerBundle\Annotation\XmlAttribute;
  25. use JMS\SerializerBundle\Annotation\XmlList;
  26. use JMS\SerializerBundle\Annotation\XmlValue;
  27. use JMS\SerializerBundle\Annotation\PostSerialize;
  28. use JMS\SerializerBundle\Annotation\PostDeserialize;
  29. use JMS\SerializerBundle\Annotation\PreSerialize;
  30. use Metadata\MethodMetadata;
  31. use Doctrine\Common\Annotations\Reader;
  32. use JMS\SerializerBundle\Annotation\Type;
  33. use JMS\SerializerBundle\Annotation\Exclude;
  34. use JMS\SerializerBundle\Annotation\Expose;
  35. use JMS\SerializerBundle\Annotation\SerializedName;
  36. use JMS\SerializerBundle\Annotation\Until;
  37. use JMS\SerializerBundle\Annotation\Since;
  38. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  39. use JMS\SerializerBundle\Annotation\Inline;
  40. use JMS\SerializerBundle\Metadata\ClassMetadata;
  41. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  42. use Metadata\Driver\DriverInterface;
  43. class AnnotationDriver implements DriverInterface
  44. {
  45. private $reader;
  46. public function __construct(Reader $reader)
  47. {
  48. $this->reader = $reader;
  49. }
  50. public function loadMetadataForClass(\ReflectionClass $class)
  51. {
  52. $classMetadata = new ClassMetadata($name = $class->getName());
  53. $classMetadata->fileResources[] = $class->getFilename();
  54. $exclusionPolicy = 'NONE';
  55. $excludeAll = false;
  56. $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
  57. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  58. if ($annot instanceof ExclusionPolicy) {
  59. $exclusionPolicy = $annot->policy;
  60. } else if ($annot instanceof XmlRoot) {
  61. $classMetadata->xmlRootName = $annot->name;
  62. } else if ($annot instanceof Exclude) {
  63. $excludeAll = true;
  64. } else if ($annot instanceof AccessType) {
  65. $classAccessType = $annot->type;
  66. } else if ($annot instanceof AccessorOrder) {
  67. $classMetadata->setAccessorOrder($annot->order, $annot->custom);
  68. }
  69. }
  70. if (!$excludeAll) {
  71. foreach ($class->getProperties() as $property) {
  72. if ($property->getDeclaringClass()->getName() !== $name) {
  73. continue;
  74. }
  75. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  76. $isExclude = $isExpose = false;
  77. $AccessType = $classAccessType;
  78. $accessor = array(null, null);
  79. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  80. if ($annot instanceof Since) {
  81. $propertyMetadata->sinceVersion = $annot->version;
  82. } else if ($annot instanceof Until) {
  83. $propertyMetadata->untilVersion = $annot->version;
  84. } else if ($annot instanceof SerializedName) {
  85. $propertyMetadata->serializedName = $annot->name;
  86. } else if ($annot instanceof Expose) {
  87. $isExpose = true;
  88. } else if ($annot instanceof Exclude) {
  89. $isExclude = true;
  90. } else if ($annot instanceof Type) {
  91. $propertyMetadata->type = $annot->name;
  92. } else if ($annot instanceof XmlList) {
  93. $propertyMetadata->xmlCollection = true;
  94. $propertyMetadata->xmlCollectionInline = $annot->inline;
  95. $propertyMetadata->xmlEntryName = $annot->entry;
  96. } else if ($annot instanceof XmlMap) {
  97. $propertyMetadata->xmlCollection = true;
  98. $propertyMetadata->xmlCollectionInline = $annot->inline;
  99. $propertyMetadata->xmlEntryName = $annot->entry;
  100. $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
  101. } else if ($annot instanceof XmlAttribute) {
  102. $propertyMetadata->xmlAttribute = true;
  103. } else if ($annot instanceof XmlValue) {
  104. $propertyMetadata->xmlValue = true;
  105. } else if ($annot instanceof AccessType) {
  106. $AccessType = $annot->type;
  107. } else if ($annot instanceof Accessor) {
  108. $accessor = array($annot->getter, $annot->setter);
  109. } else if ($annot instanceof Groups) {
  110. $propertyMetadata->setGroups($annot->names);
  111. } else if ($annot instanceof Inline) {
  112. $propertyMetadata->inline = true;
  113. }
  114. }
  115. $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
  116. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  117. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  118. $classMetadata->addPropertyMetadata($propertyMetadata);
  119. }
  120. }
  121. }
  122. foreach ($class->getMethods() as $method) {
  123. if ($method->getDeclaringClass()->getName() !== $name) {
  124. continue;
  125. }
  126. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  127. if ($annot instanceof PreSerialize) {
  128. $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName()));
  129. continue 2;
  130. } else if ($annot instanceof PostDeserialize) {
  131. $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName()));
  132. continue 2;
  133. } else if ($annot instanceof PostSerialize) {
  134. $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName()));
  135. continue 2;
  136. }
  137. }
  138. }
  139. return $classMetadata;
  140. }
  141. }