AnnotationLoader.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Validator\Mapping\Loader;
  11. use Doctrine\Common\Annotations\Reader;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Constraints\Callback;
  14. use Symfony\Component\Validator\Constraints\GroupSequence;
  15. use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
  16. use Symfony\Component\Validator\Exception\MappingException;
  17. use Symfony\Component\Validator\Mapping\ClassMetadata;
  18. /**
  19. * Loads validation metadata using a Doctrine annotation {@link Reader} or using PHP 8 attributes.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. * @author Alexander M. Turek <me@derrabus.de>
  23. */
  24. class AnnotationLoader implements LoaderInterface
  25. {
  26. protected $reader;
  27. public function __construct(Reader $reader = null)
  28. {
  29. $this->reader = $reader;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function loadClassMetadata(ClassMetadata $metadata)
  35. {
  36. $reflClass = $metadata->getReflectionClass();
  37. $className = $reflClass->name;
  38. $success = false;
  39. foreach ($this->getAnnotations($reflClass) as $constraint) {
  40. if ($constraint instanceof GroupSequence) {
  41. $metadata->setGroupSequence($constraint->groups);
  42. } elseif ($constraint instanceof GroupSequenceProvider) {
  43. $metadata->setGroupSequenceProvider(true);
  44. } elseif ($constraint instanceof Constraint) {
  45. $metadata->addConstraint($constraint);
  46. }
  47. $success = true;
  48. }
  49. foreach ($reflClass->getProperties() as $property) {
  50. if ($property->getDeclaringClass()->name === $className) {
  51. foreach ($this->getAnnotations($property) as $constraint) {
  52. if ($constraint instanceof Constraint) {
  53. $metadata->addPropertyConstraint($property->name, $constraint);
  54. }
  55. $success = true;
  56. }
  57. }
  58. }
  59. foreach ($reflClass->getMethods() as $method) {
  60. if ($method->getDeclaringClass()->name === $className) {
  61. foreach ($this->getAnnotations($method) as $constraint) {
  62. if ($constraint instanceof Callback) {
  63. $constraint->callback = $method->getName();
  64. $metadata->addConstraint($constraint);
  65. } elseif ($constraint instanceof Constraint) {
  66. if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
  67. $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
  68. } else {
  69. throw new MappingException(sprintf('The constraint on "%s::%s()" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
  70. }
  71. }
  72. $success = true;
  73. }
  74. }
  75. }
  76. return $success;
  77. }
  78. /**
  79. * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection
  80. */
  81. private function getAnnotations(object $reflection): iterable
  82. {
  83. if (\PHP_VERSION_ID >= 80000) {
  84. foreach ($reflection->getAttributes(GroupSequence::class) as $attribute) {
  85. yield $attribute->newInstance();
  86. }
  87. foreach ($reflection->getAttributes(GroupSequenceProvider::class) as $attribute) {
  88. yield $attribute->newInstance();
  89. }
  90. foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
  91. yield $attribute->newInstance();
  92. }
  93. }
  94. if (!$this->reader) {
  95. return;
  96. }
  97. if ($reflection instanceof \ReflectionClass) {
  98. yield from $this->reader->getClassAnnotations($reflection);
  99. }
  100. if ($reflection instanceof \ReflectionMethod) {
  101. yield from $this->reader->getMethodAnnotations($reflection);
  102. }
  103. if ($reflection instanceof \ReflectionProperty) {
  104. yield from $this->reader->getPropertyAnnotations($reflection);
  105. }
  106. }
  107. }