Skip to content

Commit

Permalink
Allowed returning exit code in console actions
Browse files Browse the repository at this point in the history
Allowed returning integer values as application exit code in
CConsoleCommand actions.
Application will now exit with the given integer values as exit code.
This is better than just exit(1) because onAfterAction event is called
And you can use the return value when doing
Yii::app()->getCommandRunner()->run(...) to run a specific command
inside an other command.
  • Loading branch information
cebe committed Apr 27, 2012
1 parent 0f3ab34 commit 86b75f3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Version 1.1.11 work in progress
- Enh #601: added the method loginRequired() to the IWebUser interface (mdomba)
- Enh #641: Added support for cache entry serialization through the igbinary serializer (DaSourcerer)
- Enh: Added default value to CConsoleCommand::confirm (musterknabe)
- Enh: Allowed returning integer values as application exit code in CConsoleCommand actions (cebe)
- Enh: Added third parameter to CHttpCookie to configure the cookie by array (suralc)
- Enh: Added getIsFlashRequest(), proper handling of Flash/Flex request when using CWebLogRoute with FireBug (resurtm)
- Enh: Added CBreadcrumbs::$activeLinkTemplate and CBreadcrumbs::$inactiveLinkTemplate properties which allows to change each item's template (resurtm)
Expand Down
1 change: 1 addition & 0 deletions UPGRADE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ General upgrade intructions

Upgrading from v1.1.10
----------------------
- API of protected method CConsoleCommand::afterAction() changed, if you are overiding this method make sure to update your code.

Upgrading from v1.1.9
---------------------
Expand Down
5 changes: 4 additions & 1 deletion framework/console/CConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ protected function init()
/**
* Processes the user request.
* This method creates a console command runner to handle the particular user command.
* Since version 1.1.11 this method will exit application with an exit code if one is returned by the user command.
*/
public function processRequest()
{
$this->_runner->run($_SERVER['argv']);
$exitCode=$this->_runner->run($_SERVER['argv']);
if(is_int($exitCode))
exit($exitCode);
}

/**
Expand Down
17 changes: 13 additions & 4 deletions framework/console/CConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* }
* </pre>
*
* Since version 1.1.11 the return value of action methods will be used as application exit code if it is an integer value.
*
* @property string $name The command name.
* @property CConsoleCommandRunner $commandRunner The command runner instance.
* @property string $help The command description. Defaults to 'Usage: php entry-script.php command-name'.
Expand Down Expand Up @@ -110,6 +112,9 @@ public function behaviors()
* dispatch the command request to an appropriate action with the corresponding
* option values
* @param array $args command line parameters for this command.
* @return int|null application exit code returned by the action
* will return null when action has been aborted by {@link onBeforeAction} event
* (return value is available since version 1.1.11)
*/
public function run($args)
{
Expand Down Expand Up @@ -165,8 +170,8 @@ public function run($args)

if($this->beforeAction($action,$params))
{
$method->invokeArgs($this,$params);
$this->afterAction($action,$params);
$exitCode=$method->invokeArgs($this,$params);
return $this->afterAction($action,$params,$exitCode);
}
}

Expand Down Expand Up @@ -196,11 +201,15 @@ protected function beforeAction($action,$params)
* You may override this method to do some postprocessing for the action.
* @param string $action the action name
* @param array $params the parameters to be passed to the action method.
* @param int|null $exitCode the application exit code returned by the action method.
* @return int|null application exit code (return value is available since version 1.1.11)
*/
protected function afterAction($action,$params)
protected function afterAction($action,$params,$exitCode=null)
{
$event=new CConsoleCommandEvent($this,$params,$action,$exitCode);
if($this->hasEventHandler('onAfterAction'))
$this->onAfterAction(new CConsoleCommandEvent($this, $params, $action));
$this->onAfterAction($event);
return $event->exitCode;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion framework/console/CConsoleCommandEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ class CConsoleCommandEvent extends CEvent
* {@link CConsoleCommand::afterAction}.
*/
public $stopCommand=false;
/**
* @var int exit code of application.
* This property is available in {@link CConsoleCommand::onAfterAction} event and will be set to the exit code
* returned by the console command action. You can set it to change application exit code.
*/
public $exitCode;

/**
* Constructor.
* @param mixed $sender sender of the event
* @param string $params the parameters to be passed to the action method.
* @param string $action the action name
*/
public function __construct($sender=null,$params=null,$action=null){
public function __construct($sender=null,$params=null,$action=null,$exitCode=0){
parent::__construct($sender,$params);
$this->action=$action;
$this->exitCode=$exitCode;
}
}
4 changes: 3 additions & 1 deletion framework/console/CConsoleCommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CConsoleCommandRunner extends CComponent
/**
* Executes the requested command.
* @param array $args list of user supplied parameters (including the entry script name and the command name).
* @return int|null application exit code returned by the command
* (return value is available since version 1.1.11)
*/
public function run($args)
{
Expand All @@ -62,7 +64,7 @@ public function run($args)
if(($command=$this->createCommand($name))===null)
$command=$this->createCommand('help');
$command->init();
$command->run($args);
return $command->run($args);
}

/**
Expand Down

0 comments on commit 86b75f3

Please sign in to comment.