AnnotationDriver.php 6.6 KB

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