Browse Source

[Validator] forced all validation annotations to be in the validation namespace to avoid collisions, removed the need for the wrapping @Validation annotation

Before:

    /**
     * @Validation({@DateTime()})
     */

After:

    /**
     * @validation:DateTime()
     */

The @validation:Validation() construct is not needed anymore (it is still supported
as this is useful when you have several annotations with the same class).

So, the above is equivalent to:

    /**
     * @validation:Validation({@validation:DateTime()})
     */
Fabien Potencier 14 năm trước cách đây
mục cha
commit
0fc8906feb

+ 24 - 11
src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

@@ -14,6 +14,8 @@ namespace Symfony\Component\Validator\Mapping\Loader;
 use Symfony\Component\Validator\Exception\MappingException;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
 use Doctrine\Common\Annotations\AnnotationReader;
+use Symfony\Component\Validator\Constraints\Validation;
+use Symfony\Component\Validator\Constraint;
 
 class AnnotationLoader implements LoaderInterface
 {
@@ -22,8 +24,8 @@ class AnnotationLoader implements LoaderInterface
     public function __construct()
     {
         $this->reader = new AnnotationReader();
-        $this->reader->setDefaultAnnotationNamespace('Symfony\Component\Validator\Constraints\\');
         $this->reader->setAutoloadAnnotations(true);
+        $this->reader->setAnnotationNamespaceAlias('Symfony\Component\Validator\Constraints\\', 'validation');
     }
 
     /**
@@ -31,12 +33,15 @@ class AnnotationLoader implements LoaderInterface
      */
     public function loadClassMetadata(ClassMetadata $metadata)
     {
-        $annotClass = 'Symfony\Component\Validator\Constraints\Validation';
         $reflClass = $metadata->getReflectionClass();
         $loaded = false;
 
-        if ($annot = $this->reader->getClassAnnotation($reflClass, $annotClass)) {
-            foreach ($annot->constraints as $constraint) {
+        foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
+            if ($constraint instanceof Validation) {
+                foreach ($constraint->constraints as $constraint) {
+                    $metadata->addConstraint($constraint);
+                }
+            } elseif ($constraint instanceof Constraint) {
                 $metadata->addConstraint($constraint);
             }
 
@@ -44,8 +49,12 @@ class AnnotationLoader implements LoaderInterface
         }
 
         foreach ($reflClass->getProperties() as $property) {
-            if ($annot = $this->reader->getPropertyAnnotation($property, $annotClass)) {
-                foreach ($annot->constraints as $constraint) {
+            foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
+                if ($constraint instanceof Validation) {
+                    foreach ($constraint->constraints as $constraint) {
+                        $metadata->addPropertyConstraint($property->getName(), $constraint);
+                    }
+                } elseif ($constraint instanceof Constraint) {
                     $metadata->addPropertyConstraint($property->getName(), $constraint);
                 }
 
@@ -54,11 +63,15 @@ class AnnotationLoader implements LoaderInterface
         }
 
         foreach ($reflClass->getMethods() as $method) {
-            if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
-                foreach ($annot->constraints as $constraint) {
-                    // TODO: clean this up
-                    $name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
+            foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
+                // TODO: clean this up
+                $name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
 
+                if ($constraint instanceof Validation) {
+                    foreach ($constraint->constraints as $constraint) {
+                        $metadata->addGetterConstraint($name, $constraint);
+                    }
+                } elseif ($constraint instanceof Constraint) {
                     $metadata->addGetterConstraint($name, $constraint);
                 }
 
@@ -68,4 +81,4 @@ class AnnotationLoader implements LoaderInterface
 
         return $loaded;
     }
-}
+}

+ 14 - 18
tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php

@@ -6,25 +6,23 @@ require_once __DIR__.'/EntityParent.php';
 require_once __DIR__.'/EntityInterface.php';
 
 /**
- * @Validation({
- *   @NotNull,
- *   @Symfony\Tests\Component\Validator\Fixtures\ConstraintA,
- *   @Min(3),
- *   @Choice({"A", "B"}),
- *   @All({@NotNull, @Min(3)}),
- *   @All(constraints={@NotNull, @Min(3)}),
- *   @Collection(fields={
- *     "foo" = {@NotNull, @Min(3)},
- *     "bar" = @Min(5)
- *   })
+ * @validation:NotNull
+ * @Symfony\Tests\Component\Validator\Fixtures\ConstraintA
+ * @validation:Min(3)
+ * @validation:Choice({"A", "B"})
+ * @validation:Validation({
+ *   @validation:All({@validation:NotNull, @validation:Min(3)}),
+ *   @validation:All(constraints={@validation:NotNull, @validation:Min(3)})
+ * })
+ * @validation:Collection(fields={
+ *   "foo" = {@validation:NotNull, @validation:Min(3)},
+ *   "bar" = @validation:Min(5)
  * })
  */
 class Entity extends EntityParent implements EntityInterface
 {
     /**
-     * @Validation({
-     *   @Choice(choices={"A", "B"}, message="Must be one of %choices%")
-     * })
+     * @validation:Choice(choices={"A", "B"}, message="Must be one of %choices%")
      */
     protected $firstName;
 
@@ -43,12 +41,10 @@ class Entity extends EntityParent implements EntityInterface
     }
 
     /**
-     * @Validation({
-     *   @NotNull
-     * })
+     * @validation:NotNull
      */
     public function getLastName()
     {
         return $this->lastName;
     }
-}
+}