8000 [FrameworkBundle] Return the invokable service if its name is the class name by dunglas · Pull Request #18289 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ protected function createController($controller)
*/
protected function instantiateController($class)
{
if ($this->container->has($class)) {
return $this->container->get($class);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make that check in createController() where we also retrieve the controller from the container when the service_name:method_name notation is used: https://github.com/dunglas/symfony/blob/invokable_service/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php#L56-L71

8000
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not possible without a larger refactoring because createController is never called in this case: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php#L70

An alternative is to refactor the whole getController method but it's risky and intrusive. It's why I've chosen this simple fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah, it's probably not worth to make such big changes.


$controller = parent::instantiateController($class);

if ($controller instanceof ContainerAwareInterface) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be done when getting the class from the container. It should happen only when instantiating

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function testGetControllerService()

public function testGetControllerInvokableService()
{
$invokableController = new InvokableController('bar');

$container = $this->createMockContainer();
$container->expects($this->once())
->method('has')
Expand All @@ -96,7 +98,7 @@ public function testGetControllerInvokableService()
$container->expects($this->once())
->method('get')
->with('foo')
->will($this->returnValue($this))
->will($this->returnValue($invokableController))
;

$resolver = $this->createControllerResolver(null, null, $container);
Expand All @@ -105,7 +107,33 @@ public function testGetControllerInvokableService()

$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);
}

/**
Expand Down Expand Up @@ -178,3 +206,14 @@ public function __invoke()
{
}
}

class InvokableController
{
public function __construct($bar) // mandatory argument to prevent automatic instantiation
{
}

public function __invoke()
{
}
}
0