[FrameworkBundle] Return the invokable service if its name is the class name

This commit is contained in:
Kévin Dunglas 2016-03-24 01:09:44 +01:00
parent ba1cd26237
commit 5c87d766d5
2 changed files with 45 additions and 2 deletions

View File

@ -78,6 +78,10 @@ class ControllerResolver extends BaseControllerResolver
*/
protected function instantiateController($class)
{
if ($this->container->has($class)) {
return $this->container->get($class);
}
$controller = parent::instantiateController($class);
if ($controller instanceof ContainerAwareInterface) {

View File

@ -87,6 +87,8 @@ class ControllerResolverTest extends BaseControllerResolverTest
public function testGetControllerInvokableService()
{
$invokableController = new InvokableController('bar');
$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
@ -96,7 +98,7 @@ class ControllerResolverTest extends BaseControllerResolverTest
$container->expects($this->once())
->method('get')
->with('foo')
->will($this->returnValue($this))
->will($this->returnValue($invokableController))
;
$resolver = $this->createControllerResolver(null, null, $container);
@ -105,7 +107,33 @@ class ControllerResolverTest extends BaseControllerResolverTest
$controller = $resolver->getController($request);
$this->assertInstanceOf(get_class($this), $controller);
$this->assertEquals($invokableController, $controller);
}
public function testGetControllerInvokableServiceWithClassNameAsName()
{
$invokableController = new InvokableController('bar');
$className = __NAMESPACE__.'\InvokableController';
$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
->with($className)
->will($this->returnValue(true))
;
$container->expects($this->once())
->method('get')
->with($className)
->will($this->returnValue($invokableController))
;
$resolver = $this->createControllerResolver(null, null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', $className);
$controller = $resolver->getController($request);
$this->assertEquals($invokableController, $controller);
}
/**
@ -178,3 +206,14 @@ class ContainerAwareController implements ContainerAwareInterface
{
}
}
class InvokableController
{
public function __construct($bar) // mandatory argument to prevent automatic instantiation
{
}
public function __invoke()
{
}
}