瀏覽代碼

[FrameworkBundle] added framework-wide annotation reader, updated validator tests

Johannes Schmitt 14 年之前
父節點
當前提交
7e26575bbd

+ 4 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

@@ -11,6 +11,7 @@
         <parameter key="filesystem.class">Symfony\Component\HttpKernel\Util\Filesystem</parameter>
         <parameter key="cache_warmer.class">Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate</parameter>
         <parameter key="file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter>
+        <parameter key="annotation_reader.class">Annotations\Reader</parameter>
     </parameters>
 
     <services>
@@ -51,5 +52,8 @@
             <argument type="service" id="kernel" />
             <argument>%kernel.root_dir%/Resources</argument>
         </service>
+        
+        <service id="annotation_reader" class="%annotation_reader.class%" public="false">
+        </service>
     </services>
 </container>

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml

@@ -48,6 +48,7 @@
         <service id="validator.mapping.loader.static_method_loader" class="%validator.mapping.loader.static_method_loader.class%" public="false" />
 
         <service id="validator.mapping.loader.annotation_loader" class="%validator.mapping.loader.annotation_loader.class%" public="false">
+            <argument type="service" id="annotation_reader" />
         </service>
 
         <service id="validator.mapping.loader.xml_files_loader" class="%validator.mapping.loader.xml_files_loader.class%" public="false">

+ 3 - 3
src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

@@ -11,7 +11,7 @@
 
 namespace Symfony\Component\Routing\Loader;
 
-use Doctrine\Common\Annotations\AnnotationReader;
+use Annotations\ReaderInterface;
 use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
 use Symfony\Component\Config\Resource\FileResource;
 use Symfony\Component\Routing\Route;
@@ -64,9 +64,9 @@ abstract class AnnotationClassLoader implements LoaderInterface
     /**
      * Constructor.
      *
-     * @param AnnotationReader $reader
+     * @param ReaderInterface $reader
      */
-    public function __construct(AnnotationReader $reader)
+    public function __construct(ReaderInterface $reader)
     {
         $this->reader = $reader;
     }

+ 3 - 7
src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

@@ -11,10 +11,9 @@
 
 namespace Symfony\Component\Validator\Mapping\Loader;
 
-use Annotations\Reader;
+use Annotations\ReaderInterface;
 use Symfony\Component\Validator\Exception\MappingException;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
-use Symfony\Component\Validator\Constraints\Set;
 use Symfony\Component\Validator\Constraints\GroupSequence;
 use Symfony\Component\Validator\Constraint;
 
@@ -22,12 +21,9 @@ class AnnotationLoader implements LoaderInterface
 {
     protected $reader;
 
-    public function __construct()
+    public function __construct(ReaderInterface $reader)
     {
-        $this->reader = new Reader();
-        $this->reader->setAutoloadAnnotations(true);
-        $this->reader->setIgnoreNotImportedAnnotations(false);
-        $this->reader->setIndexByClass(false);
+        $this->reader = $reader;
     }
 
     /**

+ 4 - 6
src/Symfony/Component/Validator/ValidatorFactory.php

@@ -11,6 +11,8 @@ namespace Symfony\Component\Validator;
  * file that was distributed with this source code.
  */
 
+use Annotations\Reader;
+
 use Symfony\Component\Validator\Exception\MappingException;
 use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
 use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
@@ -89,10 +91,6 @@ class ValidatorFactory implements ValidatorContextInterface
      *                                      found. Can be empty.
      * @param  Boolean $annotations         Whether to use annotations for
      *                                      retrieving mapping information
-     * @param  array $annotationNamespaces  The annotation namespaces used
-     *                                      for finding the annotation classes.
-     *                                      The namespace "validation" is used
-     *                                      by default
      * @param  string $staticMethod         The name of the static method to
      *                                      use, if static method loading should
      *                                      be enabled
@@ -100,7 +98,7 @@ class ValidatorFactory implements ValidatorContextInterface
      *                                      has neither the extension ".xml" nor
      *                                      ".yml" nor ".yaml"
      */
-    public static function buildDefault(array $mappingFiles = array(), $annotations = true, $annotationNamespaces = null, $staticMethod = null)
+    public static function buildDefault(array $mappingFiles = array(), $annotations = true, $staticMethod = null)
     {
         $xmlMappingFiles = array();
         $yamlMappingFiles = array();
@@ -128,7 +126,7 @@ class ValidatorFactory implements ValidatorContextInterface
         }
 
         if ($annotations) {
-            $loaders[] = new AnnotationLoader($annotationNamespaces);
+            $loaders[] = new AnnotationLoader(new Reader());
         }
 
         if ($staticMethod) {

+ 3 - 4
tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php

@@ -6,6 +6,7 @@ require_once __DIR__.'/EntityParent.php';
 require_once __DIR__.'/EntityInterface.php';
 
 /**
+ * @import("Symfony\Component\Validator\Constraints\*", alias="assert")
  * @Symfony\Tests\Component\Validator\Fixtures\ConstraintA
  * @assert:GroupSequence({"Foo", "Entity"})
  */
@@ -14,10 +15,8 @@ class Entity extends EntityParent implements EntityInterface
     /**
      * @assert:NotNull
      * @assert:Min(3)
-     * @assert:Set({
-     *   @assert:All({@assert:NotNull, @assert:Min(3)}),
-     *   @assert:All(constraints={@assert:NotNull, @assert:Min(3)})
-     * })
+     * @assert:All({@assert:NotNull, @assert:Min(3)}),
+     * @assert:All(constraints={@assert:NotNull, @assert:Min(3)})
      * @assert:Collection(fields={
      *   "foo" = {@assert:NotNull, @assert:Min(3)},
      *   "bar" = @assert:Min(5)

+ 3 - 0
tests/Symfony/Tests/Component/Validator/Fixtures/EntityParent.php

@@ -2,6 +2,9 @@
 
 namespace Symfony\Tests\Component\Validator\Fixtures;
 
+/**
+ * @import("Symfony\Component\Validator\Constraints\*", alias="assert")
+ */
 class EntityParent
 {
     protected $firstName;

+ 8 - 7
tests/Symfony/Tests/Component/Validator/Mapping/Loader/AnnotationLoaderTest.php

@@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Validator\Mapping\Loader;
 require_once __DIR__.'/../../Fixtures/Entity.php';
 require_once __DIR__.'/../../Fixtures/ConstraintA.php';
 
+use Annotations\Reader;
 use Symfony\Component\Validator\Constraints\All;
 use Symfony\Component\Validator\Constraints\Collection;
 use Symfony\Component\Validator\Constraints\NotNull;
@@ -27,14 +28,14 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
 {
     protected function setUp()
     {
-        if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
-            $this->markTestSkipped('Unmet dependency: doctrine-common is required for this test');
+        if (!class_exists('Annotations\Reader')) {
+            $this->markTestSkipped('Unmet dependency: Annotations is required for this test');
         }
     }
 
     public function testLoadClassMetadataReturnsTrueIfSuccessful()
     {
-        $loader = new AnnotationLoader();
+        $loader = new AnnotationLoader(new Reader());
         $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
 
         $this->assertTrue($loader->loadClassMetadata($metadata));
@@ -42,7 +43,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
 
     public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
     {
-        $loader = new AnnotationLoader();
+        $loader = new AnnotationLoader(new Reader());
         $metadata = new ClassMetadata('\stdClass');
 
         $this->assertFalse($loader->loadClassMetadata($metadata));
@@ -50,7 +51,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
 
     public function testLoadClassMetadata()
     {
-        $loader = new AnnotationLoader();
+        $loader = new AnnotationLoader($reader = new Reader());
         $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
 
         $loader->loadClassMetadata($metadata);
@@ -83,7 +84,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
      */
     public function testLoadParentClassMetadata()
     {
-        $loader = new AnnotationLoader();
+        $loader = new AnnotationLoader(new Reader());
 
         // Load Parent MetaData
         $parent_metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');
@@ -100,7 +101,7 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
      */
     public function testLoadClassMetadataAndMerge()
     {
-        $loader = new AnnotationLoader();
+        $loader = new AnnotationLoader($reader = new Reader());
 
         // Load Parent MetaData
         $parent_metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\EntityParent');

+ 13 - 16
tests/Symfony/Tests/Component/Validator/ValidatorFactoryTest.php

@@ -11,6 +11,7 @@
 
 namespace Symfony\Tests\Component\Validator;
 
+use Annotations\Reader;
 use Symfony\Component\Validator\Validator;
 use Symfony\Component\Validator\ValidatorContext;
 use Symfony\Component\Validator\ValidatorFactory;
@@ -75,14 +76,14 @@ class ValidatorFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testBuildDefaultFromAnnotations()
     {
-        if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
-            $this->markTestSkipped('Doctrine is required for this test');
+        if (!class_exists('Annotations\Reader')) {
+            $this->markTestSkipped('Annotations is required for this test');
         }
         $factory = ValidatorFactory::buildDefault();
 
         $context = new ValidatorContext();
         $context
-            ->setClassMetadataFactory(new ClassMetadataFactory(new AnnotationLoader()))
+            ->setClassMetadataFactory(new ClassMetadataFactory(new AnnotationLoader(new Reader())))
             ->setConstraintValidatorFactory(new ConstraintValidatorFactory());
 
         $this->assertEquals(new ValidatorFactory($context), $factory);
@@ -90,18 +91,14 @@ class ValidatorFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testBuildDefaultFromAnnotationsWithCustomNamespaces()
     {
-        if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
-            $this->markTestSkipped('Doctrine is required for this test');
+        if (!class_exists('Annotations\Reader')) {
+            $this->markTestSkipped('Annotations is required for this test');
         }
-        $factory = ValidatorFactory::buildDefault(array(), true, array(
-            'myns' => 'My\\Namespace\\',
-        ));
+        $factory = ValidatorFactory::buildDefault(array(), true);
 
         $context = new ValidatorContext();
         $context
-            ->setClassMetadataFactory(new ClassMetadataFactory(new AnnotationLoader(array(
-                'myns' => 'My\\Namespace\\',
-            ))))
+            ->setClassMetadataFactory(new ClassMetadataFactory(new AnnotationLoader(new Reader())))
             ->setConstraintValidatorFactory(new ConstraintValidatorFactory());
 
         $this->assertEquals(new ValidatorFactory($context), $factory);
@@ -136,7 +133,7 @@ class ValidatorFactoryTest extends \PHPUnit_Framework_TestCase
     public function testBuildDefaultFromStaticMethod()
     {
         $path = __DIR__.'/Mapping/Loader/constraint-mapping.yml';
-        $factory = ValidatorFactory::buildDefault(array(), false, null, 'loadMetadata');
+        $factory = ValidatorFactory::buildDefault(array(), false, 'loadMetadata');
 
         $context = new ValidatorContext();
         $context
@@ -148,17 +145,17 @@ class ValidatorFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testBuildDefaultFromMultipleLoaders()
     {
-        if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
-            $this->markTestSkipped('Doctrine is required for this test');
+        if (!class_exists('Annotations\Reader')) {
+            $this->markTestSkipped('Annotations is required for this test');
         }
         $xmlPath = __DIR__.'/Mapping/Loader/constraint-mapping.xml';
         $yamlPath = __DIR__.'/Mapping/Loader/constraint-mapping.yml';
-        $factory = ValidatorFactory::buildDefault(array($xmlPath, $yamlPath), true, null, 'loadMetadata');
+        $factory = ValidatorFactory::buildDefault(array($xmlPath, $yamlPath), true, 'loadMetadata');
 
         $chain = new LoaderChain(array(
             new XmlFilesLoader(array($xmlPath)),
             new YamlFilesLoader(array($yamlPath)),
-            new AnnotationLoader(),
+            new AnnotationLoader(new Reader()),
             new StaticMethodLoader('loadMetadata'),
         ));