Skip to content

Commit 34e360a

Browse files
committed
Add full route definition to invokable class
1 parent 00ab4b3 commit 34e360a

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public function load($class, $type = null)
127127
}
128128
}
129129

130+
if (0 === $collection->count() && $class->hasMethod('__invoke') && $annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
131+
$globals['path'] = '';
132+
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
133+
}
134+
130135
return $collection;
131136
}
132137

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses;
13+
14+
class BazClass
15+
{
16+
public function __invoke()
17+
{
18+
}
19+
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,73 @@ public function testClassRouteLoad()
180180
$this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods');
181181
}
182182

183+
public function testInvokableClassRouteLoad()
184+
{
185+
$classRouteData = array(
186+
'name' => 'route1',
187+
'path' => '/',
188+
'schemes' => array('https'),
189+
'methods' => array('GET'),
190+
);
191+
192+
$this->reader
193+
->expects($this->exactly(2))
194+
->method('getClassAnnotation')
195+
->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
196+
;
197+
$this->reader
198+
->expects($this->once())
199+
->method('getMethodAnnotations')
200+
->will($this->returnValue(array()))
201+
;
202+
203+
$routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass');
204+
$route = $routeCollection->get($classRouteData['name']);
205+
206+
$this->assertSame($classRouteData['path'], $route->getPath(), '->load preserves class route path');
207+
$this->assertEquals(array_merge($classRouteData['schemes'], $classRouteData['schemes']), $route->getSchemes(), '->load preserves class route schemes');
208+
$this->assertEquals(array_merge($classRouteData['methods'], $classRouteData['methods']), $route->getMethods(), '->load preserves class route methods');
209+
}
210+
211+
public function testInvokableClassWithMethodRouteLoad()
212+
{
213+
$classRouteData = array(
214+
'name' => 'route1',
215+
'path' => '/prefix',
216+
'schemes' => array('https'),
217+
'methods' => array('GET'),
218+
);
219+
220+
$methodRouteData = array(
221+
'name' => 'route2',
222+
'path' => '/path',
223+
'schemes' => array('http'),
224+
'methods' => array('POST', 'PUT'),
225+
);
226+
227+
$this->reader
228+
->expects($this->once())
229+
->method('getClassAnnotation')
230+
->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
231+
;
232+
$this->reader
233+
->expects($this->once())
234+
->method('getMethodAnnotations')
235+
->will($this->returnValue(array($this->getAnnotatedRoute($methodRouteData))))
236+
;
237+
238+
$routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BazClass');
239+
$route = $routeCollection->get($classRouteData['name']);
240+
241+
$this->assertNull($route, '->load ignores class route');
242+
243+
$route = $routeCollection->get($methodRouteData['name']);
244+
245+
$this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path');
246+
$this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');
247+
$this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods');
248+
}
249+
183250
private function getAnnotatedRoute($data)
184251
{
185252
return new Route($data);

src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function setUp()
2929

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

3434
$this->reader
3535
->expects($this->any())

0 commit comments

Comments
 (0)