Skip to content

[WIP][Console] Add ContainerAwareCommand #10087

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 1 commit into from

Conversation

realityking
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

In the spirit of #7435 I propose adding a ContainerAwareCommand to the Console Component. This is helpful when implementing both the DIC and the Console.

I'm fairly certain the code remaining in ContainerAwareCommand in the FrameworkBundle could also be dropped as the Container is injected into every command by the Application. This would also make it a little easier to move code between Projects using the Framework and those using only the Components.

Todo:

  • Get feedback whether the code remaining in FrameworkBundle is actually necessary.

* @return ContainerInterface
*/
protected function getContainer()
{
if (null === $this->container) {
if (null === parent::getContainer()) {
$this->container = $this->getApplication()->getKernel()->getContainer();
Copy link
Contributor

Choose a reason for hiding this comment

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

You have to either use setContainer or make $container protected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, overlooked that one. I'll wait for the feedback if we still need this piece of code before fixing it.

@GromNaN
Copy link
Member

GromNaN commented Jan 20, 2014

Do you have an example of application using both the Console and DependencyInjection components without the fullstack framework ?

I would give advantage to a proper dependency injection instead of promoting ContainerAware things. Like what I've done for the command twig:lint (#9855).

@realityking
Copy link
Contributor Author

Motivation is our in house application that is not open source. Until there's a way to lazy load services for commands we're not ready to go DIC for them as we have some frequently running cronjobs using the console.

Another application would be phpBB, but as far as I can tell they're using dependency injection for their commands.

I'd agree that this is probably a rare set up, so this is probably rather unattractive if ContainerAwareCommand can't be removed completely from FrameworkBundle.

@stof
Copy link
Member

stof commented Jan 20, 2014

I don't see the benefit of adding this class in the Console component: the container won't be injected in the command unless you use an Application implementation supporting it, which is not in the component either.

and the class in FrameworkBundle cannot be removed for BC reasons

@realityking
Copy link
Contributor Author

I should have worded that last part more carefully. We obviously need to retain the class in the FrameworkBundle for 2.x. I meant if we can remove all the code in it so we can eventually remove it.

@realityking
Copy link
Contributor Author

I did my homework, there are a few open source projects that have a copy of this code in their repository:

@fabpot
Copy link
Member

fabpot commented Mar 3, 2014

I also fail to see the benefit of such a class in the Console component as the most important code to make it work transparently is still in the framework bundle: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Console/Application.php#L80-L84.

/**
* Command.
*
* @author Fabien Potencier <fabien@symfony.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

he is not the author, or add yourself too right? 👶

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well since it's essentially a copy of Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand plus a small modification to getContainer so that only seemed fair.

@realityking
Copy link
Contributor Author

@fabpot I understand where you're coming from, as this is "some assembly required". As mentioned above, this only makes sense if we can remove this condition: https://github.com/realityking/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php#L35

I think it's safe, but I'd like someone with more knowledge of the ins and outs to confirm that.

@fabpot
Copy link
Member

fabpot commented Mar 26, 2014

@realityking The code you mention can be safely removed, but again, you would still need the code from the FrameworkBundle Application class to make this work "out of the box".

@gnugat
Copy link
Contributor

gnugat commented Apr 13, 2014

@GromNaN I do have some CLI projects which use the DIC, however I've never injected the container in my commands...

@GromNaN
Copy link
Member

GromNaN commented Apr 13, 2014

@gnugat I'm using a lot the Console standalone or with Pimple and I regret that so many open-source Commands are tied to the Symfony/DI component.
Not injecting the container is great ! So you don't need the ContainerAwareCommand proposed in this PR.

@gnugat
Copy link
Contributor

gnugat commented Apr 13, 2014

@GromNaN nobody needs the ContainerAwareCommand when using the Console and the DIC without FrameworkBundle. As @fabpot said, implementing this interface only have sense when using https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Console/Application.php#L80-L84.

The Console's default Application doesn't inject the container to commands which implement this interface. @realityking If you want to inject the container into your commands, you don't need ContainerAwareCommand.

@fabpot
Copy link
Member

fabpot commented Jul 25, 2014

Closing now.

@fabpot fabpot closed this Jul 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants