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

Skip to content

[FrameworkBundle] Added events for CLI commands #6124

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 5 commits into from
Closed

[FrameworkBundle] Added events for CLI commands #6124

wants to merge 5 commits into from

Conversation

bamarni
Copy link
Contributor

@bamarni bamarni commented Nov 26, 2012

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

Fixes the following tickets: #1929, #3889

I've allowed myself to continue the effort from @flevour in #3889 as it was only missing unit tests and a small tweak from my pov, I've kept the authorship in the commit.

* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Console\Event;
Copy link
Member

Choose a reason for hiding this comment

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

I would move the event object outside the Console namespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean Symfony\Bundle\FrameworkBundle\Event\ConsoleEvent?

Copy link
Member

Choose a reason for hiding this comment

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

exactly

@lsmith77
Copy link
Contributor

+1

1 similar comment
@everzet
Copy link
Contributor

everzet commented Nov 27, 2012

👍

*/
public function run(InputInterface $input, OutputInterface $output)
{
$doDispatch = $this->dispatcher && !$this->isSilent();
Copy link
Contributor

Choose a reason for hiding this comment

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

Better do an isset($this->dispatcher) ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why the isSilent here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rande : I think it's better to check it here too, relying only on whether or not the dispatcher is there is not entirely true as it is just an optimization of the current application, avoiding the service to be build when it is not needed, checking here too makes the command more robust.

@lolautruche : I don't think it is a big deal, and I often see this without isset() in symfony code.

@lolautruche
Copy link
Contributor

👍

private $dispatcher;

/**
* Wether or not events should be dispatched for the command.
Copy link
Contributor

Choose a reason for hiding this comment

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

Whether

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx, fixed

@DenisGorbachev
Copy link

I second this PR, very much needed.

@bamarni
Copy link
Contributor Author

bamarni commented Dec 8, 2012

What are the use-cases for this? The only one I saw was the swiftmailer
command to send emails from the spool, which currently fails when it
comes to memory spool, as it uses the kernel.terminate event.

Ideally this capability should be on top of ContainerAwareCommand not to
mess up the Application, here I've kind of blindly implemented what was
discussed on the original ticket, which was to add this on all commands
excepted cache:clear, but it just leads to a dirty hack imo.

Excepted commands one define himself, most of them are just isolated
tools for the application / developer, as such it looks weird to me to
know that potentially emails could be sent when I run any of them.

I'd suggest to have an EventDispatchingCommand extending
ContainerAwareCommand, and that's it. I'd also suggest to provide traits
support for this, which should be the best way to easily plug
capabilities to commands on the long-term whitout running into hacks.

What do you think?

@stof
Copy link
Member

stof commented Dec 8, 2012

I see another use case: setting the base url in the routing RequestContext so that generated urls are right.

and for Swiftmailer, it would send emails only if your command send an email. The MemorySpool will never send an email spooled in a different proces.

I don't like the idea of introducing a separate class for this. It would mean that every developer writing a command would have to decide if the command will dispatch events or no, making it confusing for people using the event ("Why don't I see the event for this command ?")

@bamarni
Copy link
Contributor Author

bamarni commented Dec 8, 2012

For the swiftmailer issue my fault indeed it's about sending emails from
commands with the memory setting, and for your other use case yes it's
interesting, while those use cases are very common (eg. newsletter), I
don't see why they should affect all other commands, people needing
those kind of feature could just extend a command on top of
ContainerAwareCommand (EventDispatchingCommand or even AbstractCommand
to allow new features later on),

I'm not against making all commands dispatching events in the absolute,
but unfortunately it complicates the code, remember it'd also forces us
to allow disabling it (cache clear command) or to somehow manually
disabling it for this command, while I could practically revert all the
changes of this PR if we choose my first suggestion and come up with a
single class addition.

@lsmith77
Copy link
Contributor

lsmith77 commented Dec 8, 2012

my initial use case was a header I had to set on specific http connections that were triggered both on the web site as well as CLI.

@bamarni
Copy link
Contributor Author

bamarni commented Dec 9, 2012

I've just took time to look at it and I've been able to implement @stof suggestion without so much pain, so I'm fine with all commands dispatching events.

{
protected $dispatcher;
Copy link
Member

Choose a reason for hiding this comment

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

why the dispatcher here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

typo => container
Le 9 déc. 2012 15:07, "Christophe Coevoet" [email protected] a
écrit :

In src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:

{

  • protected $dispatcher;

why the dispatcher here ?


Reply to this email directly or view it on GitHubhttps://github.com//pull/6124/files#r2358459.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

This adds an init and terminate event for commands. They are
dispatched from ContainerAwareCommand.

The cache:clear command can't implement this (cf. #3889 on Github).
@bamarni
Copy link
Contributor Author

bamarni commented Dec 9, 2012

from my pov this PR is now finished.

@ghost
Copy link

ghost commented Dec 29, 2012

@bamarni - I think you need to rebase this to current master as it cannot be automatically merged as it stands.

Conflicts:
	src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
@bamarni
Copy link
Contributor Author

bamarni commented Dec 29, 2012

argh... it should be fixed now, thx

@bamarni
Copy link
Contributor Author

bamarni commented Jan 6, 2013

Is it too late for adding this to 2.2? As a reminder this in not only a new feature, but it'd provide a way to fix a bug, with the standard edition, by default, all mails sent from commands are lost (cf. symfony/symfony-standard#249).

cc @fabpot

@lsmith77
Copy link
Contributor

ping

@attilabukor
Copy link
Contributor

I think it would be nice to have it in 2.2 too.

@rande
Copy link
Contributor

rande commented Jan 30, 2013

+1000

@marcospassos
Copy link

+1

@marcospassos
Copy link

IMO the dialog helper should be passed through the event. I've an use case of a multitenant application that I need to ask the host before some commands are executed (like doctrine:*), in order to set the RequestContext.

@XWB
Copy link
Contributor

XWB commented Feb 5, 2013

+1 I was looking for such an implementation, it would be nice to have it in 2.2

@fabpot ping

@startupz
Copy link

startupz commented Feb 6, 2013

I need it too

@fabpot ping

@bamarni
Copy link
Contributor Author

bamarni commented Feb 6, 2013

I've added the helperSet as per @marcospassos suggestion and also made the tests more robusts by checking both events are getting dispatched properly.

@startupz
Copy link

Up

@XWB
Copy link
Contributor

XWB commented Feb 22, 2013

@fabpot ping

@bamarni
Copy link
Contributor Author

bamarni commented Feb 22, 2013

there is no need to ping I guess :) 2.2 is in rc now, so it's obviously too late, and this is not the best moment for the core team to review 2.3 features.

@XWB
Copy link
Contributor

XWB commented Feb 22, 2013

Fair enough :)

@lsmith77
Copy link
Contributor

lsmith77 commented Mar 6, 2013

i guess now its time to ping again :)

@XWB
Copy link
Contributor

XWB commented Mar 7, 2013

@fabpot ping ;)

@fabpot
Copy link
Member

fabpot commented Mar 25, 2013

closing in favor of #7466

@fabpot fabpot closed this Mar 25, 2013
fabpot added a commit that referenced this pull request Mar 25, 2013
This PR was merged into the master branch.

Discussion
----------

Console dispatcher

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #3889, #6124
| License       | MIT
| Doc PR        | symfony/symfony-docs#2352

refs #1884, #1929

This is an alternative implementation for adding events to console applications.

This implementation has the following features:

* Available for anyone using the Console component and it is not tied to
  FrameworkBundle (this is important as one thing we are trying to solve is
  email sending from a command, and frameworks like Silex using the Console
  component needs a solution too);

* Non-intrusive as the current code has not been changed (except for renaming
  an internal variable that was wrongly named -- so that's not strictly needed
  for this PR)

* The new DispatchableApplication class also works without a dispatcher,
  falling back to the regular behavior. That makes easy to create applications
  that can benefit from a dispatcher when available, but can still work
  otherwise.

* Besides the *before* and *after* events, there is also an *exception* event
  that is dispatched whenever an exception is thrown.

* Each event is quite powerful and can manipulate the input, the output, but
  also the command to be executed.

Commits
-------

4f9a55a refactored the implementation of how a console application can handle events
4edf29d added helperSet to console event objects
f224102 Added events for CLI commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.