From 640ccdf06924bed2f05bdfcfd9cc9379e266e566 Mon Sep 17 00:00:00 2001 From: Claude Dioudonnat Date: Wed, 21 Nov 2018 20:04:39 +0100 Subject: [PATCH] [Routing] Remove duplicate schemes and methods for invokable controllers --- .../Routing/Loader/AnnotationClassLoader.php | 31 +++++++++------- .../Loader/AnnotationClassLoaderTest.php | 37 ++++++++++++++++++- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php index 8c96835826a9..76a120b8efd0 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php @@ -120,11 +120,9 @@ public function load($class, $type = null) } if (0 === $collection->count() && $class->hasMethod('__invoke')) { + $globals = $this->resetGlobals(); foreach ($this->reader->getClassAnnotations($class) as $annot) { if ($annot instanceof $this->routeAnnotationClass) { - $globals['path'] = ''; - $globals['name'] = ''; - $this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke')); } } @@ -212,17 +210,7 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho protected function getGlobals(\ReflectionClass $class) { - $globals = array( - 'path' => '', - 'requirements' => array(), - 'options' => array(), - 'defaults' => array(), - 'schemes' => array(), - 'methods' => array(), - 'host' => '', - 'condition' => '', - 'name' => '', - ); + $globals = $this->resetGlobals(); if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) { if (null !== $annot->getName()) { @@ -265,6 +253,21 @@ protected function getGlobals(\ReflectionClass $class) return $globals; } + private function resetGlobals() + { + return array( + 'path' => '', + 'requirements' => array(), + 'options' => array(), + 'defaults' => array(), + 'schemes' => array(), + 'methods' => array(), + 'host' => '', + 'condition' => '', + 'name' => '', + ); + } + protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition) { return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition); diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php index beab0b2e835a..a0230ada88ec 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php @@ -181,7 +181,7 @@ public function testClassRouteLoad() $this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods'); } - public function testInvokableClassRouteLoad() + public function testInvokableClassRouteLoadWithMethodAnnotation() { $classRouteData = array( 'name' => 'route1', @@ -209,6 +209,41 @@ public function testInvokableClassRouteLoad() $this->assertEquals($classRouteData['methods'], $route->getMethods(), '->load preserves class route methods'); } + public function testInvokableClassRouteLoadWithClassAnnotation() + { + $classRouteData = array( + 'name' => 'route1', + 'path' => '/', + 'schemes' => array('https'), + 'methods' => array('GET'), + ); + + $this->reader + ->expects($this->exactly(1)) + ->method('getClassAnnotation') + ->will($this->returnValue($this->getAnnotatedRoute($classRouteData))) + ; + + $this->reader + ->expects($this->exactly(1)) + ->method('getClassAnnotations') + ->will($this->returnValue(array($this->getAnnotatedRoute($classRouteData)))) + ; + + $this->reader + ->expects($this->once()) + ->method('getMethodAnnotations') + ->will($this->returnValue(array())) + ; + + $routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass'); + $route = $routeCollection->get($classRouteData['name']); + + $this->assertSame($classRouteData['path'], $route->getPath(), '->load preserves class route path'); + $this->assertEquals($classRouteData['schemes'], $route->getSchemes(), '->load preserves class route schemes'); + $this->assertEquals($classRouteData['methods'], $route->getMethods(), '->load preserves class route methods'); + } + public function testInvokableClassMultipleRouteLoad() { $classRouteData1 = array(