AnnotationDriver.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = false;
  109. $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata;
  110. $AccessType = $classAccessType;
  111. $accessor = array(null, null);
  112. $propertyAnnotations = $propertiesAnnotations[$propertyKey];
  113. foreach ($propertyAnnotations as $annot) {
  114. if ($annot instanceof Since) {
  115. $propertyMetadata->sinceVersion = $annot->version;
  116. } else if ($annot instanceof Until) {
  117. $propertyMetadata->untilVersion = $annot->version;
  118. } else if ($annot instanceof SerializedName) {
  119. $propertyMetadata->serializedName = $annot->name;
  120. } else if ($annot instanceof Expose) {
  121. $isExpose = true;
  122. } else if ($annot instanceof Exclude) {
  123. $isExclude = true;
  124. } else if ($annot instanceof Type) {
  125. $propertyMetadata->type = $annot->name;
  126. } else if ($annot instanceof XmlList) {
  127. $propertyMetadata->xmlCollection = true;
  128. $propertyMetadata->xmlCollectionInline = $annot->inline;
  129. $propertyMetadata->xmlEntryName = $annot->entry;
  130. } else if ($annot instanceof XmlMap) {
  131. $propertyMetadata->xmlCollection = true;
  132. $propertyMetadata->xmlCollectionInline = $annot->inline;
  133. $propertyMetadata->xmlEntryName = $annot->entry;
  134. $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
  135. } else if ($annot instanceof XmlKeyValuePairs) {
  136. $propertyMetadata->xmlKeyValuePairs = true;
  137. } else if ($annot instanceof XmlAttribute) {
  138. $propertyMetadata->xmlAttribute = true;
  139. } else if ($annot instanceof XmlValue) {
  140. $propertyMetadata->xmlValue = true;
  141. } else if ($annot instanceof AccessType) {
  142. $AccessType = $annot->type;
  143. } else if ($annot instanceof ReadOnly) {
  144. $propertyMetadata->readOnly = true;
  145. } else if ($annot instanceof Accessor) {
  146. $accessor = array($annot->getter, $annot->setter);
  147. } else if ($annot instanceof Groups) {
  148. $propertyMetadata->groups = $annot->groups;
  149. } else if ($annot instanceof Inline) {
  150. $propertyMetadata->inline = true;
  151. }
  152. }
  153. $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
  154. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  155. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  156. $classMetadata->addPropertyMetadata($propertyMetadata);
  157. }
  158. }
  159. }
  160. return $classMetadata;
  161. }
  162. }