AnnotationDriver.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, $method->getName());
  92. $propertiesMetadata[] = $virtualPropertyMetadata;
  93. $propertiesAnnotations[] = $methodAnnotations;
  94. continue 2;
  95. }
  96. }
  97. }
  98. if (!$excludeAll) {
  99. foreach ($class->getProperties() as $property) {
  100. if ($property->getDeclaringClass()->getName() !== $name) {
  101. continue;
  102. }
  103. $propertiesMetadata[] = new PropertyMetadata($name, $property->getName());
  104. $propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property);
  105. }
  106. foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
  107. $isExclude = $isExpose = false;
  108. $AccessType = $classAccessType;
  109. $accessor = array(null, null);
  110. $propertyAnnotations = $propertiesAnnotations[$propertyKey];
  111. foreach ($propertyAnnotations as $annot) {
  112. if ($annot instanceof Since) {
  113. $propertyMetadata->sinceVersion = $annot->version;
  114. } else if ($annot instanceof Until) {
  115. $propertyMetadata->untilVersion = $annot->version;
  116. } else if ($annot instanceof SerializedName) {
  117. $propertyMetadata->serializedName = $annot->name;
  118. } else if ($annot instanceof Expose) {
  119. $isExpose = true;
  120. } else if ($annot instanceof Exclude) {
  121. $isExclude = true;
  122. } else if ($annot instanceof Type) {
  123. $propertyMetadata->type = $annot->name;
  124. } else if ($annot instanceof XmlList) {
  125. $propertyMetadata->xmlCollection = true;
  126. $propertyMetadata->xmlCollectionInline = $annot->inline;
  127. $propertyMetadata->xmlEntryName = $annot->entry;
  128. } else if ($annot instanceof XmlMap) {
  129. $propertyMetadata->xmlCollection = true;
  130. $propertyMetadata->xmlCollectionInline = $annot->inline;
  131. $propertyMetadata->xmlEntryName = $annot->entry;
  132. $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
  133. } else if ($annot instanceof XmlAttribute) {
  134. $propertyMetadata->xmlAttribute = true;
  135. } else if ($annot instanceof XmlValue) {
  136. $propertyMetadata->xmlValue = true;
  137. } else if ($annot instanceof AccessType) {
  138. $AccessType = $annot->type;
  139. } else if ($annot instanceof Accessor) {
  140. $accessor = array($annot->getter, $annot->setter);
  141. } else if ($annot instanceof Groups) {
  142. $propertyMetadata->groups = $annot->groups;
  143. } else if ($annot instanceof Inline) {
  144. $propertyMetadata->inline = true;
  145. } else if ($annot instanceof ReadOnly) {
  146. $propertyMetadata->readOnly = true;
  147. }
  148. }
  149. $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
  150. if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
  151. || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
  152. $classMetadata->addPropertyMetadata($propertyMetadata);
  153. }
  154. }
  155. }
  156. return $classMetadata;
  157. }
  158. }