AnnotationLoader.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Mapping\Loader;
  11. use Doctrine\Common\Annotations\Reader;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. use Symfony\Component\Serializer\Annotation\MaxDepth;
  14. use Symfony\Component\Serializer\Exception\MappingException;
  15. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  16. use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
  17. /**
  18. * Annotation loader.
  19. *
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. */
  22. class AnnotationLoader implements LoaderInterface
  23. {
  24. /**
  25. * @var Reader
  26. */
  27. private $reader;
  28. /**
  29. * @param Reader $reader
  30. */
  31. public function __construct(Reader $reader)
  32. {
  33. $this->reader = $reader;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function loadClassMetadata(ClassMetadataInterface $classMetadata)
  39. {
  40. $reflectionClass = $classMetadata->getReflectionClass();
  41. $className = $reflectionClass->name;
  42. $loaded = false;
  43. $attributesMetadata = $classMetadata->getAttributesMetadata();
  44. foreach ($reflectionClass->getProperties() as $property) {
  45. if (!isset($attributesMetadata[$property->name])) {
  46. $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
  47. $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
  48. }
  49. if ($property->getDeclaringClass()->name === $className) {
  50. foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
  51. if ($annotation instanceof Groups) {
  52. foreach ($annotation->getGroups() as $group) {
  53. $attributesMetadata[$property->name]->addGroup($group);
  54. }
  55. } elseif ($annotation instanceof MaxDepth) {
  56. $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
  57. }
  58. $loaded = true;
  59. }
  60. }
  61. }
  62. foreach ($reflectionClass->getMethods() as $method) {
  63. if ($method->getDeclaringClass()->name !== $className) {
  64. continue;
  65. }
  66. $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
  67. if ($accessorOrMutator) {
  68. $attributeName = lcfirst($matches[2]);
  69. if (isset($attributesMetadata[$attributeName])) {
  70. $attributeMetadata = $attributesMetadata[$attributeName];
  71. } else {
  72. $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
  73. $classMetadata->addAttributeMetadata($attributeMetadata);
  74. }
  75. }
  76. foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
  77. if ($annotation instanceof Groups) {
  78. if (!$accessorOrMutator) {
  79. throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
  80. }
  81. foreach ($annotation->getGroups() as $group) {
  82. $attributeMetadata->addGroup($group);
  83. }
  84. } elseif ($annotation instanceof MaxDepth) {
  85. if (!$accessorOrMutator) {
  86. throw new MappingException(sprintf('MaxDepth on "%s::%s" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
  87. }
  88. $attributeMetadata->setMaxDepth($annotation->getMaxDepth());
  89. }
  90. $loaded = true;
  91. }
  92. }
  93. return $loaded;
  94. }
  95. }