Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 812a878

Browse filesBrowse files
committed
bug #29274 [Routing] Remove duplicate schemes and methods for invokable controllers (claudusd)
This PR was merged into the 3.4 branch. Discussion ---------- [Routing] Remove duplicate schemes and methods for invokable controllers | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29225 | License | MIT This PR backport for 3.4 branch the same issue than the PR #29225. I add a test to check the fix when annotation are on the class and rename another one when the route annotation is on the invoke method. Commits ------- 640ccdf [Routing] Remove duplicate schemes and methods for invokable controllers
2 parents d713671 + 640ccdf commit 812a878
Copy full SHA for 812a878

File tree

2 files changed

+53
-15
lines changed
Filter options

2 files changed

+53
-15
lines changed

‎src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
+17-14Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ public function load($class, $type = null)
120120
}
121121

122122
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
123+
$globals = $this->resetGlobals();
123124
foreach ($this->reader->getClassAnnotations($class) as $annot) {
124125
if ($annot instanceof $this->routeAnnotationClass) {
125-
$globals['path'] = '';
126-
$globals['name'] = '';
127-
128126
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
129127
}
130128
}
@@ -212,17 +210,7 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho
212210

213211
protected function getGlobals(\ReflectionClass $class)
214212
{
215-
$globals = array(
216-
'path' => '',
217-
'requirements' => array(),
218-
'options' => array(),
219-
'defaults' => array(),
220-
'schemes' => array(),
221-
'methods' => array(),
222-
'host' => '',
223-
'condition' => '',
224-
'name' => '',
225-
);
213+
$globals = $this->resetGlobals();
226214

227215
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
228216
if (null !== $annot->getName()) {
@@ -265,6 +253,21 @@ protected function getGlobals(\ReflectionClass $class)
265253
return $globals;
266254
}
267255

256+
private function resetGlobals()
257+
{
258+
return array(
259+
'path' => '',
260+
'requirements' => array(),
261+
'options' => array(),
262+
'defaults' => array(),
263+
'schemes' => array(),
264+
'methods' => array(),
265+
'host' => '',
266+
'condition' => '',
267+
'name' => '',
268+
);
269+
}
270+
268271
protected function createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition)
269272
{
270273
return new Route($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testClassRouteLoad()
181181
$this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods');
182182
}
183183

184-
public function testInvokableClassRouteLoad()
184+
public function testInvokableClassRouteLoadWithMethodAnnotation()
185185
{
186186
$classRouteData = array(
187187
'name' => 'route1',
@@ -209,6 +209,41 @@ public function testInvokableClassRouteLoad()
209209
$this->assertEquals($classRouteData['methods'], $route->getMethods(), '->load preserves class route methods');
210210
}
211211

212+
public function testInvokableClassRouteLoadWithClassAnnotation()
213+
{
214+
$classRouteData = array(
215+
'name' => 'route1',
216+
'path' => '/',
217+
'schemes' => array('https'),
218+
'methods' => array('GET'),
219+
);
220+
221+
$this->reader
222+
->expects($this->exactly(1))
223+
->method('getClassAnnotation')
224+
->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
225+
;
226+
227+
$this->reader
228+
->expects($this->exactly(1))
229+
->method('getClassAnnotations')
230+
->will($this->returnValue(array($this->getAnnotatedRoute($classRouteData))))
231+
;
232+
233+
$this->reader
234+
->expects($this->once())
235+
->method('getMethodAnnotations')
236+
->will($this->returnValue(array()))
237+
;
238+
239+
$routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass');
240+
$route = $routeCollection->get($classRouteData['name']);
241+
242+
$this->assertSame($classRouteData['path'], $route->getPath(), '->load preserves class route path');
243+
$this->assertEquals($classRouteData['schemes'], $route->getSchemes(), '->load preserves class route schemes');
244+
$this->assertEquals($classRouteData['methods'], $route->getMethods(), '->load preserves class route methods');
245+
}
246+
212247
public function testInvokableClassMultipleRouteLoad()
213248
{
214249
$classRouteData1 = array(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.