AnnotationDriver.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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\XmlKeyValuePairs;
  27. use JMS\SerializerBundle\Annotation\PostSerialize;
  28. use JMS\SerializerBundle\Annotation\PostDeserialize;
  29. use JMS\SerializerBundle\Annotation\PreSerialize;
  30. use JMS\SerializerBundle\Annotation\VirtualProperty;
  31. use Metadata\MethodMetadata;
  32. use Doctrine\Common\Annotations\Reader;
  33. use JMS\SerializerBundle\Annotation\Type;
  34. use JMS\SerializerBundle\Annotation\Exclude;
  35. use JMS\SerializerBundle\Annotation\Groups;
  36. use JMS\SerializerBundle\Annotation\Expose;
  37. use JMS\SerializerBundle\Annotation\SerializedName;
  38. use JMS\SerializerBundle\Annotation\Until;
  39. use JMS\SerializerBundle\Annotation\Since;
  40. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  41. use JMS\SerializerBundle\Annotation\Inline;
  42. use JMS\SerializerBundle\Annotation\ReadOnly;
  43. use JMS\SerializerBundle\Metadata\ClassMetadata;
  44. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  45. use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
  46. use Metadata\Driver\DriverInterface;
  47. class AnnotationDriver implements DriverInterface
  48. {
  49. private $reader;
  50. public function __construct(Reader $reader)
  51. {
  52. $this->reader = $reader;
  53. }
  54. public function loadMetadataForClass(\ReflectionClass $class)
  55. {
  56. $classMetadata = new ClassMetadata($name = $class->getName());
  57. $classMetadata->fileResources[] = $class->getFilename();
  58. $propertiesMetadata = array();
  59. $propertiesAnnotations = array();
  60. $exclusionPolicy = 'NONE';
  61. $excludeAll = false;
  62. $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
  63. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  64. if ($annot instanceof ExclusionPolicy) {
  65. $exclusionPolicy = $annot->policy;
  66. } else if ($annot instanceof XmlRoot) {
  67. $classMetadata->xmlRootName = $annot->name;
  68. } else if ($annot instanceof Exclude) {
  69. $excludeAll = true;
  70. } else if ($annot instanceof AccessType) {
  71. $classAccessType = $annot->type;
  72. } else if ($annot instanceof AccessorOrder) {
  73. $classMetadata->setAccessorOrder($annot->order, $annot->custom);
  74. }
  75. }
  76. foreach ($class->getMethods() as $method) {
  77. if ($method->getDeclaringClass()->getName() !== $name) {
  78. continue;
  79. }
  80. $methodAnnotations = $this->reader->getMethodAnnotations($method);
  81. foreach ($methodAnnotations as $annot) {
  82. if ($annot instanceof PreSerialize) {
  83. $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName()));
  84. continue 2;
  85. } else if ($annot instanceof PostDeserialize) {
  86. $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName()));
  87. continue 2;
  88. } else if ($annot instanceof PostSerialize) {
  89. $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName()));
  90. continue 2;
  91. } else if ($annot instanceof VirtualProperty) {
  92. $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $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 XmlKeyValuePairs) {
  135. $propertyMetadata->xmlKeyValuePairs = true;
  136. } else if ($annot instanceof XmlAttribute) {
  137. $propertyMetadata->xmlAttribute = true;
  138. } else if ($annot instanceof XmlValue) {
  139. $propertyMetadata->xmlValue = true;
  140. } else if ($annot instanceof AccessType) {
  141. $AccessType = $annot->type;
  142. } else if ($annot instanceof Accessor) {
  143. $accessor = array($annot->getter, $annot->setter);
  144. } else if ($annot instanceof Groups) {
  145. $propertyMetadata->groups = $annot->groups;
  146. } else if ($annot instanceof Inline) {
  147. $propertyMetadata->inline = true;
  148. } else if ($annot instanceof ReadOnly) {
  149. $propertyMetadata->readOnly = true;
  150. }
  151. }
  152. $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
  153. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  154. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  155. $classMetadata->addPropertyMetadata($propertyMetadata);
  156. }
  157. }
  158. }
  159. return $classMetadata;
  160. }
  161. }