Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allowed returning exit code in console actions

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...
commit 86b75f31f1f07432f13615e5eb869bf911fd4542 1 parent 0f3ab34
Carsten Brandt cebe authored
1  CHANGELOG
View
@@ -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)
1  UPGRADE
View
@@ -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
---------------------
5 framework/console/CConsoleApplication.php
View
@@ -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);
}
/**
17 framework/console/CConsoleCommand.php
View
@@ -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'.
@@ -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)
{
@@ -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);
}
}
@@ -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;
}
/**
9 framework/console/CConsoleCommandEvent.php
View
@@ -30,6 +30,12 @@ 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.
@@ -37,8 +43,9 @@ class CConsoleCommandEvent extends CEvent
* @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 framework/console/CConsoleCommandRunner.php
View
@@ -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)
{
@@ -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);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.