Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged branch fabpot/console-dispatcher (PR #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
- Loading branch information
Showing
9 changed files
with
423 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console; | ||
|
||
/** | ||
* Contains all events dispatched by an Application. | ||
* | ||
* @author Francesco Levorato <git@flevour.net> | ||
*/ | ||
final class ConsoleEvents | ||
{ | ||
/** | ||
* The COMMAND event allows you to attach listeners before any command is | ||
* executed by the console. It also allows you to modify the command, input and output | ||
* before they are handled to the command. | ||
* | ||
* The event listener method receives a Symfony\Component\Console\Event\ConsoleCommandEvent | ||
* instance. | ||
* | ||
* @var string | ||
*/ | ||
const COMMAND = 'console.command'; | ||
|
||
/** | ||
* The TERMINATE event allows you to attach listeners after a command is | ||
* executed by the console. | ||
* | ||
* The event listener method receives a Symfony\Component\Console\Event\ConsoleTerminateEvent | ||
* instance. | ||
* | ||
* @var string | ||
*/ | ||
const TERMINATE = 'console.terminate'; | ||
|
||
/** | ||
* The EXCEPTION event occurs when an uncaught exception appears. | ||
* | ||
* This event allows you to deal with the exception or | ||
* to modify the thrown exception. The event listener method receives | ||
* a Symfony\Component\Console\Event\ConsoleForExceptionEvent | ||
* instance. | ||
* | ||
* @var string | ||
*/ | ||
const EXCEPTION = 'console.exception'; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Console/Event/ConsoleCommandEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Event; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Allows to do things before the command is executed. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ConsoleCommandEvent extends ConsoleEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Event; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Allows to inspect input and output of a command. | ||
* | ||
* @author Francesco Levorato <git@flevour.net> | ||
*/ | ||
class ConsoleEvent extends Event | ||
{ | ||
protected $command; | ||
|
||
private $input; | ||
private $output; | ||
|
||
public function __construct(Command $command, InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->command = $command; | ||
$this->input = $input; | ||
$this->output = $output; | ||
} | ||
|
||
/** | ||
* Gets the command that is executed. | ||
* | ||
* @return Command A Command instance | ||
*/ | ||
public function getCommand() | ||
{ | ||
return $this->command; | ||
} | ||
|
||
/** | ||
* Gets the input instance. | ||
* | ||
* @return InputInterface An InputInterface instance | ||
*/ | ||
public function getInput() | ||
{ | ||
return $this->input; | ||
} | ||
|
||
/** | ||
* Gets the output instance. | ||
* | ||
* @return OutputInterface An OutputInterface instance | ||
*/ | ||
public function getOutput() | ||
{ | ||
return $this->output; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Console/Event/ConsoleForExceptionEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Event; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Allows to handle exception thrown in a command. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ConsoleForExceptionEvent extends ConsoleEvent | ||
{ | ||
private $exception; | ||
private $exitCode; | ||
|
||
public function __construct(Command $command, InputInterface $input, OutputInterface $output, \Exception $exception, $exitCode) | ||
{ | ||
parent::__construct($command, $input, $output); | ||
|
||
$this->setException($exception); | ||
$this->exitCode = $exitCode; | ||
} | ||
|
||
/** | ||
* Returns the thrown exception. | ||
* | ||
* @return \Exception The thrown exception | ||
*/ | ||
public function getException() | ||
{ | ||
return $this->exception; | ||
} | ||
|
||
/** | ||
* Replaces the thrown exception. | ||
* | ||
* This exception will be thrown if no response is set in the event. | ||
* | ||
* @param \Exception $exception The thrown exception | ||
*/ | ||
public function setException(\Exception $exception) | ||
{ | ||
$this->exception = $exception; | ||
} | ||
|
||
/** | ||
* Gets the exit code. | ||
* | ||
* @return integer The command exit code | ||
*/ | ||
public function getExitCode() | ||
{ | ||
return $this->exitCode; | ||
} | ||
} |
Oops, something went wrong.