Skip to content

Commit bfdb73d

Browse files
Merge branch '4.0'
* 4.0: Fix tests PropertyInfo\DoctrineExtractor - There is bug when indexBy is meta key Fix PercentType error rendering. [minor] SCA [Cache] Inline some hot function calls fixed Silex project's URL fixed deprecations in tests fixed Twig URL [Cache] Add missing `@internal` tag on ProxyTrait fix formatting arguments in plaintext format Fix PSR exception context key Don't assume that file binary exists on *nix OS Fix that ESI/SSI processing can turn a \"private\" response \"public\" [Form] Fixed trimming choice values fix rendering exception stack traces [Routing] Fix loading multiple class annotations for invokable classes
2 parents 04e063d + 1dfbfdf commit bfdb73d

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

Loader/AnnotationClassLoader.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,16 @@ public function load($class, $type = null)
120120
}
121121
}
122122

123-
/** @var $annot RouteAnnotation */
124-
if (0 === $collection->count() && $class->hasMethod('__invoke') && $annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
125-
$globals['path'] = null;
126-
$globals['name'] = '';
127-
$globals['localized_paths'] = array();
128-
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
123+
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
124+
foreach ($this->reader->getClassAnnotations($class) as $annot) {
125+
if ($annot instanceof $this->routeAnnotationClass) {
126+
$globals['path'] = '';
127+
$globals['name'] = '';
128+
$globals['localized_paths'] = array();
129+
130+
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
131+
}
132+
}
129133
}
130134

131135
return $collection;

Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\Annotations\AnnotationRegistry;
16-
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
1717
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
1818
use Symfony\Component\Routing\Route;
1919
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\AbstractClassController;
@@ -35,7 +35,7 @@
3535
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionPathController;
3636
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RouteWithPrefixController;
3737

38-
class AnnotationClassLoaderTest extends TestCase
38+
class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
3939
{
4040
/**
4141
* @var AnnotationClassLoader
@@ -166,6 +166,51 @@ public function testLocalizedPrefixLocalizedRoute()
166166
$this->assertEquals('/en/action', $routes->get('action.en')->getPath());
167167
}
168168

169+
public function testInvokableClassMultipleRouteLoad()
170+
{
171+
$classRouteData1 = array(
172+
'name' => 'route1',
173+
'path' => '/1',
174+
'schemes' => array('https'),
175+
'methods' => array('GET'),
176+
);
177+
178+
$classRouteData2 = array(
179+
'name' => 'route2',
180+
'path' => '/2',
181+
'schemes' => array('https'),
182+
'methods' => array('GET'),
183+
);
184+
185+
$reader = $this->getReader();
186+
$reader
187+
->expects($this->exactly(1))
188+
->method('getClassAnnotations')
189+
->will($this->returnValue(array(new RouteAnnotation($classRouteData1), new RouteAnnotation($classRouteData2))))
190+
;
191+
$reader
192+
->expects($this->once())
193+
->method('getMethodAnnotations')
194+
->will($this->returnValue(array()))
195+
;
196+
$loader = new class($reader) extends AnnotationClassLoader {
197+
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot) {}
198+
};
199+
200+
$routeCollection = $loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass');
201+
$route = $routeCollection->get($classRouteData1['name']);
202+
203+
$this->assertSame($classRouteData1['path'], $route->getPath(), '->load preserves class route path');
204+
$this->assertEquals($classRouteData1['schemes'], $route->getSchemes(), '->load preserves class route schemes');
205+
$this->assertEquals($classRouteData1['methods'], $route->getMethods(), '->load preserves class route methods');
206+
207+
$route = $routeCollection->get($classRouteData2['name']);
208+
209+
$this->assertSame($classRouteData2['path'], $route->getPath(), '->load preserves class route path');
210+
$this->assertEquals($classRouteData2['schemes'], $route->getSchemes(), '->load preserves class route schemes');
211+
$this->assertEquals($classRouteData2['methods'], $route->getMethods(), '->load preserves class route methods');
212+
}
213+
169214
public function testMissingPrefixLocale()
170215
{
171216
$this->expectException(\LogicException::class);

Tests/Loader/AnnotationDirectoryLoaderTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ protected function setUp()
2929

3030
public function testLoad()
3131
{
32-
$this->reader->expects($this->exactly(4))->method('getClassAnnotation');
32+
$this->reader->expects($this->exactly(3))->method('getClassAnnotation');
3333

3434
$this->reader
3535
->expects($this->any())
3636
->method('getMethodAnnotations')
3737
->will($this->returnValue(array()))
3838
;
3939

40+
$this->reader
41+
->expects($this->any())
42+
->method('getClassAnnotations')
43+
->will($this->returnValue(array()))
44+
;
45+
4046
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
4147
}
4248

@@ -45,7 +51,6 @@ public function testLoadIgnoresHiddenDirectories()
4551
$this->expectAnnotationsToBeReadFrom(array(
4652
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
4753
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
48-
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass',
4954
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
5055
));
5156

@@ -55,6 +60,12 @@ public function testLoadIgnoresHiddenDirectories()
5560
->will($this->returnValue(array()))
5661
;
5762

63+
$this->reader
64+
->expects($this->any())
65+
->method('getClassAnnotations')
66+
->will($this->returnValue(array()))
67+
;
68+
5869
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
5970
}
6071

0 commit comments

Comments
 (0)