AnnotationDriver.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\PostSerialize;
  19. use JMS\SerializerBundle\Annotation\PostDeserialize;
  20. use JMS\SerializerBundle\Annotation\PreSerialize;
  21. use JMS\SerializerBundle\Annotation\PreDeserialize;
  22. use Metadata\MethodMetadata;
  23. use Doctrine\Common\Annotations\Reader;
  24. use JMS\SerializerBundle\Annotation\Type;
  25. use JMS\SerializerBundle\Annotation\Exclude;
  26. use JMS\SerializerBundle\Annotation\Expose;
  27. use JMS\SerializerBundle\Annotation\SerializedName;
  28. use JMS\SerializerBundle\Annotation\Until;
  29. use JMS\SerializerBundle\Annotation\Since;
  30. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  31. use JMS\SerializerBundle\Metadata\ClassMetadata;
  32. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  33. use Metadata\Driver\DriverInterface;
  34. class AnnotationDriver implements DriverInterface
  35. {
  36. private $reader;
  37. public function __construct(Reader $reader)
  38. {
  39. $this->reader = $reader;
  40. }
  41. public function loadMetadataForClass(\ReflectionClass $class)
  42. {
  43. $classMetadata = new ClassMetadata($name = $class->getName());
  44. $classMetadata->fileResources[] = $class->getFilename();
  45. foreach ($this->reader->getClassAnnotations($class) as $annot) {
  46. if ($annot instanceof ExclusionPolicy) {
  47. $classMetadata->exclusionPolicy = $annot->policy;
  48. }
  49. }
  50. foreach ($class->getProperties() as $property) {
  51. if ($property->getDeclaringClass()->getName() !== $name) {
  52. continue;
  53. }
  54. $propertyMetadata = new PropertyMetadata($name, $property->getName());
  55. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  56. if ($annot instanceof Since) {
  57. $propertyMetadata->sinceVersion = $annot->version;
  58. } else if ($annot instanceof Until) {
  59. $propertyMetadata->untilVersion = $annot->version;
  60. } else if ($annot instanceof SerializedName) {
  61. $propertyMetadata->serializedName = $annot->name;
  62. } else if ($annot instanceof Expose) {
  63. $propertyMetadata->exposed = true;
  64. } else if ($annot instanceof Exclude) {
  65. $propertyMetadata->excluded = true;
  66. } else if ($annot instanceof Type) {
  67. $propertyMetadata->type = $annot->name;
  68. }
  69. }
  70. $classMetadata->addPropertyMetadata($propertyMetadata);
  71. }
  72. foreach ($class->getMethods() as $method) {
  73. if ($method->getDeclaringClass()->getName() !== $name) {
  74. continue;
  75. }
  76. foreach ($this->reader->getMethodAnnotations($method) as $annot) {
  77. if ($annot instanceof PreSerialize) {
  78. $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName()));
  79. continue 2;
  80. } else if ($annot instanceof PostDeserialize) {
  81. $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName()));
  82. continue 2;
  83. } else if ($annot instanceof PostSerialize) {
  84. $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName()));
  85. continue 2;
  86. } else if ($annot instanceof PreDeserialize) {
  87. $classMetadata->addPreDeserializeMethod(new MethodMetadata($name, $method->getName()));
  88. continue 2;
  89. }
  90. }
  91. }
  92. return $classMetadata;
  93. }
  94. }