8000 [Routing] Remove duplicate schemes and methods for invokable controllers by claudusd · Pull Request #29274 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Remove duplicate schemes and methods for invokable controllers #29274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(
Expand Down
0