Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Don't create unused alias if the command is public #22410

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
Apr 13, 2017

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Apr 12, 2017

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

Currently, a public alias is created for any console.command tagged service, but this alias is unused if the service is public (as it would be useless).

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍

@nicolas-grekas nicolas-grekas added this to the 3.2 milestone Apr 13, 2017
@fabpot
Copy link
Member

fabpot commented Apr 13, 2017

Merging into 3.3 as this is not a bug fix.

@fabpot
Copy link
Member

fabpot commented Apr 13, 2017

Can you rebase on master? That would help me. Thanks.

@chalasr chalasr force-pushed the dont-alias-public-cmds branch from 56d9eb2 to 32d6476 Compare April 13, 2017 16:54
@chalasr chalasr changed the base branch from 3.2 to master April 13, 2017 16:55
@chalasr chalasr force-pushed the dont-alias-public-cmds branch from 32d6476 to cd4a01c Compare April 13, 2017 16:59
@chalasr
Copy link
Member Author

chalasr commented Apr 13, 2017

Rebased on master.

$alias2 = $alias1.'_my-command2';
$this->assertTrue($container->hasAlias($alias1));
$this->assertTrue($container->hasAlias($alias2));
}
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 test has been added to the legacy test case by mistake, copied it there.

@fabpot
Copy link
Member

fabpot commented Apr 13, 2017

Thank you @chalasr.

@fabpot fabpot merged commit cd4a01c into symfony:master Apr 13, 2017
fabpot added a commit that referenced this pull request Apr 13, 2017
…nd is public (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle] Don't create unused alias if the command is public

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

Currently, a public alias is created for any `console.command` tagged service, but this alias is unused if the service is public (as it would be useless).

Commits
-------

cd4a01c Don't create unused aliases for public command
@chalasr chalasr deleted the dont-alias-public-cmds branch April 13, 2017 18:09
@GuilhemN
Copy link
Contributor

Actually, it was used here so this should be reverted in big part (we can only remove console.command.$fqcn.$id when the command service is public).

@chalasr
Copy link
Member Author

chalasr commented Apr 28, 2017

I'll take care of this.

@chalasr
Copy link
Member Author

chalasr commented Apr 29, 2017

@GuilhemN In fact public command services are still properly registered. What happens:

  • Here If the command is located in a Command directory + the command classname is suffixed by Command + it has no required constructor arguments (can occur for services with only method calls) then a new instance of the command is created via reflection and registered in the application
  • Here The application registers all the commands present in console.command.ids(which contain public service ids) by loading them from the container so that the public service instance replaces the reflection one and, at the end, is the one used when running the command.

So the behavior is not broken, but causes an unnecessary reflection class instantiation when it is possible (which should rarely be the case but still) followed by some method calls.
I will propose to fix this by mapping commands ids by their alias in console.command.ids (even if this one is not registered in the container i.e for public services), then skip reflection if the predictable alias exists as a key of console.command.ids.

@GuilhemN
Copy link
Contributor

Ok, thanks for investigating deeper than I did, it works as is but the line I mentionned is meant to prevent the reflection call so having your fix would be great indeed ☺

@chalasr
Copy link
Member Author

chalasr commented Apr 29, 2017

See #22579

fabpot added a commit that referenced this pull request Apr 29, 2017
…for command public services (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Console][HttpKernel] Avoid reflection-based registration for command public services

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22410 (comment)
| License       | MIT
| Doc PR        | n/a

By mapping commands ids by their alias in `console.command.ids` (even if the alias is not registered in the container for public services), then skipping reflection if the predictable alias exists as a key of `console.command.ids`.

Please note that the whole command service registration process is far from ideal.
I'm working on changing this for 3.4 in a transparent way regarding end users, leveraging PSR-11 to make the console component DI friendly, allowing to register commands as true private services (no more public aliases) and providing laziness for those.

Commits
-------

6c1b384 Avoid reflection-based registration for command public services
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