AnnotationLoader.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Symfony\Component\Validator\Mapping\Loader;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. use Symfony\Component\Validator\Exception\MappingException;
  12. use Symfony\Component\Validator\Mapping\ClassMetadata;
  13. use Doctrine\Common\Annotations\AnnotationReader;
  14. use Symfony\Component\Validator\Constraints\Validation;
  15. use Symfony\Component\Validator\Constraint;
  16. class AnnotationLoader implements LoaderInterface
  17. {
  18. protected $reader;
  19. public function __construct()
  20. {
  21. $this->reader = new AnnotationReader();
  22. $this->reader->setAutoloadAnnotations(true);
  23. $this->reader->setAnnotationNamespaceAlias('Symfony\Component\Validator\Constraints\\', 'validation');
  24. }
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function loadClassMetadata(ClassMetadata $metadata)
  29. {
  30. $reflClass = $metadata->getReflectionClass();
  31. $className = $reflClass->getName();
  32. $loaded = false;
  33. foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
  34. if ($constraint instanceof Validation) {
  35. foreach ($constraint->constraints as $constraint) {
  36. $metadata->addConstraint($constraint);
  37. }
  38. } elseif ($constraint instanceof Constraint) {
  39. $metadata->addConstraint($constraint);
  40. }
  41. $loaded = true;
  42. }
  43. foreach ($reflClass->getProperties() as $property) {
  44. if ($property->getDeclaringClass()->getName() == $className) {
  45. foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
  46. if ($constraint instanceof Validation) {
  47. foreach ($constraint->constraints as $constraint) {
  48. $metadata->addPropertyConstraint($property->getName(), $constraint);
  49. }
  50. } elseif ($constraint instanceof Constraint) {
  51. $metadata->addPropertyConstraint($property->getName(), $constraint);
  52. }
  53. $loaded = true;
  54. }
  55. }
  56. }
  57. foreach ($reflClass->getMethods() as $method) {
  58. if ($method->getDeclaringClass()->getName() == $className) {
  59. foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
  60. // TODO: clean this up
  61. $name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
  62. if ($constraint instanceof Validation) {
  63. foreach ($constraint->constraints as $constraint) {
  64. $metadata->addGetterConstraint($name, $constraint);
  65. }
  66. } elseif ($constraint instanceof Constraint) {
  67. $metadata->addGetterConstraint($name, $constraint);
  68. }
  69. $loaded = true;
  70. }
  71. }
  72. }
  73. return $loaded;
  74. }
  75. }