AnnotationDriver.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\VirtualProperty;
  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. $propertiesMetadata = array();
  58. $propertiesAnnotations = array();
  59. $exclusionPolicy = 'NONE';
  60. $excludeAll = false;
  61. $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
  62. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  63. if ($annot instanceof ExclusionPolicy) {
  64. $exclusionPolicy = $annot->policy;
  65. } else if ($annot instanceof XmlRoot) {
  66. $classMetadata->xmlRootName = $annot->name;
  67. } else if ($annot instanceof Exclude) {
  68. $excludeAll = true;
  69. } else if ($annot instanceof AccessType) {
  70. $classAccessType = $annot->type;
  71. } else if ($annot instanceof AccessorOrder) {
  72. $classMetadata->setAccessorOrder($annot->order, $annot->custom);
  73. }
  74. }
  75. foreach ($class->getMethods() as $method) {
  76. if ($method->getDeclaringClass()->getName() !== $name) {
  77. continue;
  78. }
  79. $methodAnnotations = $this->reader->getMethodAnnotations($method);
  80. foreach ($methodAnnotations as $annot) {
  81. if ($annot instanceof PreSerialize) {
  82. $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName()));
  83. continue 2;
  84. } else if ($annot instanceof PostDeserialize) {
  85. $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName()));
  86. continue 2;
  87. } else if ($annot instanceof PostSerialize) {
  88. $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName()));
  89. continue 2;
  90. } else if ($annot instanceof VirtualProperty) {
  91. $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $annot->field);
  92. $virtualPropertyMetadata->getter = $method->getName();
  93. $propertiesMetadata[] = $virtualPropertyMetadata;
  94. $propertiesAnnotations[] = $methodAnnotations;
  95. continue 2;
  96. }
  97. }
  98. }
  99. if (!$excludeAll) {
  100. foreach ($class->getProperties() as $property) {
  101. if ($property->getDeclaringClass()->getName() !== $name) {
  102. continue;
  103. }
  104. $propertiesMetadata[] = new PropertyMetadata($name, $property->getName());
  105. $propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property);
  106. }
  107. foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
  108. $isExclude = $isExpose = false;
  109. $AccessType = $classAccessType;
  110. $accessor = array(null, null);
  111. $propertyAnnotations = $propertiesAnnotations[$propertyKey];
  112. foreach ($propertyAnnotations as $annot) {
  113. if ($annot instanceof Since) {
  114. $propertyMetadata->sinceVersion = $annot->version;
  115. } else if ($annot instanceof Until) {
  116. $propertyMetadata->untilVersion = $annot->version;
  117. } else if ($annot instanceof SerializedName) {
  118. $propertyMetadata->serializedName = $annot->name;
  119. } else if ($annot instanceof Expose) {
  120. $isExpose = true;
  121. } else if ($annot instanceof Exclude) {
  122. $isExclude = true;
  123. } else if ($annot instanceof Type) {
  124. $propertyMetadata->type = $annot->name;
  125. } else if ($annot instanceof XmlList) {
  126. $propertyMetadata->xmlCollection = true;
  127. $propertyMetadata->xmlCollectionInline = $annot->inline;
  128. $propertyMetadata->xmlEntryName = $annot->entry;
  129. } else if ($annot instanceof XmlMap) {
  130. $propertyMetadata->xmlCollection = true;
  131. $propertyMetadata->xmlCollectionInline = $annot->inline;
  132. $propertyMetadata->xmlEntryName = $annot->entry;
  133. $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
  134. } else if ($annot instanceof XmlAttribute) {
  135. $propertyMetadata->xmlAttribute = true;
  136. } else if ($annot instanceof XmlValue) {
  137. $propertyMetadata->xmlValue = true;
  138. } else if ($annot instanceof AccessType) {
  139. $AccessType = $annot->type;
  140. } else if ($annot instanceof Accessor) {
  141. $accessor = array($annot->getter, $annot->setter);
  142. } else if ($annot instanceof Groups) {
  143. $propertyMetadata->groups = $annot->groups;
  144. } else if ($annot instanceof Inline) {
  145. $propertyMetadata->inline = true;
  146. } else if ($annot instanceof ReadOnly) {
  147. $propertyMetadata->readOnly = true;
  148. }
  149. }
  150. $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
  151. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  152. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  153. $classMetadata->addPropertyMetadata($propertyMetadata);
  154. }
  155. }
  156. }
  157. return $classMetadata;
  158. }
  159. }