AnnotationDriver.php 5.6 KB

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