10000 [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

Conversation

dunglas
Copy link
Member
@dunglas dunglas commented Mar 24, 2016
Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

if a service is invokable and has the same name than its class name, the controller resolver of FrameworkBundle doesn't retrieve the service and tries to construct a new instance of the class instead.

This is a very rare edge case, but this fix is useful for dunglas/DunglasActionBundle#36: referencing auto-registered controllers following the ADR style in YAML and XML routing files will be more intuitive.

Currently: defaults: { _controller: 'Your\Action\FQN:__invoke' }, after this fix: defaults: { _controller: 'Your\Action\FQN' }.

This PR also fix a currently useless test.

@dunglas dunglas added the Ready label Mar 24, 2016
@dunglas dunglas changed the title [FrameworkBundle] Return the service if it's name is the class name [FrameworkBundle] Return the service if its name is the class name Mar 24, 2016
@dunglas dunglas force-pushed the invokable_service branch from 15ebcff to 0a7ea2c Compare March 24, 2016 00:21
@dunglas dunglas changed the title [FrameworkBundle] Return the service if its name is the class name [FrameworkBundle] Return the invokable service if its name is the class name Mar 24, 2016
$controller = $this->container->get($class);
} else {
$controller = parent::instantiateController($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

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.

@xabbuh
Copy link
Member
xabbuh commented Mar 25, 2016

👍 (with @stof's comment applied)

@@ -78,7 +78,11 @@ protected function createController($controller)
*/
protected function instantiateController($class)
{
$controller = parent::instantiateController($class);
if ($this->container->has($class)) {
$controller = $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.

the simple way to fix my comment about ContainerAwareInterface is to return directly here

@stof
Copy link
Member
stof commented Mar 25, 2016

Status: needs work

@dunglas
Copy link
Member Author
dunglas commented Mar 25, 2016

@stof fixed

Status: needs review

@fabpot
Copy link
Member
fabpot commented Mar 25, 2016

Thank you @dunglas.

@fabpot fabpot closed this in f9b0aaa Mar 25, 2016
@dunglas dunglas deleted the invokable_service branch March 25, 2016 17:28
@dunglas
Copy link
Member Author
dunglas commented Mar 25, 2016

I've unintentionally opened this PR against master, but it should be merged in 2.7 (as described in the PR header). @fabpot is it possible to merge it back in other branches?

fabpot added a commit that referenced this pull request Apr 1, 2016
… name is the class name (dunglas)

This PR was merged into the 2.7 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/A
| License       | MIT
| Doc PR        | n/a

Backport #18289 to 2.7 as this is a bug fix.

Commits
-------

5c87d76 [FrameworkBundle] Return the invokable service if its name is the class name
@fabpot fabpot mentioned this pull request May 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0