AnnotationDriver.php 3.7 KB

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