bug #26899 [Routing] Fix loading multiple class annotations for invokable classes (1ed)
This PR was merged into the 3.4 branch.
Discussion
----------
[Routing] Fix loading multiple class annotations for invokable classes
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR | -
The support for full route definition for invokable controllers as class annotation was introduced by
https://github.com/symfony/symfony/pull/2172, but that works with one route only, which is inconsistent with how `@Route` works at other places. This PR adds support for multiple class annotations for invokable controllers and fixes the inconsistency.
<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
- Bug fixes must be submitted against the lowest branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
-->
Commits
-------
2a9c668ad7
[Routing] Fix loading multiple class annotations for invokable classes
This commit is contained in:
commit
8f2132fce3
@ -119,10 +119,15 @@ abstract class AnnotationClassLoader implements LoaderInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 === $collection->count() && $class->hasMethod('__invoke') && $annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
|
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
|
||||||
$globals['path'] = '';
|
foreach ($this->reader->getClassAnnotations($class) as $annot) {
|
||||||
$globals['name'] = '';
|
if ($annot instanceof $this->routeAnnotationClass) {
|
||||||
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
|
$globals['path'] = '';
|
||||||
|
$globals['name'] = '';
|
||||||
|
|
||||||
|
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $collection;
|
return $collection;
|
||||||
|
@ -191,9 +191,9 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->reader
|
$this->reader
|
||||||
->expects($this->exactly(2))
|
->expects($this->exactly(1))
|
||||||
->method('getClassAnnotation')
|
->method('getClassAnnotations')
|
||||||
->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
|
->will($this->returnValue(array($this->getAnnotatedRoute($classRouteData))))
|
||||||
;
|
;
|
||||||
$this->reader
|
$this->reader
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
@ -205,8 +205,49 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
$route = $routeCollection->get($classRouteData['name']);
|
$route = $routeCollection->get($classRouteData['name']);
|
||||||
|
|
||||||
$this->assertSame($classRouteData['path'], $route->getPath(), '->load preserves class route path');
|
$this->assertSame($classRouteData['path'], $route->getPath(), '->load preserves class route path');
|
||||||
$this->assertEquals(array_merge($classRouteData['schemes'], $classRouteData['schemes']), $route->getSchemes(), '->load preserves class route schemes');
|
$this->assertEquals($classRouteData['schemes'], $route->getSchemes(), '->load preserves class route schemes');
|
||||||
$this->assertEquals(array_merge($classRouteData['methods'], $classRouteData['methods']), $route->getMethods(), '->load preserves class route methods');
|
$this->assertEquals($classRouteData['methods'], $route->getMethods(), '->load preserves class route methods');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvokableClassMultipleRouteLoad()
|
||||||
|
{
|
||||||
|
$classRouteData1 = array(
|
||||||
|
'name' => 'route1',
|
||||||
|
'path' => '/1',
|
||||||
|
'schemes' => array('https'),
|
||||||
|
'methods' => array('GET'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$classRouteData2 = array(
|
||||||
|
'name' => 'route2',
|
||||||
|
'path' => '/2',
|
||||||
|
'schemes' => array('https'),
|
||||||
|
'methods' => array('GET'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->reader
|
||||||
|
->expects($this->exactly(1))
|
||||||
|
->method('getClassAnnotations')
|
||||||
|
->will($this->returnValue(array($this->getAnnotatedRoute($classRouteData1), $this->getAnnotatedRoute($classRouteData2))))
|
||||||
|
;
|
||||||
|
$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($classRouteData1['name']);
|
||||||
|
|
||||||
|
$this->assertSame($classRouteData1['path'], $route->getPath(), '->load preserves class route path');
|
||||||
|
$this->assertEquals($classRouteData1['schemes'], $route->getSchemes(), '->load preserves class route schemes');
|
||||||
|
$this->assertEquals($classRouteData1['methods'], $route->getMethods(), '->load preserves class route methods');
|
||||||
|
|
||||||
|
$route = $routeCollection->get($classRouteData2['name']);
|
||||||
|
|
||||||
|
$this->assertSame($classRouteData2['path'], $route->getPath(), '->load preserves class route path');
|
||||||
|
$this->assertEquals($classRouteData2['schemes'], $route->getSchemes(), '->load preserves class route schemes');
|
||||||
|
$this->assertEquals($classRouteData2['methods'], $route->getMethods(), '->load preserves class route methods');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvokableClassWithMethodRouteLoad()
|
public function testInvokableClassWithMethodRouteLoad()
|
||||||
|
@ -29,7 +29,7 @@ class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
|
|
||||||
public function testLoad()
|
public function testLoad()
|
||||||
{
|
{
|
||||||
$this->reader->expects($this->exactly(4))->method('getClassAnnotation');
|
$this->reader->expects($this->exactly(3))->method('getClassAnnotation');
|
||||||
|
|
||||||
$this->reader
|
$this->reader
|
||||||
->expects($this->any())
|
->expects($this->any())
|
||||||
@ -37,6 +37,12 @@ class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
->will($this->returnValue(array()))
|
->will($this->returnValue(array()))
|
||||||
;
|
;
|
||||||
|
|
||||||
|
$this->reader
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getClassAnnotations')
|
||||||
|
->will($this->returnValue(array()))
|
||||||
|
;
|
||||||
|
|
||||||
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
|
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +51,6 @@ class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
$this->expectAnnotationsToBeReadFrom(array(
|
$this->expectAnnotationsToBeReadFrom(array(
|
||||||
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
|
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
|
||||||
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
|
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
|
||||||
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
|
|
||||||
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
|
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -55,6 +60,12 @@ class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
|
|||||||
->will($this->returnValue(array()))
|
->will($this->returnValue(array()))
|
||||||
;
|
;
|
||||||
|
|
||||||
|
$this->reader
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getClassAnnotations')
|
||||||
|
->will($this->returnValue(array()))
|
||||||
|
;
|
||||||
|
|
||||||
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
|
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user