AnnotationDriver.php 7.4 KB

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