AnnotationDriver.php 5.4 KB

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