-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Console\Event; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly
+1 |
1 similar comment
👍 |
*/ | ||
public function run(InputInterface $input, OutputInterface $output) | ||
{ | ||
$doDispatch = $this->dispatcher && !$this->isSilent(); |
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the isSilent here ?
There was a problem hiding this comment.
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.
👍 |
private $dispatcher; | ||
|
||
/** | ||
* Wether or not events should be dispatched for the command. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx, fixed
I second this PR, very much needed. |
What are the use-cases for this? The only one I saw was the swiftmailer Ideally this capability should be on top of ContainerAwareCommand not to Excepted commands one define himself, most of them are just isolated I'd suggest to have an EventDispatchingCommand extending What do you think? |
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 ?") |
For the swiftmailer issue my fault indeed it's about sending emails from I'm not against making all commands dispatching events in the absolute, |
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. |
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the dispatcher here ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
from my pov this PR is now finished. |
@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
argh... it should be fixed now, thx |
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 |
ping |
I think it would be nice to have it in 2.2 too. |
+1000 |
+1 |
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. |
+1 I was looking for such an implementation, it would be nice to have it in 2.2 @fabpot ping |
I need it too @fabpot ping |
I've added the helperSet as per @marcospassos suggestion and also made the tests more robusts by checking both events are getting dispatched properly. |
Up |
@fabpot ping |
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. |
Fair enough :) |
i guess now its time to ping again :) |
@fabpot ping ;) |
closing in favor of #7466 |
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
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.