Drupal investigation

AnnotationLoader.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Exception\MappingException;
  14. use Symfony\Component\Serializer\Mapping\AttributeMetadata;
  15. use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
  16. /**
  17. * Annotation loader.
  18. *
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. */
  21. class AnnotationLoader implements LoaderInterface
  22. {
  23. /**
  24. * @var Reader
  25. */
  26. private $reader;
  27. /**
  28. * @param Reader $reader
  29. */
  30. public function __construct(Reader $reader)
  31. {
  32. $this->reader = $reader;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function loadClassMetadata(ClassMetadataInterface $classMetadata)
  38. {
  39. $reflectionClass = $classMetadata->getReflectionClass();
  40. $className = $reflectionClass->name;
  41. $loaded = false;
  42. $attributesMetadata = $classMetadata->getAttributesMetadata();
  43. foreach ($reflectionClass->getProperties() as $property) {
  44. if (!isset($attributesMetadata[$property->name])) {
  45. $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
  46. $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
  47. }
  48. if ($property->getDeclaringClass()->name === $className) {
  49. foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
  50. if ($groups instanceof Groups) {
  51. foreach ($groups->getGroups() as $group) {
  52. $attributesMetadata[$property->name]->addGroup($group);
  53. }
  54. }
  55. $loaded = true;
  56. }
  57. }
  58. }
  59. foreach ($reflectionClass->getMethods() as $method) {
  60. if ($method->getDeclaringClass()->name === $className) {
  61. foreach ($this->reader->getMethodAnnotations($method) as $groups) {
  62. if ($groups instanceof Groups) {
  63. if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
  64. $attributeName = lcfirst($matches[2]);
  65. if (isset($attributesMetadata[$attributeName])) {
  66. $attributeMetadata = $attributesMetadata[$attributeName];
  67. } else {
  68. $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
  69. $classMetadata->addAttributeMetadata($attributeMetadata);
  70. }
  71. foreach ($groups->getGroups() as $group) {
  72. $attributeMetadata->addGroup($group);
  73. }
  74. } else {
  75. 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));
  76. }
  77. }
  78. $loaded = true;
  79. }
  80. }
  81. }
  82. return $loaded;
  83. }
  84. }