Skip to content

Commit

Permalink
Adding full-featured events CConsoleCommand::onBeforeAction & CConsol…
Browse files Browse the repository at this point in the history
…eCommand::onAfterAction
  • Loading branch information
Yiivgeny committed Feb 16, 2012
1 parent a1ccd42 commit cc531e4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -4,6 +4,7 @@

Version 1.1.11 work in progress
-------------------------------
- Enh: Added full-featured events CConsoleCommand::onBeforeAction & CConsoleCommand::onAfterAction (Yiivgeny)
- Enh #171: Added support for PUT and DELETE request tunneled through POST via parameter named _method in POST body (musterknabe)


Expand Down
1 change: 1 addition & 0 deletions framework/YiiBase.php
Expand Up @@ -677,6 +677,7 @@ public static function registerAutoloader($callback, $append=false)
'CConsoleApplication' => '/console/CConsoleApplication.php',
'CConsoleCommand' => '/console/CConsoleCommand.php',
'CConsoleCommandRunner' => '/console/CConsoleCommandRunner.php',
'CConsoleCommandEvent' => '/console/CConsoleCommandEvent.php',
'CHelpCommand' => '/console/CHelpCommand.php',
'CDbCommand' => '/db/CDbCommand.php',
'CDbConnection' => '/db/CDbConnection.php',
Expand Down
35 changes: 33 additions & 2 deletions framework/console/CConsoleCommand.php
Expand Up @@ -152,7 +152,16 @@ public function run($args)
*/
protected function beforeAction($action,$params)
{
return true;
if($this->hasEventHandler('onBeforeAction'))
{
$event = new CConsoleCommandEvent($this, $params, $action);
$this->onBeforeAction($event);
return $event->isContinue;
}
else
{
return true;
}
}

/**
Expand All @@ -163,6 +172,8 @@ protected function beforeAction($action,$params)
*/
protected function afterAction($action,$params)
{
if($this->hasEventHandler('onAfterAction'))
$this->onAfterAction(new CConsoleCommandEvent($this, $params, $action));
}

/**
Expand Down Expand Up @@ -499,4 +510,24 @@ public function confirm($message)
echo $message.' [yes|no] ';
return !strncasecmp(trim(fgets(STDIN)),'y',1);
}
}

/**
* This event is raised before an action is to be executed.
* @param CConsoleCommandEvent $event the event parameter
* @since 1.1.11
*/
public function onBeforeAction($event)
{
$this->raiseEvent('onBeforeAction', $event);
}

/**
* This event is raised after an action finishes execution.
* @param CConsoleCommandEvent $event the event parameter
* @since 1.1.11
*/
public function onAfterAction($event)
{
$this->raiseEvent('onAfterAction',$event);
}
}
44 changes: 44 additions & 0 deletions framework/console/CConsoleCommandEvent.php
@@ -0,0 +1,44 @@
<?php
/**
* CConsoleCommandEvent class file.
*
* @author Evgeny Blinov <e.a.blinov@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* CConsoleCommandEvent class.
*
* CConsoleCommandEvent represents the event parameters needed by events raised by a console command.
*
* @author Evgeny Blinov <e.a.blinov@gmail.com>
* @package system.console
* @since 1.1.11
*/
class CConsoleCommandEvent extends CEvent
{
/**
* @var string the action name
*/
public $action;
/**
* @var boolean whether the action should be executed.
* If this property is set false by the event handler, the console command action will quit after handling this event.
* If true, the normal execution cycles will continue, including performing the action and calling
* {@link CConsoleCommand::afterAction}.
*/
public $isContinue = true;

/**
* 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, $params, $action){
parent::__construct($sender, $params);
$this->action = $action;
}
}

0 comments on commit cc531e4

Please sign in to comment.